358 33 Powerpoint-Slides 9-Stacks-Queues Chapter-9
358 33 Powerpoint-Slides 9-Stacks-Queues Chapter-9
Another plate
will be added on
The topmost plate
top of this plate
will be removed first
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4 5 6
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4
0 1 2 TOP = 3 4 5 6 7 8 9
1 2 3 4 5
0 1 2 3 TOP = 4 6 7 8 9
Step 1: Allocate memory for the new node and name it as New_Node
Step 2: SET New_Node->DATA = VAL
Step 3: IF TOP = NULL, then
SET New_Node->NEXT = NULL
SET TOP = New_Node
ELSE
SET New_node->NEXT = TOP
SET TOP = New_Node
[END OF IF]
Step 4: END
1 7 3 4 2 6 5 X
TOP
9 1 7 3 4 2 6 5 X
TOP
9 1 7 3 4 2 6 5 X
TOP
1 7 3 4 2 6 5 X
TOP
Stack A Stack B
Step 1: Reverse the infix string. Note that while reversing the
string you must interchange left and right parenthesis.
Step2: Obtain the corresponding postfix expression of the infix
expression obtained as a result of Step1.
Step 3: Reverse the postfix expression to get the prefix
expression
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Here, front = 1 and rear = 6.
However, before inserting an element in the queue we must check for
overflow conditions. An overflow will occur when we will try to insert an
element into a queue that is already full. When Rear = MAX – 1, where
MAX is the size of the queue that is, MAX specifies the maximum number
of elements that the queue can hold.
Similarly, before deleting an element from the queue, we must check for
underflow condition. An underflow condition occurs when we try to delete
an element from a queue that is already empty. If front = -1 and rear = -1,
this means there is no element in the queue.
0 1 2 3 4 5 6 7 8 9
Front=0 1 2 3 4 5 6 7 8 rear = 9
If rear != MAX – 1, then the value will be inserted and rear will be
incremented as illustrated in figure
90 49 7 18 14 36 45 21
Front=0 1 2 3 4 5 6 7 rear= 8 9
7 18 14 36 45 21 99 72
Algorithm to insert an element in the circular queue
0 1 front = 2 3 4 5 6 7 8 rear = 9
0 1 2 3 4 5 6 7 8 9
If the queue is not empty and after returning the value on front, if front =
rear, then it means now the queue has become empty and so front and
rear is set to -1. This is illustrated in figure
Delete this element and set
rear = front = -1
81
0 1 2 3 4 5 6 7 8 front=rear= 9
72 63 9 18 27 39 81
0 1 2 3 4 front= 5 6 7 8 rear= 9
1 7 3 4 2 6 5 X
FRONT REAR
Insert
Operation 9 1 7 3 4 2 6 5 X
REAR
FRONT
Delete Operation 1 7 3 4 2 6 5 X
REAR
FRONT
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR->DATA = VAL
Step 3: IF FRONT = NULL, then
SET FRONT = REAR = PTR;
SET FRONT->NEXT = REAR->NEXT = NULL
ELSE
SET REAR->NEXT = PTR
SET REAR = PTR
SET REAR->NEXT = NULL
[END OF IF]
Step 4: END
29 37 45 54 63
0 1 2 LEFT = 3 4 5 6 RIGHT = 7 8 9
56 63 27 18
42
A 1 B 2 C 3 D 3 E 4
Queue A Queuez B