Dale - Computer Science Illuminated 326

# Dale - Computer Science Illuminated 326 - be in reverse...

This preview shows page 1. Sign up to view the full content.

9.6 Stacks and Queues 299 The mental image of the cafeteria plates has left an imprint on the tradi- tional names used for the insert and delete operations. The insert is called Push and the delete is called Pop . We Push and item onto the stack and Pop an item off the stack. A stack does not have the property of length, so there is no operation that returns the number of items on the stack. We do need operations that determine whether a stack is Empty because trying to Pop an item when the stack is empty is an error. Let’s look at a case in which we use a stack. When we were designing the insertion algorithm for the sorted list, we said that putting the item into the last slot was efficient for the array-based implementation but not for the linked implementation. The obvious place to put each new item in the linked implementation is at the beginning of the list, but the list would
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: be in reverse order. Let’s assume that the insert puts the item at the begin-ning of the list and uses a stack to print a list in reverse order. That is, if the list is in reverse order and we print it in reverse order, the list should be ordered appropriately. Right? Let’s try it. If items 90, 65, 80, 95, 75, 60 are entered into a linked implementation of an unordered list with each item going at the front of the list, the list would look like the one in Figure 9.5. The first item is accessed and put onto the stack, the second item is accessed and put on the stack, and so on. When the last item has been added to the stack, the contents of the stack look like this: Top of stack Bottom of the stack 60 75 95 80 65 90 Print list While ( more items ) Get an item Push item While ( NOT IsEmpty ) Pop item Print item...
View Full Document

## This note was uploaded on 01/13/2011 for the course CSE 1550 taught by Professor Marianakant during the Fall '10 term at York University.

Ask a homework question - tutors are online