Stacks and Queues

# Stacks and Queues - Stacks and Queues Page 1 of 15 STACKS...

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

S TACKS AND Q UEUES Contents z Introduction { Stacks { Queues z Implementing Stacks { Array Implementation ± Test Yourself #1 ± Test Yourself #2 ± Test Yourself #3 { Linked - list Implementation ± Test Yourself #4 ± Test Yourself #5 z Implementing Queues { Array Implementation { Linked - list Implementation z Comparison of Array and Linked - List Implementations z Applications of Stacks and Queues z Test Yourself #6 Introduction Both stacks and queues are like lists (ordered collections of items), but with more restricted operations. They can both be implemented either using an array or using a linked list to hold the actual items. Stacks The conceptual picture of a stack is something like this: Think of a stack of newspapers, or trays in a cafeteria. The only item that can be taken out (or even seen) is the most recently added item; a stack is a Last-In-First-Out (LIFO) data structure. Here are the operations for a stack of objects of type E: _______ _________ / \ / \ values in \/ / \/ | ----- | values out | | | ----- | | | | ----- | | | | | ---------- Page 1 of 15 Stacks and Queues 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Stacks-and-Queues/

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

View Full Document
Queues The conceptual picture of a queue is something like this: Think of people standing in line. A queue is a First-In-First-Out (FIFO) data structure. Items can only be added at the rear of the queue, and the only item that can be removed is the one at the front of the queue. Here are the queue operations: Implementing Stacks The stack ADT is very similar to the list ADT; therefore, their implementations are also quite similar. Array Implementation Below is the definition of the Stack class, using an array to store the items in the stack; note that we include a static final variable INITSIZE, to be used by the Stack constructor as the initial size of the array (the same thing was done for the List class). OPERATION DESCRIPTION Stack<E>() (constructor) create an empty stack boolean empty () return true iff the stack is empty int size() return the number of items in the stack void push(E ob) add ob to the top of the stack E pop() remove and return the item from the top of the stack (error if the stack is empty) E peek() return the item that is on the top of the stack, but do not remove it (error if the stack is empty) ------------------ values in ----> items in the queue ----> values out ------------------ ^ ^ | | this is the rear of this is the front of the queue the queue OPERATION DESCRIPTION Queue<E>() (constructor) create an empty queue boolean empty() return true iff the queue is empty int size() return the number of items in the queue void enqueue(E ob) add ob to the rear of the queue E dequeue() remove and return the item from the front of the queue (error if the queue is empty) Page 2 of 15 Stacks and Queues 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Stacks-and-Queues/
TEST YOURSELF #1 Write the Stack constructor. solution

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/27/2008 for the course CS 367 taught by Professor Marvinsolomon during the Spring '08 term at University of Wisconsin.

### Page1 / 15

Stacks and Queues - Stacks and Queues Page 1 of 15 STACKS...

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

View Full Document
Ask a homework question - tutors are online