Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.

Similar presentations


Presentation on theme: "Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007."— Presentation transcript:

1 Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007

2 Objective In this chapter we will learn: Stacks Queues Different implementations (arrays and linked list) of both Comparison of implementation

3 Stack A stack is a data structure that works on the principle of Last In First Out (LIFO). So last item put on the stack is the first item that can be taken off, like a physical stack of books/plates. In stack new elements are added to and removed from the top of the structure.

4 Two Implementations of Stack As Vector Storing the items contiguously. As List Storing items noncontiguously.

5 Stack Can be implemented with an array and an integer that indicates the top element of the stack (tos). Empty stack : tos = -1. Basic Operations : PUSH and POP.

6 How stack works a b a a tos= -1 tos=0 tos=1 pop(b)push(a)push(b)Empty stack tos=0

7 Stack – Vector Implementation template class Stack{ { public: Stack( ); bool isEmpty( ) const; const Object & top( ) const; void makeEmpty( ); void pop( ); void push( const Object & x ); Object topAndPop( ); private: vector theArray; int topOfStack; }; A stack can be implemented with an vector and an integer that indicates the index of the top element. //construct the stack Template Stack ::Stack():the Array(1){ topOfStack = -1; } //test if the stack is logically empty Template Stack ::isEmpty() const{ return topOfStack == -1; }

8 The push/pop function (vector-based) template void Stack ::push( const Object & x ) { if( topOfStack == theArray.size( ) - 1 ) theArray.resize( theArray.size( ) * 2 + 1 ); theArray[ ++topOfStack ] = x; } // If there is no vector doubling, push takes constant time, otherwise it // takes O(N) time. But it does not happen often. template void Stack ::pop( const Object & x ) { if( isEmpty()) throw UnderflowException(); topOfStack--; }

9 Linked List Implementation Advantage of the linked list : using only one pointer per item at a time. Disadvantage of contiguous vector implementation : using excess space equal to the number of vacant array items.

10 Linked List Implementation of Stack The stack class can be implemented as a linked list in which the top of the stack is represented by the first item in the list. topOfStack

11 Linked List Implementation of Stack Each stack item stores element value pointer to next element

12 Application of Stack Recognizing palindromes Checking balanced expressions Evaluating algebraic expressions is easier. Searching networks, traversing trees (keeping a track where we are).

13 Infix to Postfix Conversion Scan the Infix string from left to right. Initialise an empty stack. If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character. Repeat this step till all the characters are scanned. (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty. Return the Postfix string.

14 Example : String a+b*c-d STACK PostfixString

15 Postfix Expression Evaluation for each character C in a given string { if C is an operand push C onto stack; else // C is an operator { pop item from stack, and store in Opr2; pop item from stack, and store in Opr1; result = Opr1 C Opr2, using C as an operator; push result onto stack; }

16 QUEUE A queue is an abstract data struture where various entities such as data, objects, persons, or events are stored and waiting to be processed. The most well known operation of the queue is the First-In-First-Out (FIFO) queue process. In a FIFO queue, the first element in the queue will be the first one out

17 Queue Can be implemented using Vector/Array Link List Two main Operations : Enqueue Dequeue

18 Queue: Vector/Array Implementation Store items in an vector/array with front item at index zero and back item at index Back. Enqueue is easy: increment Back. Dequeue is inefficient: all elements have to be shifted. (If use only one index) Result: Dequeue will be O (N ).

19 Queue Back a ab b Step 1. makeEmpty() Step 2. enqueue() Step 3. enqueue() b Back After dequeue() : Step 4. dequeue()

20 Better Idea Keep a Front index. To Dequeue, increment Front. Therefore, Dequeue takes constant time now. abcd FrontBack abcd FrontBack

21 Queue Front Back a Front Back ab Front Back b Front Back Step 1. makeEmpty() Step 2. enqueue() Step 3. enqueue() b Front Back After dequeue() : Step 4. dequeue()

22 Circular Implementation Previous implementation is O( 1 ) per operation. However, after vector.size() times enqueues, we are full, even if queue is logically nearly empty for dequeue opeartions on the elements. Solution: use wraparound to reuse the cells at the start of the vector. To increment, add one, but if that goes past end, reset to zero.

23 Problem with the approach : c d Front Back Front e c d Back Solution : Recycle / Wrap Around

24 Circular Example Both Front and Back wraparound as needed. bcd FrontBack bcd FrontBack ef efg

25 QUEUE--Vector Implementation Mostly straightforward; maintain Front Back CurrentSize: Current number of items in queue Only tricky part is vector doubling because the queue items are not necessarily stored in an array starting at location 0, and the contiguity of wraparound must be maintained.

26 Queue – Vector Implementation Template Class Queue{ public: Queue(); bool isEmpty() const; const Object & getFront() const; void makeEmpty(); Object dequeue(); void enqueue (const Ojbect & x); private: vector theArray; int currentSize; int front; int back; void increment (int & x) const; void doubleQueue(); } template void Queue ::enqueue(const Object & x){ if(currentSize == theArray.size()) doubleQueue(); increment( back); theArray[back] = x; currentSize++; } template void Queue ::doubleQueue() { theArray.resize(theArray.size() * 2 + 1); if(front != 0){ for(int i=0; i<front; i++){ theArray[i+currentSize] = theArray[i]; back += currentSize; }

27 template const Object & Queue ::getFront() const { if (isEmpty()) throw UnderflowException(); return theArray[front]; } template Object Queue ::dequeue() { if( isEmpty()) throw UnderflowException(); currentSize--; Object frontItem = theArray[front]; increment(front); return frontItem; } template void Queue ::makeEmpty(){ currentSize = 0; front = 0; back = theArray.size() –1; } Queue – Vector Implementation cont.

28 Linked List Implementation Advantage of the linked list is excess memory is only one pointer per item. In contrast, a contiguous vector implementation uses excess space.

29 Queue Same idea as like STACK, but has front and back back front

30 Comparison of the Two Methods Both of them run in constant time per operation. The vector version is likely to be faster. But it has two drawbacks: The wraparound is a little confusing; It might waste more space.

31 Deque/Double-Ended Queue A deque is a double-ended queue. A deque is a little modification on the queue data structure, where access is given to both the ends. Operations : addFront, addRear, removeFront, removeRear. That is, a deque is especially optimized for pushing and popping elements at the beginning and end. As with vectors, storage management is handled automatically.

32 Common errors (Page 561) Do not delete the top node directly before adjusting the top of the stack pointer Be aware of memory leaks Access is constant time in both of these implementations.


Download ppt "Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007."

Similar presentations


Ads by Google