Note 3: Stack and Queue Concept in Data Structure for Application Stack: It is a sequence of items that are accessible at only one end of the sequence. Think of a stack as a collection of items that are piled one on top of the other, with access limited to the topmost item. A stack inserts item on the top of the stack and removes item from the top of the stack. It has LIFO (last-in / first-out) ordering for the items on the stack. Type of Stack: Linear Stack Linked List Stack Operation of Stack: Add: a push( ) operation adds an item to the topmost location on the stack. stack: stack: stack: top = 3 3 2 1 top = 2 add 4 to the stack 4 top = 3 3 3 2 2 1 1 increase top push 4 to the stack Push function: void push ( short stack[], short stack_size, short top, short item) { if ( top >= stack_size –1) { cout << “The stack is full!” << endl; return; } stack[+ + top] = item; } Data Structures in C++ Note 3 15 Delete: a pop( ) operation removes an item from the topmost location on the stack stack: stack: 4 3 stack: top = 3 top = 3 3 3 2 2 2 1 1 1 delete 4 from the stack pop 4 out of the stack top = 2 decrease top Pop function: short pop (short stack[], short stack_size, short top) { if ( top = = –1) { cout << “The stack is empty!” << endl; return 0; } return stack[top – – ] ; } Queue: A queue is a sequential storage structure that permits access only at the two ends of the sequence. We refer to the ends of the sequence as the front and rear. A queue inserts new elements at the rear and removes elements from the front of the sequence. You will note that a queue removes elements in the same order in which they were stored, and hence a queue provides FIFO (first-in / first-out), or FCFS (first-come / first-served), ordering. Type of the Queue: Linear Queue: non-circular queue, circular queue, priority queue Linked List Queue: non-circular queue, circular queue, priority queue Operation of Queue: Add: insert operation for the queue is adding item to the new element at the rear of queue. Delete: remove operation for the queue is deleting item from the front element of queue. Data Structures in C++ Note 3 16 The non-circular queue with 8 elements: Algorithms: Variables: short qfront = -1, qrear = -1, qsize = 8; Insert: T qrear = qsize-1 and qfront = -1 F qrear = qsize-1 T queue is full return 1 F shift all elements left so that 1st element is at 0 qfront = -1 qrear++ queue[qrear] = value return 0 Delete: qfront = qrear T queue is empty qfront = -1 qrear = -1 return 1 F qfront++ value = queue[qfront] return 0 Graphical Presentation: 0 1 2 3 4 qfront The circular queue with 8 elements: Algorithms: Variables: short qfront = 0, qcount = 0, qsize = 8; 5 qrear 6 7 Data Structures in C++ Note 3 17 Insert: qcount = qsize F T qrear = (qfront+qcount)% qsize queue[qrear] = value qcount++ return 0 queue is full return 1 Delete: qcount = 0 F T queue is empty return 1 value = queue[qfront] qfront = (qfront+1)% qsize qcount - return 0 Graphical Presentation: 4 3 2 5 1 6 qrear 0 7 qfront Structure Type Stacks Queue Stacks and Queue Structure Table Array Link List Linear Stacks Non-Circular Queue Circular Queue Priority Queue Linear Stacks Non-Circular Queue Circular Queue Priority Queue Link List Array Linear Stacks Non-Circular Queue Circular Queue Priority Queue Data Structures in C++ Note 3 18 Multiple Stacks and Queues: Multiple Stacks: Following pictures are two ways to do two stacks in array: 1. None fixed size of the stacks: Graphical Picture: without fixed size of stack Array_ptr -2 -1 12 0 0 1 2 3 4 5 6 7 8 9 10 11 Stack 2 Stack 1 Top Stack 2 Top Stack 1 12 Stack 1 expands from the 0th element to the right Stack 2 expands from the 12th element to the left As long as the value of Top1 and Top2 are not next to each other, it has free elements for input the data in the array When both Stacks are full, Top1 and Top 2 will be next to each other There is no fixed boundary between Stack 1 and Stack 2 Elements –1 and –2 are using to store the information needed to manipulate the stack (subscript for Top 1 and Top 2) 2. Fixed size of the stacks: Graphical Picture: with fixed size of stack Array_ptr -4 -3 -2 -1 5 6 6 0 0 2 3 4 5 6 7 8 9 10 Stack 2 Stack 1 Top Stack 1 Size Stack 2 Top Stack 2 Size Stack 1 1 Stack 1 expands from the 0th element to the right Stack 2 expands from the 6th element to the left As long as the value of Top 1 is less than 6 and greater than 0, Stack 1 has free elements to input the data in the array Data Structures in C++ Note 3 19 As long as the value of Top 2 is less than 11 and greater than 5, Stack 2 has free elements to input the data in the array When the value of Top 1 is 5, Stack 1 is full When the value of Top 2 is 10, stack 2 is full Elements –1 and –2 are using to store the size of Stack 1 and the subscript of the array for Top 1 needed to manipulate Stack 1 Elements –3 and –4 are using to store the size of Stack 2 and the subscript of the array for Top 2 needed to manipulate Stack 2 Multiple Queues: Following pictures are two ways to do two queues in array: 1. None fixed size of the queues: Graphical Picture: without fixed size of the queue -6 -5 -4 -3 -2 -1 0 5 4 0 0 5 Queue 2 Count Queue 2 Front Queue 2 Size Queue 1 Count Queue 1 Front Queue 1 Size Array_ptr 0 1 2 3 4 5 Queue 1 6 7 8 Queue 2 Temporary Boundary Queue 1 expands from the 0th element to the right and circular back to the 0th element Queue 2 expands from the 8th element to the left and circular back to the 8th element Temporary boundary between the Queue 1 and the Queue 2; as long as there has free elements in the array and boundary would be shift Free elements could be any where in the Queue such as before the front, after the rear, and between front and rear in the Queue Queue 1’s and Queue 2 ‘s size could be change if it is necessary. When the Queue 1 is full and the Queue 2 has free space; the Queue 1 can increase the size to use that free space from the Queue 2. Same way for the Queue 2 Elements –1, –2, and –3 are using to store the size of the Queue 1, the front of the Queue 1, and the data count for the Queue 1 needed to manipulate the Queue 1 Elements –4, –5, and –6 are using to store the size of the Queue 2, the front of the Queue 2, and the data count for the Queue 2 needed to manipulate the Queue 2 Inserts data to the Queue 1, Q1Rear = (Q1Front + Q1count) % Q1Size Data Structures in C++ Note 3 20 Inserts data to the Queue 2, Q2Rear = (Q2Front + Q2count) % Q2Size + Q1Size Deletes data from the Queue 1, Q1Front = (Q1Front + 1) % Q1Size Deletes data from the Queue 2, Q2Front = (Q2Front + 1) % Q2Size + Q1Size 2. Fixed size of the queue: Graphical Picture: with fixed size of the queue -6 -5 -4 -3 -2 -1 0 5 4 0 0 5 Queue 2 Count Queue 2 Front Queue 2 Size Queue 1 Count Queue 1 Front Queue 1 Size Array_ptr 0 1 2 3 4 5 Queue 1 6 7 8 Queue 2 Boundary between the Queues Queue 1 expands from the 0th element to the 4th element and circular back to 0th element Queue 2 expands from the 8th element to the 5th element and circular back to 8th element The boundary is fixed between the Queue 1 and the Queue 2 Free elements could be any where in the Queue such as before the front, after the rear, and between front and rear in the Queue Elements –1, –2, and –3 are using to store the size of the Queue 1, the front of the Queue 1, and the data count for the Queue 1 needed to manipulate the Queue 1 Elements –4, –5, and –6 are using to store the size of the Queue 2, the front of the Queue 2, and the data count for the Queue 2 needed to manipulate the Queue 2 Inserts data to the Queue 1, Q1Rear = (Q1Front + Q1count) % Q1Size Inserts data to the Queue 2, Q2Rear = (Q2Front + Q2count) % Q2Size + Q1Size Deletes data from the Queue 1, Q1Front = (Q1Front + 1) % Q1Size Deletes data from the Queue 2, Q2Front = (Q2Front + 1) % Q2Size + Q1Size