1 / 17

Stacks and Queues

Stacks and Queues. Stacks. A stack is an abstract data structure, with some special properties: Insertion and deletion is only allowed at the end of the stack (usually called the top )

nay
Download Presentation

Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues

  2. Stacks • A stack is an abstract data structure, with some special properties: • Insertion and deletion is only allowed at the end of the stack (usually called the top) • Due to this restriction, elements are removed from the stack in the opposite order in which they were added • This is called LIFO order (Last In, First Out) RHS – SOC

  3. Stacks public interface Stack<T> { void push(T element); T pop(); T peek(); } RHS – SOC

  4. Stacks push(D) D D = pop() push(C) C C = pop() push(B) B B = pop() A push(A) A = pop() RHS – SOC

  5. Stacks • Note that we have not stated exactly how a Stack is implemented • Can be done in various ways (array, linked list, etc.) • However, data structure should support efficient (O(1)) insertion and removal at the end of the collection RHS – SOC

  6. Stacks • What are stacks used for…? • Expression evaluation • Memory management • …any situation where LIFO order is the proper order for processing data RHS – SOC

  7. Stacks RHS – SOC

  8. Stacks • The (in)famous HP15-C pocket calculator used Reverse Polish Notation for entering and evaluating expressions • This notation is very closely related to stakcs • Loved by many, hated by more… RHS – SOC

  9. 1 2 x 3 7 = 1 2 E 3 7 x E Stacks • 12 x 37 is not typed in as: • but rather as: RHS – SOC

  10. Stacks Push(X) X X = pop() Push(37) 37 37 = pop() 444 12 Push(12) 12 = pop() Push(12 x 37) RHS – SOC

  11. Queues • A queue is (also) an abstract data structure, with some special properties: • Insertion is only allowed at the end of the queue (usually called the tail) • Deletion is only allowed at the start of the queue (usually called the head) • Elements are removed from the stack in the same order in which they were added • This is called FIFO order (First In, First Out) RHS – SOC

  12. Queues public interface Queue<T> { void add(T element); T remove(); T peek(); } RHS – SOC

  13. Queues add(D) D D = remove() add(C) C C = remove() add(B) B B = remove() A add(A) A = remove() RHS – SOC

  14. Queue • Like for a Stack, a Queue can be implemented in various ways • Data structure should support efficient (O(1)) insertion and removal at the start and end of the collection • Linked list likely choice RHS – SOC

  15. Queue • What are queues used for…? • Event handling • All situations where we need to process data in the order it is produced, but cannot process data immediately RHS – SOC

  16. Stacks and Queues in Java • Stack<T> is part of Java library, and can be used as-is • Queue<T> is an interface! • There are several implementations of the Queue interface availabe • See the documentation for details RHS – SOC

  17. Exercises • Review: R15.11, R15.12 • Programming: P15.11, P15.12 RHS – SOC

More Related