Queue
Queue
0 1 2 3 4 5 6 7 8 9
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
• Now, if we want to delete an element from the queue, then the value of front will be incremented. Deletions are
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
[END OF IF]
Step 2: Exit
1 7 3 4 2 6 5 X
FRONT REAR
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
0 1 2 3 4 5 6 7 8 9
90 49 7 18 14 36 45 21 99 72
front=0 1 2 3 4 5 6 7
8 rear = 9
If rear != MAX – 1, then the rear will be incremented and value will
be inserted
90 49 7 18 14 36 45 21 99
front=0 1 2 3 4 5 6 7 rear=
8 9
front=1 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 are set to -1.
Delete this element and set
81 rear = front = -1
0 1 2 3 4 5 6 7 8
front=rear= 9
If the queue is not empty and after returning the value on front, if front = MAX -1,
then front is set to 0.
72 63 9 18 27 39 81
0 1 2 3 4 rear= 5 6 7 8
front= 9
29 37 45 54 63
0 1 2 LEFT = 3 4 5 6
RIGHT = 7 8 9
63 27 18
42
RIGHT = 0 1 2 3 4 5 6 LEFT = 7 8 9
A 1 B 2 C 3 D 3 E 4 X
Queue A Queue B