Stack:: Note 3: Stack and Queue Concept in Data Structure For Application
Stack:: Note 3: Stack and Queue Concept in Data Structure For Application
Stack:: Note 3: Stack and Queue Concept in Data Structure For Application
14
stack:
stack:
top = 3
3
2
1
top = 2
add 4 to the
stack
top = 3
increase top
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;
}
15
Delete: a pop( ) operation removes an item from the topmost location on the stack
stack:
stack:
stack:
4
3
top = 3
top = 3
3
delete 4 from
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.
The non-circular queue with 8 elements:
16
Algorithms:
Variables:
short qfront = -1, qrear = -1, qsize = 8;
Insert:
qrear = qsize-1
and
qfront = -1
qrear = qsize-1
queue
is full
return
1
F
F
Delete:
qfront = qrear
queue
is
empty
qfront = -1
qrear = -1
return 1
qfront++
value = queue[qfront]
return 0
Graphical Presentation:
0
qfront
Insert:
qrear
qcount = qsize
17
queue
is full
return
1
Delete:
qcount = 0
T
queue
is
empty
return
1
value = queue[qfront]
qfront = (qfront+1)% qsize
qcount - return 0
Graphical Presentation:
4
qrear
qfront
Structure
Type
Stacks
Queue
Linear Stacks
Non-Circular Queue
Circular Queue
Priority Queue
18
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
10
11
Stack 2
Stack 1 Top
Stack 2 Top
Stack 1
12
-4
-3
-2
-1
10
Stack 2
Stack 1 Top
Stack 1 Size
Stack 2 Top
Stack 2 Size
Stack 1
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
Queue 2 Count
Queue 2 Front
Queue 2 Size
Queue 1 Count
Queue 1 Front
Queue 1 Size
Array_ptr
Queue 1
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 1s 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
20
-6
-5
-4
-3
-2
-1
Queue 2 Count
Queue 2 Front
Queue 2 Size
Queue 1 Count
Queue 1 Front
Queue 1 Size
Array_ptr
Queue 1
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