Professional Documents
Culture Documents
Algorithm
Queues
Queue
Queue Operations
Topics
Representation of Queue
Types of Queue
Queues
$ $
Front
Rear
Queues
Queue – an ordered list in which all
insertions take place at one end
while all deletions take place at
the other end.
Top/rear – end of the list where all
insertion takes place
Bottom/front – other end of the list
where all deletions take place
A queue is processed in a First In
First Out fashion, FIFO.
Operating systems:
queue of print jobs to send to the printer
queue of programs / processes to be run
(CPU scheduling)
queue of network data packets to send
Queues in Programming:
Computer modeling a line of customers or clients
$ $
Front
Rear
Operations on Queue
$ $
Front
Rear
Arrays Stacks Queues
Insert Push Put, add, enqueue
Delete Pop Get, delete, remove,
dequeue
Search Peek Peek, retrieve
Operations on Queue
Operations on
Queue
Enqueue – insertion is
always at the top of the
list
Enqueue – insertion an element at the top of the list
Dequeue – to delete an element at the bottom of the
list
Peek or Retrieve - print or display the element at the
bottom of the list
• being viewed or retrieved is what you are about to
Operations on
delete or remove and not what you have inserted Queue
Empty and Full
• return you an error because the queue is empty
• a message that the queue is full when trying to
insert another element
Operations on Queue
Add : adds a new node • Add(X,Q) → add the value X to the tail of queue
1. Initialiaze(Q) <empty> -
2. Enqueue (‘A’,Q) A -
3. Enqueue(‘B’,Q) A B -
4. Enqueue(‘C’,Q) A B C -
5. Dequeue (Q) B C A
6. Enqueue(‘D’,Q) B C D -
7. Dequeue(Q) C D B
8. Dequeue(Q) D C
9. Dequeue(Q) <empty> D
Exercise: Queue Operation
Queue as one-
dimensional array
Representation of Queues
Queue as one-dimensional array
10
7
null
head_ptr
tail_ptr
13
Linked List
Representation
15
10
Which end do you think
is the front of the
7
queue? Why?
null
head_ptr
tail_ptr
Linked List
Front Representation
13
15
The head_ptr points to the
front of the list.
10
null
head_ptr
Rear
tail_ptr
Types of Queue
Simple Queue
In a simple queue,
insertion takes place at
the rear and removal
occurs at the front. It
strictly follows FIFO
rule.
Types of Queue
Circular Queue
In a circular queue, the last
element points to the first
element making a circular link.
The main advantage of a circular
queue over a simple queue is
better memory utilization. If the
last position is full and the first
position is empty then, an
element can be inserted in the
first position. This action is not
possible in a simple queue.
Types of Queue
Priority Queue
A priority queue is a special
type of queue in which each
element is associated with a
priority and is served
according to its priority. If
elements with the same
priority occur, they are served
according to their order in the
queue.
Circular Queue
4 8 6
An array of integers to
implement a queue of No data yet in
integers this part of the array.
Array Implementation – Circular Queue
2 last
4 8 6
Array Implementation – Circular Queue
2 last
4 8 6
Array Implementation – Circular Queue
too.
1 first
3 last
8 6 2
Array Implementation – Circular Queue
5 last
2 6 1
Array Implementation – Circular Queue
0 last
4 2 6 1
Array Implementation – Circular Queue
Aspects of Implementing queue as an Array
Easy to implement, but it has a limited capacity
with a fixed array or you must use a dynamic
3 size
array for an unbounded capacity
Special behaviour is needed when the rear
reaches the end of the array, called circular 0 first
array. The idea of a circular array is that the end of
the array “wraps around” to the start of the array 2 last
4 8 6
Algorithm for inserting element in a circular queue:
4. a. if the top points where bottom is pointing and the queue holds a not
NULL value for it, then its a "queue overflow" state, so quit; else go to step-b
• b. insert the new value for the queue position pointed by the bottom
Circular Queue
Circular
Queue
Circular Queue
Algorithm to delete the element at the top of a
circular queue
1. If the queue is empty then say "empty queue"
and quit; else continue
2. Delete the top element
3. If the top is pointing to the last position of the
queue then step-4 else step-5
4. Make the top point to the first position in the
queue and quit
5. Increment the top position by one
Java Code for Queue
public void insert (int j)
{ if (rear == maxSize -1)
rear = -1;
queArray [++rear] = j; nItems++;
}
public int remove()
{ int temp = queArray [front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public int peekFront()
{ return queArray[front]; }
Queue: Circular Array vs. Linked List
39