23-stacks-queues-deques

23-stacks-queues-deques - Stacks Queues and Deques Stacks...

This preview shows pages 1–7. Sign up to view the full content.

Stacks, Queues, and Deques

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Stacks, Queues, and Deques A stack is a last in, first out ( LIFO ) data structure Items are removed from a stack in the reverse order from the way they were inserted A queue is a first in, first out ( FIFO ) data structure Items are removed from a queue in the same order as they were inserted A deque is a double-ended queue—items can be inserted and removed at either end
3 Array implementation of stacks To implement a stack, items are inserted and removed at the same end (called the top ) Efficient array implementation requires that the top of the stack be towards the center of the array, not fixed at one end To use an array to implement a stack, you need both the array itself and an integer The integer tells you either: Which location is currently the top of the stack, or How many elements are in the stack

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Pushing and popping If the bottom of the stack is at location 0 , then an empty stack is represented by top = -1 or count = 0 To add ( push ) an element, either: Increment top and store the element in   stk[top] , or Store the element in   stk[count]   and increment   count To remove ( pop ) an element, either: Get the element from   stk[top]   and decrement   top , or Decrement count and get the element in   stk[count] top = 3 or count = 4 0      1      2      3      4      5      6     7      8      9 17 23 97 44 stk:
5 After popping When you pop an element, do you just leave the “deleted” element sitting in the array? The surprising answer is, “it depends” If this is an array of primitives, or if you are programming in C or C++, then doing anything more is just a waste of time If you are programming in Java, and the array contains objects, you should set the “deleted” array element to null Why? To allow it to be garbage collected! top = 2 or count = 3 0      1      2      3      4      5      6     7      8      9 17 23 97 44 stk:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Sharing space Of course, the bottom of the stack could be at the other end top = 6 or count = 4 17 23 97 44 0      1      2      3      4      5      6     7      8      9 stk: Sometimes this is done to allow two stacks to share the same storage area topStk2 = 6 17 23 97 44 49 57 3 0      1      2      3      4      5      6     7      8      9 stks: topStk1 = 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/07/2010 for the course ENGENEER 1220 taught by Professor Sad during the Spring '09 term at Islamic University.

Page1 / 28

23-stacks-queues-deques - Stacks Queues and Deques Stacks...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online