Queue

Queue is an abstract data type or ordered collection of items where the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue.


The most recently added item in the queue must wait at the end of the collection. The item that has been in the collection the longest is at the front. This ordering principle is sometimes called FIFO, first-in first-out. It is also known as "first-come first-served".

/* Enqueue */
If (REAR == N) Then	[Check for overflow]
	Print: Overflow
Else
	If (FRONT and REAR == 0) Then	[Check if QUEUE is empty]
		(a) Set FRONT = 1
		(b) Set REAR = 1
	Else
		Set REAR = REAR + 1	[Increment REAR by 1]
	[End of Step 4 If]
	QUEUE[REAR] = ITEM
	Print: ITEM inserted
[End of Step 1 If]
Exit

/* Dequeue */
If (FRONT == 0) Then	[Check for underflow]
	Print: Underflow
Else
	ITEM = QUEUE[FRONT]
	If (FRONT == REAR) Then	[Check if only one element is left]
		(a) Set FRONT = 0
		(b) Set REAR = 0
	Else
		Set FRONT = FRONT + 1	[Increment FRONT by 1]
	[End of Step 5 If]
	Print: ITEM deleted
[End of Step 1 If]
Exit


/****** Output ******/

Queue in C


Queue in C++


Queue in JAVA




Circular Queue in C




Double Ended Queue in C++


Double Ended Queue in JAVA

Recommended for you