Queue – Data Structure
Prepared by
Dr Senthil Kumar A M
What is Queue Data Structure?
Representation of Queue
Components of Queue
Operations of Queue
isEmpty()
isFull()
enQueue()
deQueue()
display()
2
Queue
What is Queue?
― Queue is a data structure
― element can be added in the last and taken only
from the first.
― Known as FIFO (First In First Out)
Representation of Queue?
― The Queue data structure can be represented in two
different implementations:
Using Array
Using LinkedList
3
Components of Queue
Components of Queue
―front : is a variable refers to front
element in queue.
―rear : is a variable refers to rear element
in queue.
Initial conditions
front = rear= -1
4
Queue Operations (Array)
Queue Operations using Array
―isEmpty()
―isFull()
―enQueue()
―deQueue()
―display()
5
Empty Operation
isEmpty()
―Operation that returns true if rear variable
has -1 as its value (or) returns false
boolean isEmpty()
Queue
if(rear == -1) -1 0 1 2 3 4
return true;
else
return false; rear
End if
6
Full Operation
isFull()
―Operation that returns true if rear reached
maximum (maxQueue) size of an array (or)
returns false
boolean isFull()
Queue
if(rear == size-1) 0 1 2 3 4
Size-1
return true; 10 20 30 40 50
else
return false;
rear =
End if
7
Main Operations
Queue Operation
8
Enqueue Operation
enQueue()
―Element can be enqueued when Queue is
not full.
―If Queue is empty then front and rear are
added by 1
Queue enQueue(10)
―
-1 Otherwise
0 1 2 rear
3 is 4added by 1
10 20 30 40 50 enQueue(20)
enQueue(30)
Size-1 enQueue(40)
front
enQueue(50)
rear enQueue(60) Queue is Full
9
enqueue()
EnQueue(int E)
If(isFull())
print("Queue is Full");
else
If(front == -1 && rear == -1)
front = 0;
rear = 0;
else
rear = rear + 1;
End if
queue[rear]=item;
End if
10
Dequeue Operation
deQueue()
―Element can be dequeued when Queue is
not empty.
―Front variable will be shifted to next element
deQueue()
Queue
-1 deQueue()
10 20 30 40 50 deQueue()
deQueue()
deQueue()
deQueue()
11
dequeue()
DeQueue()
If(isEmpty())
print("Queue is Empty");
else
y=queue[front];
If(front == rear)
front = rear = -1;
else
front = front + 1;
End if
return y;
End if
12
dequeue()
Display()
if(isEmpty())
print("Queue is Empty");
else
for(int i=front;i<=rear;i++)
print(Q[i] );
End if
13