You are on page 1of 6

QUEUE (ALGORITHM & EXAMPLES)

1.
2.
3.
4.

Array implementation of Queue


Linked List Implementation of Queue
Doubly Ended Queue
Priority Queue

1. ARRAY IMPLEMENTATION OF QUEUE


a. Insertion:
ALGORITHM: Let QUEUE can have maximum of N elements. FRONT points to the first
element in the queue and REAR points to the last element in the queue. ITEM represents
the element to be inserted in the queue.
1. If (FRONT = 1 and REAR = N) OR ( FRONT = REAR + 1)
2.
Write OVERFLOW
3.
Return
4. If FRONT = NULL
5.
FRONT = 1 and REAR = 1
6. Else If REAR = N
7.
REAR = 1
8. Else
9.
REAR = REAR + 1
10. QUEUE [ REAR ] = ITEM
11. Return
b. Deletion:
ALGORITHM: Let QUEUE can have maximum of N elements. FRONT points to the first
element in the queue and REAR points to the last element in the queue. This function
returns the ITEM which stores the element to be deleted from the queue.
1. If FRONT = NULL
2.
Write UNDERFLOW
3.
Return
4. ITEM = QUEUE [ FRONT ]
5. If FRONT = REAR
6.
FRONT = NULL
7.
REAR = NULL
8. Else If FRONT = N
9.
FRONT = 1
10. Else
11.
FRONT = FRONT + 1
12. Return

EXAMPLE:

2. LINKED LIST IMPLEMENTATION OF QUEUE


a. Insertion:
ALGORITHM: AVAIL is the pointer to the new memory block. FRONT points to the
first node and REAR points to the last node. ITEM is the element to be inserted.
1. If AVAIL = NULL
2.
Write OVERFLOW
3.
Return
4. NEW = AVAIL
5. INFO [ NEW] = ITEM
6. LINK [ NEW] = NULL
7. If FRONT = NULL
8.
FRONT = REAR = NEW
9. Else
10.
LINK [ REAR] = NEW
11.
REAR = NEW
12. Return
b. Deletion:
ALGORITHM: FRONT points to the first node and REAR points to the last node. ITEM
stores the INFO of the block to be deleted.
1.
2.
3.
4.
5.
6.
7.
8.

If FRONT = NULL
Write UNDERFLOW
Return
TEMP = FRONT
ITEM = INFO [TEMP]
FRONT = LINK [ TEMP]
Free (TEMP)
Return

EXAMPLE:

3. DOUBLY ENDED QUEUE


A dequeue / deque is a linear list in which elements can be added or removed at either end
but not in the middle.

EXAMPLE:

PRIORITY QUEUE
A priority queue is a collection of elements such that each element has been assigned a priority
and such that the order in which elements are deleted and processed comes from the following
rules:
1) An element of higher priority is processed before any element of lower priority.
2) 2 elements with the same priority are processed according to the order in which they were
added to the queue.
Priority queues come in two forms
1. Max-priority Queue
2. Min-priority Queue

1. One-way List representation of a Priority Queue:


Each node in the list will contain 3 items:
a) An information field INFO
b) A priority number PRN
c) A LINK
A node X precedes a node Y in the list when X has higher priority than Y or when both have
the same priority but X was added to the list before Y. This means that the order in the oneway list corresponds to the order of the priority queue.
Priority Number here is assumed as: Lower the priority no., Higher the priority.
Example:

INSERTION:
ALGORITHM: This algo adds an ITEM with priority number N to a priority queue which is
maintained in memory as a one-way list.
1. If AVAIL = NULL
2.
Print OVERFLOW
3.
Return
4. NEW=AVAIL
5. INFO[NEW]=ITEM
6. PRN[NEW]=N
7. PTR = START

8. While (LINK[PTR] != NULL) and (PRN[LINK[PTR]] <= N)


9.
PTR=LINK[PTR]
10. LINK[NEW]=LINK[PTR]
11. LINK[PTR]=NEW

EXAMPLE:

DELETION
ALGORITHM: This algorithm deletes and processes the first element in a priority queue
which appears in memory as a one-way list:
1. ITEM=INFO[START]
2. TMP=START
3. START=LINK[START]
4. Free(TMP)
5. Exit/ Return

EXAMPLE:

2. Applications:
a) In Heaps
b) Max-priority queues: To schedule jobs on a shared computer etc.

You might also like