u3.stack queue

Download U3.stack queue

Post on 15-Jan-2015




1 download

Embed Size (px)


U3.stack queue


  • 1. Stacks

2. What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element inserted is the first one to be removed Example Which is the first element to pick up? 3. Last In First Outtoptop ED D DC top C C CB top B B B BA A A A AA top 4. Stack Applications Real life Pile of books, files, plates TOH More applications related to computer science stack Program execution Evaluating expressions Palindrome finder Parentheses matcher A Palindrome is a string that reads the same in either direction Examples: Able was I ere I saw Elba 5. Stackobjects: a finite ordered list with zero or more elements.methods: Stack createS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean isFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size)return TRUE else return FALSE Stack push(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return 6. Stack (contd)Boolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size))return TRUEelse return FALSEElement pop(stack) ::= if(IsEmpty(stack)) returnelse remove and return the item on thetop of the stack. 7. Array-based Stack Implementation Allocate an array of some size (pre-defined) Maximum N elements in stack Bottom stack element stored at element 0 last index in the array is the top Increment top when one element is pushed,decrement after pop 8. Stack Implementation #include #include # define MAXSIZE 200 int stack[MAXSIZE]; int top; //index pointing to the top of stack void main() { void push(int); int pop(); int will=1,i,num; clrscr(); while(will ==1) { printf(" MAIN MENU:n 1.Add element to stackn2.Delete element from the stack"); scanf("%d",&will);8 Chapter 5: Stacks 9. switch(will) { case 1: printf("Enter the data... "); scanf("%d",&num); push(num); break; case 2: i=pop(); printf("Value returned from pop function is %d ",i); break; default: printf("Invalid Choice . "); } printf(" Do you want to do more operations on Stack ( 1 for yes, anyother key to exit) "); scanf("%d" , &will); } //end of outer while }//end of main9Chapter 5: Stacks 10. void push(int y){if(top>MAXSIZE) { printf("nSTACK FULL"); return; }else { top++; stack[top]=y; }}10Chapter 5: Stacks 11. int pop(){int a;if(top 3 4 * 5 6 * + 27. Queue27 28. The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ FrontRear 29. The Queue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $FrontRear 30. The Queue Operations When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Front Rear 31. The Queue Class The C++template standard class queuetemplate library {has a queuepublic:template class. queue( );void push(const Item& The template entry);parameter is thevoid pop( );type of the items bool empty( ) const;that can be put inItem front( ) const;the queue. 32. Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear).[0] [1] [2] [3][4][5]... 486An array ofintegers toimplement a We dont care whats inqueue of integersthis part of the array. 33. Array Implementation The easiest implementation also 3 size keeps track of the number of items in the queue and the index of the firstfirst 0 element (at the front of the queue), the last element (at the rear). 2 last[0][1] [2] [3] [4] [5] ... 48 6 34. A Dequeue Operation When an element leaves the queue, 2 size size is decremented, and first changes, too. first 1 2 last[0] [1] [2] [3] [4][5] ... 486 35. An Enqueue Operation When an element enters the queue, 3 size size is incremented, and last changes, too. first 1 3 last[0] [1] [2] [3] [4][5] ... 8 6 2 36. At the End of the Array There is special behavior at the end 3 size of the array. For example, suppose we want to add a new element to this first3 queue, where the last index is [5]:5 last[0][1] [2] [3] [4] [5]2 6 1 37. At the End of the Array The new element goes at the front of4 size the array (if that spot isnt already used):first 3 0 last[0][1] [2] [3] [4] [5] 426 1 38. Array Implementation Easy to implement3 size But it has a limited capacity with a fixed array Or you must use a dynamic array for an 0 first unbounded capacity Special behavior is needed when the rear2 last reaches the end of the array. [0] [1] [2][3] [4][5]...4 86 39. Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail13 pointer.1510 7nullhead_ptr tail_ptr 40. Linked List Implementation Which end do you think is the front of the queue? Why? 13 15 107 null head_ptrtail_ptr 41. Linked List Implementation The head_ptr points to the front of the list. Because it is harder to removeFront13 items from the tail of the list.1510 7nullhead_ptr tail_ptr Rear 42. A priority queue is a container in which access or deletion is of the highest- priority item, according to some way of Assigning priorities to items.42Priority Queues 43. FOR EXAMPLE, SUPPOSE A HOSPITALEMERGENCY ROOM HAS THEFOLLOWING FOUR PATIENTS, WITHNAME, PRIORITY, AND INJURY:43Priority Queues 44. Matt 20 sprained ankleAndrew 45 broken legSamira 20 high blood pressureKerem 83 heart attackIN WHAT ORDER SHOULD THEPATIENTS BE TREATED? 44Priority Queues 45. THERE ARE MANY APPLICATIONSOF PRIORITY QUEUES, IN AREAS ASDIVERSE AS SCHEDULING, DATACOMPRESSION, AND JPEG (JointPhotographic Experts Group) ENCODING.45 Priority Queues 46. Using Priority Queue toTrack Your Assignments Organize class or work assignments by due dates Early due date, higher priority diagram of class Assignment