Monday, September 12, 2016
Simple Queue algorithms
Simple Queue algorithms:
This algorithm is for inserting an element in Queue:
Description: Here QUEUE is
an array with Max locations. FRONT and REAR points
to the front and rear of the QUEUE. ITEM is the
value to be inserted.
1) Input value
in ITEM
2) If (REAR ==
Max-1)
Then
[Check
for overflow]
3) Print
”Queue is full”
4) Else
5) If
(FRONT and REAR == -1) Then
[Check if QUEUE is empty]
Set FRONT = 0
Set REAR = 0
6) Else
7) Set
REAR = REAR +
1
[Increment
REAR by 1]
[End
of Step 5 If]
8) QUEUE[REAR]
= ITEM
9) Print
”ITEM inserted “
[End of Step 1 If]
10) Exit
|
This algorithm is for deletion of an element in Queue:
Description: Here QUEUE is
an array with max locations. FRONT and REAR points
to the front and rear of the QUEUE.
1.
If
(FRONT == -1) Then
[Check
for underflow]
2.
Print “Queue is
empty”
3.
Else
4.
ITEM
= QUEUE[FRONT]
5. If (FRONT == REAR)
Then [Check
if only one element is left]
(a) Set FRONT = -1
(b) Set REAR = -1
6.
Else
7.
Set FRONT = FRONT +
1
[Increment
FRONT by 1]
[End of Step 5 If ]
8.
Print “ ITEM deleted “
[ End of Step 1 If ]
9. Exit
|
Algorithms
of queue by Representation
of a queue as a linked list:
This
algorithm is for insertion of an element in queue as linked list:
Description: Here QUEUE is
a linked list with. FRONT and REAR points
to the front and rear of the QUEUE. ITEM is the value to be
inserted.
1) Input
value in ITEM
2) if(
temp == NULL) then
a. print“ Queue is
full”
b. temp→data = item
c. temp→link = NULL
if(
front == NULL)
i. rear = front = temp
ii. return
3) rear→link
= temp
4) rear
= rear→link
5) Exit
|
Description: Here QUEUE is
an linked list. FRONT and REAR points to
the front and rear of the QUEUE.
1. if(
front == NULL) then
a) print
“Queue is empty”
b) temp =
front
c) item = front→data
d) front =
front→link
e) delete
temp
2. Exit
|
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment