Stack and Queue Author Srinadh Gunnam Stack Introduction

  • Slides: 17
Download presentation
Stack and Queue Author : Srinadh Gunnam

Stack and Queue Author : Srinadh Gunnam

Stack -- Introduction Last In First Out Data structure. The element that can be

Stack -- Introduction Last In First Out Data structure. The element that can be removed is the one that is most recently inserted. Only top most element is accessible. Stack pointer always points to the top element that can be removed. Ex : Plates arranged on top of one another.

Stack – Operations Push : - Inserts the given item at the top of

Stack – Operations Push : - Inserts the given item at the top of the stack Pop : - Removes the top most element. Over flow : Error, when tried to push into an fully filled stack Under flow : Error, when tried to pop from an empty stack

Queue -- Introduction First In First Out Data structure. The elements are removed in

Queue -- Introduction First In First Out Data structure. The elements are removed in the order of their insertion. Head always points to the front of the queue, from where elements can be removed. Tail always points to the end of the queue , where a new element can be inserted. Ex : typical queues in reservation centres and cinema halls .

Queue – Operations Enqueue : - inserts the given item at the location pointed

Queue – Operations Enqueue : - inserts the given item at the location pointed by tail Dequeue : - removes the element, which is pointed by head. Over flow : error, when tried to Enqueue into an fully filled queue (only in case of limited capacity queues) Under flow : error, when tried to Dequeue from an empty queue.

Animation Details The animations of stack and queue are shown side by side. User

Animation Details The animations of stack and queue are shown side by side. User can select the operations he is interested in, by using the respective buttons. The capacity of stack and queue is taken to be limited in the present example. The user can also be given the option to enter the capacity of the stack or queue. (to provide more interactivity. )

Animation Initially the animation will be as shown in the next slide , with

Animation Initially the animation will be as shown in the next slide , with empty stack and queue (the capacity is taken to be 5). Stack pointer S points to bottom of the first element. In queue, Head H points to bottom of first element (as there are no elements to be removed) Tail T points to first element where the given item can be inserted.

Animation Initially the scenario as shown bellow.

Animation Initially the scenario as shown bellow.

Animation • User can insert the elements using the Push button in case of

Animation • User can insert the elements using the Push button in case of stack and Enqueue button in case of queue. He can specify the value in the text box provided. • A sample example when elements 1, 2 and 3 are inserted in the order is shown in the next slide. • Here the stack pointer moves with the top most element location. • Tail T always points to next free location in queue. • Head H points to the current first element or to the starting location of the queue. (Generally it wont change till all the elements are dequeued).

Animation After insertion

Animation After insertion

Animation User can also remove the elements by using button Pop in case of

Animation User can also remove the elements by using button Pop in case of stack and Dequeue in case of queue. A sample example when one element is removed from the last scenario is shown in next slide. In case of stack, the element at the location pointed by the stack pointer is removed and pointer is updated to the next top element. In case of queue, the element at the location pointed by Head is removed and all the elements are moved by one position towards head. Tail will be updated to next free location.

Animation

Animation

Boundary conditions When user tries to insert a new element in a fully filled

Boundary conditions When user tries to insert a new element in a fully filled stack or queue, “STCK OVER-FLOW “ or “QUEUE FULL “ should be displayed without insertion. When the last element is also deleted i. e. stack or queue becomes empty, stack pointer should point to bottom of first element. Head to bottom of first element and tail to first location. When either is empty and user tries to remove an element , the error message “STACK UNDER-FLOW “ or “QUEUE EMPTY” should be displayed.

Interactivity The user can observe the operations of stack and queue by using the

Interactivity The user can observe the operations of stack and queue by using the respective buttons. Initially the user can be asked to enter the size of the stack or queue and then they can be created accordingly with the specified size. All the concepts (Introduction and operations) can be shown on one side of animation for user reference.

Questions If the characters 'D', 'C', 'B', 'A' are placed in a stack (in

Questions If the characters 'D', 'C', 'B', 'A' are placed in a stack (in that order), and then removed one at a time, in what order will they be removed? ( ABCD) 2. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? ( DCBA) 3. One example of stack operation : CDs in a case 4. One example of queue operation : Luggage checking in airports etc. 1.

Useful links http: //www. cmpe. boun. edu. tr/~akin/cmpe 223/chap 2. htm http: //www 2.

Useful links http: //www. cmpe. boun. edu. tr/~akin/cmpe 223/chap 2. htm http: //www 2. roguewave. com/support/docs/hppdocs/ stdug/10. html www. wikipedia. com

 Thank You Any suggestions ?

Thank You Any suggestions ?