stacks and queues concepts

Download STACKS AND QUEUES CONCEPTS

Post on 25-May-2015

260 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • 1. 1Chapter 2Stacks and QueuesM V B REDDY

2. 2Stack ADT Recall that ADT is abstract data type, a setof data and a set of operations that actupon the data. In a stack, the set of data is the stack ofelements. Stack is known as a LIFO (last-in-first-out)data structure because the last data toenter the stack is the first to exit the stack. 3. 3Stack ADT Operations push: places an element onto the top of astack. pop: removes an element from the top of thestack. peek: which retrieves (copies) a value from thetop of the stack without removing it. an operation to determine whether or not thestack is empty. an operation to empty out a stack. 4. 4Push Push means place a new data element atthe top of the stack115173stack 5. 5Push (cont.) Push means place a new data element atthe top of the stack115173stack 6. 6Push (cont.) Push means place a new data element atthe top of the stack311517stack 7. Pop means take a data element off the topof the stack7Pop311517stack 8. Pop means take a data element off the topof the stack8Pop (cont.)115173stack 9. Pop means take a data element off the topof the stack9Pop (cont.)115173stack 10. 10Peek Peek means retrieve the top of the stackwithout removing it3115173stack 11. 11Array Stack Class Template3 template 4 class Stack {5 public:6 Stack() { }7 ~Stack( ) { }8 void push( T& elementToPush ) { }9 bool pop( T& poppedElement ) { }10 bool peek( T& topElement ) { }11 bool isEmpty( ) const { }12 void makeEmpty( ) { }13 private:14 T* elements; // dynamic array15 int size;16 int top;17 };used as anindex to the topof the stack 12. 12Array StackConstructor and Destructor3 Stack () : top(-1), size(0)4 {5 elements = NULL;6 }78 ~Stack( )9 {10 makeEmpty( );11 } 13. 13Array StackisEmpty and makeEmpty65 bool isEmpty( ) const66 {67 return top == -1;68 }6970 void makeEmpty( )71 {72 size = 0;73 top = -1;74 delete [] elements;75 } 14. 14Array Stack Popelements125 25 200 700 1 2 3topAn element cant really be removed from anarray, as one would think pop would achieve. 15. 15Array Stack Pop(cont.)elements125 25 200 700 1 2 3topclientThe element 70 is at the top of the stack, andwhat really happens during a pop, is that 70 isreturned to the client 16. 16Array Stack Pop(cont.)elements125 25 200 700 1 2 3topand top is decrementedclient 17. 17Array Stack Pop(cont.)elements125 25 200 700 1 2 3topclientThe element 70 is still in the array, but it is nolonger accessible. The next push willoverwrite it. Say, we would like to push 63 18. 18Array Stack Pushelements125 25 200 700 1 2 3topFirst, top is incremented 19. 19Array Stack Push(cont.)elements125 25 200 630 1 2 3topThen, 63 is pushed into that position 20. 20Is the Array Stack Full/Empty? An array stack is full when top == size - 1 An array stack is empty when top == -1 21. 21Linked-List Stack Stacks can also be implemented with alinked list. The front node is the top of the stack. 22. BobAli22Linked-List Stack(cont.)topTo pop, we remove the node at the front of thelinked list, and return the element to the clientAlitop 23. 23Linked-List Stack(cont.)topTo push Cat, we place the new element in a nodeand insert it at the front of the linked listCattopAli Ali 24. 24Linked-List Stack Class Template13 template 14 class Stack {15 public:16 Stack( ) { }17 ~Stack( ) { }18 void push( const T & element ) { }19 bool pop( T & element ) { }20 bool peek( T & element ) { }21 bool isEmpty( ) const { }22 void makeEmpty( ) { }23 private:24 Node *top;25 }; 25. 25Linked-List StackConstructor and Destructor8 Stack( ) : top(NULL) { }78 ~Stack( )9 {10 makeEmpty( );11 } 26. 26Linked-List Push32 void push( const T& element ) {33 Node *newNode = new Node;34 newNode->info = element;35 if (top == NULL) {36 newNode->next = NULL;37 top = newNode;38 }39 else {40 newNode->next = top;41 top = newNode;42 }newNode43 } 27. Linked-List Push into Empty Stack2732 void push( const T& element ) {33 Node *newNode = new Node;34 newNode->info = element;35 if (top == NULL) { // if stack is empty.36 newNode->next = NULL;37 top = newNode;38 }top39 else {40 newNode->next = top;41 top = newNode;42 }43 }newNodeNULL 28. Linked-List Push into Empty Stack28(cont.)32 void push( const T& element ) {33 Node *newNode = new Node;34 newNode->info = element;35 if (top == NULL) { // if stack is empty.36 newNode->next = NULL;37 top = newNode;38 }39 else {40 newNode->next = top;41 top = newNode;42 }43 }topnewNode 29. 29Linked-List Push into Non-EmptyStack32 void push( const T& element ) {33 Node *newNode = new Node;34 newNode->info = element;35 if (top == NULL) { // if stack is empty.36 newNode->next = NULL;37 top = newNode;38 }39 else { // stack is not empty.40 newNode->next = top;41 top = newNode;42 }43 } newNodetop 30. 30Linked-List Push into Non-EmptyStack (cont.)32 void push( const T& element ) {33 Node *newNode = new Node;34 newNode->info = element;35 if (top == NULL) { // if stack is empty.36 newNode->next = NULL;37 top = newNode;38 }39 else { // stack is not empty.40 newNode->next = top;41 top = newNode;42 }newNode43 }top 31. 31Linked-List Peek56 bool peek( T& element )57 {58 if ( top == NULL )59 return false;60 element = top->info;61 return true;62 } 32. 32Linked-List Pop56 bool pop( T& element )57 {58 if ( top == NULL )59 return false;60 element = top->info;61 Node *ptr = top;62 top = top->next;63 delete ptr;64 return true;65 } 33. 33Linked-List StackisEmpty and makeEmpty65 bool isEmpty( ) const66 {67 return top == NULL;68 }6970 void makeEmpty( )71 {72 T temp;73 while ( pop( temp ) );74 } 34. 34The Queue ADT The queue is a data structure that is like aline of people When people join the line, they go at the end When people are served, they come off thefront of the line Queue is known as a FIFO (last-in, last-out)data structure because the last datato enter the queue is the last to exit fromthe queue. 35. 35Queue ADT Operations enqueue: add an element to the end ofthe line dequeue: take an element from the frontof the line peek: retrieve (copy) the element at thefront of the line without removing it an operation to determine whether or notthe queue is empty an operation that will empty out the queue 36. 36Queue (cont.) In addition to a pointer at the beginning ofthe linked list (called front), a pointer tothe end of the linked list (called back) isalso maintained in the private section The back pointer makes it fast to add newelements to the end of the queue youdont have to use a loop to go all the waythrough the queue to find the last node 37. 37Linked-List Dequeuefront backBobAli 38. 38Linked-List Dequeue(cont.)front backAli 39. 39Linked-List Enqueuefront back 40. 40Linked-List Enqueue(cont.)front back 41. Linked-List Queue Class Template13 template 14 class Queue {15 public:16 Queue( );17 ~Queue( );18 void enqueue( const T & element );19 bool dequeue( T & deqElement );20 bool peek( T & frontElement );21 bool isEmpty( ) const;22 void makeEmpty( );23 private:24 Node *front;25 Node *back;26 };41 42. 42Linked-List QueueConstructor and Destructor4 Queue( )5 {6 front = back = NULL;7 }89 ~Queue( )10 {11 makeEmpty( );12 } 43. 43Linked-List Queue Enqueue21 void enqueue( const T & element )22 {23 Node *newNode = new Node;24 newNode->info = element;25 newNode->next = NULL;28 if (front == NULL) { // list is empty.29 front = back = newNode;30 }31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }newNode 44. Linked-List Queue Enqueue (cont.)4422 void enqueue( const T & element )23 {24 Node *newNode = new Node;25 newNode->info = element;26 newNode->next = NULL;27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }newNodeCase 1: The queueis initially empty.frontback 45. Linked-List Queue Enqueue (cont.)4522 void enqueue( const T & element )23 {24 Node *newNode = new Node;25 newNode->info = element;26 newNode->next = NULL;27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }newNodeCase 2: The queuehas nodes.frontback 46. Linked-List Queue Enqueue (cont.)4622 void enqueue( const T & element )23 {24 Node *newNode = new Node;25 newNode->info = element;26 newNode->next = NULL;27 if (front == NULL) { // list is empty.28 front = newNode;29 back = front;30 }31 else { // list is not empty.32 back->next = newNode;33 back = newNode;34 }35 }newNodeCase 2: The queuehas nodes.frontback 47. Returns false if client triesto dequeue an emptyqueue.47Linked-List Queue Dequeue41 bool dequeue( T & deqElement )42 {43 if ( front == NULL)44 return false;Dequeue continued 48. 48Linked-List QueueDequeue (cont.)45 deqElement = front->info;46 Node *ptr = front;47 front = front->next;48 delete ptr;49 return true;50 }deqElement:passed in byreferencefront ptr back 49. 49Linked-List QueueDequeue (cont.)45 deqElement = front->info;46 Node *ptr = front;47 front = front->next;48 delete ptr;49 return true;50 }deqElement:ptr front back 50. 50Linked-List QueueDequeue (cont.)45 deqElement = front->info;46 Node *ptr = front;47 front = front->next;48 delete ptr;49 return true;50 }deqElement:ptr front back 51. 51Linked-List Queue Peek56 bool peek( T & frontElement )57 {58 if ( front == NULL)59 return false;60 frontElement = front->info;61 return true;62 } 52. 52Linked-List QueueisEmpty and makeEmpty65 bool isEmpty( ) const66 {67 return front == NULL;68 }6970 void makeEmpty( )71 {72 T temp;73 while ( dequeue( temp ) );74 } 53. 53Array Queue Similar to the linked-list queue, there are 2attributes called front and back, but theyare indexes into