Ebooks, Audobooks and Classical Music from Liber Liber
a b c d e f g h i j k l m n o p q r s t u v w x y z





Web - Amazon

We provide Linux to the World


We support WINRAR [What is this] - [Download .exe file(s) for Windows]

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
SITEMAP
Audiobooks by Valerio Di Stefano: Single Download - Complete Download [TAR] [WIM] [ZIP] [RAR] - Alphabetical Download  [TAR] [WIM] [ZIP] [RAR] - Download Instructions

Make a donation: IBAN: IT36M0708677020000000008016 - BIC/SWIFT:  ICRAITRRU60 - VALERIO DI STEFANO or
Privacy Policy Cookie Policy Terms and Conditions
Ngăn xếp – Wikipedia tiếng Việt

Ngăn xếp

Bách khoa toàn thư mở Wikipedia


Lược đồ một ngăn xếp
Lược đồ một ngăn xếp

Trong khoa học máy tính, một ngăn xếp(tiếng Anh :stack) là một cấu trúc dữ liệu trừu tượng hoạt động theo nguyên lý "vào sau ra trước" (Last In First Out (LIFO).

Mục lục

[sửa] Kiểu dữ liệu trừu tượng ngăn xếp

Một ngăn xếp là một cấu trúc dữ liệu dạng thùng chứa (container) của các phần tử (thường gọi là các nút (node)) và có hai phép toán cơ bản : push and pop. Push bổ xung một phần tử vào đỉnh (top) của ngăn xếp,nghiã là sau các phần tử đã có trong ngăn xếp. Pop giải phóng và trả về phần tử đang đứng ở đỉnh của nhăn xếp. Trong stack, các đối tượng có thể được thêm vào stack bất kỳ lúc nào nhưng chỉ có đối tượng thêm vào sau cùng mới được phép lấy ra khỏi stack.

Ngoài ra, stack cũng hỗ trợ một số thao tác khác:

  • isEmpty(): Kiểm tra xem stack có rỗng không.
  • Top(): Trả về giá trị của phần tử nằm ở đầu stack mà không hủy nó khỏi stack. Nếu stack rỗng thì lỗi sẽ xảy ra.


[sửa] Các phép toán

Trong ngôn ngữ máy tính hiện nay, một ngăn xếp thường được dùng với các phép toán "push" và "pop". Độ dài (số phần tử) của ngăn xếp cũng là một tham số cần thiết của nó. Đôi khi cần tới một phép toán chỉ cần lấy giá trị của phần từ trên đỉnh ngăn xếp mà không xóa nó khỏi ngăn xếp (peak).

Sau đây là đoạn mã giả để bổ sung, loại bỏ, tính độ dài, và lấy giá trị phần tử ở đỉnh của ngăn xếp.

 record Node {
    data // The data being stored in the node
    next // A reference to the next node; null for last node
 }
 record Stack {
     Node stackPointer   // points to the 'top' node; null for an empty stack
 }
 function push(Stack stack, Node newNode) { // push node onto Stack
     newNode.next   := Stack.stackPointer
     stack.stackPointer := newNode
 }
 function pop(Stack stack) { // increase the stack pointer and return 'top' node
     // You could check if stack.stackPointer is null here.
     // If so, you may wish to error, citing the stack underflow.
     node := stack.stackPointer
     stack.stackPointer := stack.stackPointer.next
     return node
 }
 function peak(Stack stack) { // return 'top' node
     return stack.stackPointer
 }
 function length(Stack stack) { // return the amount of nodes in the stack
     length := 0
     node := stack.stackPointer
     while node not null {
         length := length + 1
         node := node.next
     }
     return length
 }


[sửa] Ứng dụng

Ngăn xếp có nhiều ứng dụng trong khoa học máy tính.

[sửa] Tính các biểu thức đại số

Ví dụ: Tính biểu thức

((1 + 2) * 4) + 3

Trước hết viết biểu thức theo hậu thứ tự:

1 2 + 4 * 3 +

Khi tính một biểu thức ta dùng một ngăn xếp để cất dữ liệu trung gian như sau: Đọc từ trái sang phải mỗi lần một phần tử:

  • nếu gặp một toán hạng (biến hoặc hằng), đặt (push)toán hạng đó vào ngăn xếp;
  • nếu gạp một toán tử, lấy (pop) hai phần tử trên đỉnh ngăn xếp ra, thực hiện phép toán và đặt (push) lại kết quả vào ngăn xếp.
Input Phép toán Ngăn xếp
1 Push(1) 1
2 Push(2) 1, 2
+ Pop(2), Pop(1), 1+2=3; Push(3) 3
4 Push(4) 3, 4
* Pop(4), Pop(3), 3*4=12; Push(12) 12
3 Push(3) 12, 3
+ Pop(3), Pop(12), 12+3=15; Push(15) 15

Kết quả 15 đặt ở đỉnh của nhăn xếp khi tính xong.

[sửa] Quản lý bộ nhớ khi thi hành chương trình

[sửa] Xem thêm

  • Hàng đợi

[sửa] Tham khảo

  • Donald Knuth. The Art of Computer Programming, Volume 1: Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.2.1: Stacks, Queues, and Deques, pp. 238–243.
  • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 10.1: Stacks and queues, pp.200–204.

[sửa] Liên kết ngoài

Our "Network":

Project Gutenberg
https://gutenberg.classicistranieri.com

Encyclopaedia Britannica 1911
https://encyclopaediabritannica.classicistranieri.com

Librivox Audiobooks
https://librivox.classicistranieri.com

Linux Distributions
https://old.classicistranieri.com

Magnatune (MP3 Music)
https://magnatune.classicistranieri.com

Static Wikipedia (June 2008)
https://wikipedia.classicistranieri.com

Static Wikipedia (March 2008)
https://wikipedia2007.classicistranieri.com/mar2008/

Static Wikipedia (2007)
https://wikipedia2007.classicistranieri.com

Static Wikipedia (2006)
https://wikipedia2006.classicistranieri.com

Liber Liber
https://liberliber.classicistranieri.com

ZIM Files for Kiwix
https://zim.classicistranieri.com


Other Websites:

Bach - Goldberg Variations
https://www.goldbergvariations.org

Lazarillo de Tormes
https://www.lazarillodetormes.org

Madame Bovary
https://www.madamebovary.org

Il Fu Mattia Pascal
https://www.mattiapascal.it

The Voice in the Desert
https://www.thevoiceinthedesert.org

Confessione d'un amore fascista
https://www.amorefascista.it

Malinverno
https://www.malinverno.org

Debito formativo
https://www.debitoformativo.it

Adina Spire
https://www.adinaspire.com