Insertion and Deletion in a Queue
Algorithm (INSERT) :-
Step 1 :- If(rear == maxsize - 1)
output "Queue overflow and return"
else
set rear = rear + 1
Step 2 :- [Insert an element]
queue[rear] = item
Step 3 :- [set front pointer]
If front == -1
then front = 0
Step 4 :- Return
Algorithm (DELETION) :-
Step 1 :- If (front>rear)|| front == -1
Output "Queue underflow"
Exit
Step 2 :-[Remove an element]
item = queue[front]
Step 3 :-[check for empty queue]
if(front == rear)
front = 0
rear = -1
else
front = front + 1
Disadvantage of Queue:-
Consider the following queue as show below:-
0 1 2 3 4
10 20 30 40 50
Step 1 :- If(rear == maxsize - 1)
output "Queue overflow and return"
else
set rear = rear + 1
Step 2 :- [Insert an element]
queue[rear] = item
Step 3 :- [set front pointer]
If front == -1
then front = 0
Step 4 :- Return
Algorithm (DELETION) :-
Step 1 :- If (front>rear)|| front == -1
Output "Queue underflow"
Exit
Step 2 :-[Remove an element]
item = queue[front]
Step 3 :-[check for empty queue]
if(front == rear)
front = 0
rear = -1
else
front = front + 1
Disadvantage of Queue:-
Consider the following queue as show below:-
0 1 2 3 4
10 20 30 40 50
- This situation arises when 5 element say 10, 20, 30, 40, 50
- are inserted and then deleting first four element
- If we try to insert an element 60, since rear (r) has value maxsize - 1 we get an overflow condition and it is not possible to insert any element
- Even through queue is not full , in this case it is not possible to insert any item into the queue .
- This disadvantage can over-come if we use the circular queue
No comments