Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at.

Similar presentations


Presentation on theme: "Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at."— Presentation transcript:

1 Stacks and Queues

2 DCS – SWC 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)

3 DCS – SWC 3 Stacks public interface Stack { void push(T element); T pop(); T peek(); }

4 DCS – SWC 4 Stacks A B C D push(A) push(B) push(C) push(D) A = pop() B = pop() C = pop() D = pop()

5 DCS – SWC 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

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

7 DCS – SWC 7 Stacks

8 DCS – SWC 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 stacks Loved by many, hated by more…

9 DCS – SWC 9 Stacks 12 x 37 is not typed in as: but rather as: 12x37=12E37x E

10 DCS – SWC 10 Stacks 12 37 X Push(12) Push(37) Push(X) 12 = pop() 37 = pop() X = pop() Push(12 x 37) 444

11 DCS – SWC 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)

12 DCS – SWC 12 Queues public interface Queue { void add(T element); T remove(); T peek(); }

13 DCS – SWC 13 Queues A B C D add(A) add(B) add(C) add(D) A = remove() B = remove() C = remove() D = remove()

14 DCS – SWC 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

15 DCS – SWC 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

16 DCS – SWC 16 Stacks and Queues in Java Stack is part of Java library, and can be used as-is Queue is an interface! There are several implementations of the Queue interface availabe See the documentation for details


Download ppt "Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at."

Similar presentations


Ads by Google