Different between stack and queue –
#
|
STACK
|
QUEUE
|
1
|
Objects are inserted and removed at
the same end.
|
Objects are inserted and removed from
different ends.
|
2
|
In stacks only one pointer is used.
It points to the top of the stack.
|
In queues, two different pointers are
used for front and rear ends.
|
3
|
In stacks, the last inserted object
is first to come out.
|
In queues, the object inserted first
is first deleted.
|
4
|
Stacks follow Last In First Out
(LIFO) order.
|
Queues following First In First Out
(FIFO) order.
|
5
|
Stack operations are called push and
pop.
|
Queue operations are called enqueue and
dequeue.
|
6
|
Stacks are visualized as vertical
collections.
|
Queues are visualized as horizontal
collections.
|
7
|
Collection of dinner plates at a
wedding reception is an example of stack.
|
People standing in a file to board a
bus is an example of queue
|
Ø
Circular
queue – in the circular queue, the first index comes right after the last
index.
1. The circular queue will be full only when front = 0 and
rear = Max – 1.
2. A circular
queue is implemented in the same manner as a linear queue is implemented.
3. The only difference will be in the code that performs
insertion and deletion operations.
4. For insertion,
we now have to check for the following three conditions:
·
If front = 0 and
rear = MAX – 1,
then the circular queue is full.
·
If rear! = MAX – 1,
then rear will
be Incremented and the value will be inserted.
·
If front! = 0 and
rear = MAX – 1,
then it means that the queue is not full. So, set rear = 0 and
insert the new element.
Algorithm of insertion -
Step 1: IF FRONT =
and Rear = MAX - 1
Write OVERFLOW
Goto step 4
[End of If ]
Step 2: IF FRONT = -1
and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX -
1 and FRONT! = 0
SET REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR]
= VAL
Step 4: EXIT
Algorithm of deletion
–
Step 1: IF FRONT = -1
Write
UNDERFLOW
Goto
Step 4
[END
of IF]
Step 2: SET VAL =
QUEUE [FRONT]
Step 3: IF FRONT =
REAR
SET
FRONT = REAR = -1
ELSE
IF
FRONT = MAX -1
SET
FRONT = 0
ELSE
SET
FRONT = FRONT + 1
[END
of IF]
[END
OF IF]
Step 4: EXIT