0% found this document useful (0 votes)
41 views8 pages

Dsa521s Que 2

A queue is a linear data structure that follows the first-in, first-out (FIFO) principle for inserting and removing elements. Elements are inserted at the rear of the queue and removed from the front. Common queue operations include enqueue to insert an element, dequeue to remove an element, peek/front to view the front element, and functions to check if the queue is empty or full. Queues can be implemented using arrays or linked lists and have applications where processing or sharing of resources happens in order, such as printer queues.

Uploaded by

g4zbjcw8jp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Dsa521s Que 2

A queue is a linear data structure that follows the first-in, first-out (FIFO) principle for inserting and removing elements. Elements are inserted at the rear of the queue and removed from the front. Common queue operations include enqueue to insert an element, dequeue to remove an element, peek/front to view the front element, and functions to check if the queue is empty or full. Queues can be implemented using arrays or linked lists and have applications where processing or sharing of resources happens in order, such as printer queues.

Uploaded by

g4zbjcw8jp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Queue Data Structure

What is a Queue?
A queue is a linear data structure that follows a rule for insertion and deletion of elements. The
insertion is done from one end and deletion is done from the other end. That means, the first
element into the queue is the first element out of the queue (FIFO) or the last element into the
queue, is the last element out of the queue (LILO).
Insertion in a queue is done at the rear / tail, and deletion is done at the front / head of the queue.
The technical name for insertion is enqueue, and for deletion is dequeue.
Below is a logical representation of a queue.

rear
0 1 2

dequeue 5 8 3 enqueue
583 583

front

Operations of a queue
Enqueue and dequeue are the two main operations of a queue. Below are some operations of a
queue.

 Enqueue()
 Dequeue()
 Front() / Peek()
 Display()
 isFull()
 isEmpty
The table below shows four main operations of a stack and their description.

Enqueue Dequeue Front / Peek IsEmpty IsFull

Insert an Delete / remove an Returns the Return true if the Return true if the
element into element from the element at the queue is empty. queue is full.
the queue. queue. front of the isEmpty() isFull()
enqueue(5) dequeue() queue. It does
not remove the
element.
peek()

Applications of a queue
 Shared resources e.g. Printer
o A printer will put jobs on a queue if many requests are sent to it
 Call centres (customer care)
o A customer is put on hold on a queue if all representatives are busy
 Processor
o Requests to the processor are put in a queue

Implementation of a queue
A queue can be implemented using an array, linked list or a stack.

Implementation of a queue (Using Arrays)


Implementing a queue using arrays means we are going use static memory allocation. That
means we have to specify the size of the queue beforehand, and while implementing the
queue, we have to follow the FIFO rule. Below is a logical representation of a queue using
arrays.

rear
0 1 2

dequeue 5 8 3 enqueue
583 583

front
Given the queue above, let’s look at the implementation of queue using arrays.

Main Function

Note: We can write a pseudocode in the main function to allow the user to choose whether
to insert, delete, peek or to display elements in the queue, then feed the choice to a case
structure that will call the respective functions to perform that operation. Let’s start with
the main function below.

START
enqueue(5)
enqueue(8)
enqueue(3)
display()
peek()
dequeue()
peek()
display()
END

Enqueue / Insert element into queue

int size = 3
int queue[] = new int[size] //declaring array called queue
front = -1 //setting empty queue
rear = -1 //setting empty queue

enqueue(num){
IF(rear == size - 1)THEN //check if the queue is full
DISPLAY “Queue is full”
ELSE IF(front == -1 AND rear == -1)THEN //check if the queue empty
front = 0 //move front to index 0
rear = 0 //move rear to index 0
queue[rear] = num //insert element in queue
ELSE
rear ++ //move rear to next index position
queue[rear] = num //insert element in queue
ENDIF
}
Dequeue / delete element from queue

dequeue(){
IF(front == -1 AND rear == -1)THEN //check if the queue is empty
DISPLAY “Queue is empty”
ELSE IF(front == rear)
front = -1 //setting empty queue
rear = -1 //setting empty queue
ELSE
DISPLAY “Deleted element is: ” queue[front]
front ++ //delete element from queue
ENDIF
}

Display elements of queue

display(){
IF(front == -1 AND rear == -1)THEN //check if the queue is empty
DISPLAY “Queue is empty”
ELSE
FOR(i = front; i < rear + 1; i++)
DISPLAY queue[i] //display elements of queue
ENDFOR
ENDIF
}

Peek the queue

peek(){
IF(front == -1 AND rear == -1)THEN //check if the queue is empty
DISPLAY “Queue is empty”
ELSE
DISPLAY queue[front] //display element in front of queue
ENDIF
}
Implementation of a queue (Using Linked List)
Implementing a queue using linked list means we are going use dynamic memory allocation,
while following the FIFO rule. Below is a logical representation of a singly linked list.

head

100 5 200 2 300 8 null

100 200 300

Below is a logical representation of a queue using a linked list.

rear

300

front

100 5 200 2 300 8 null

100 200 300

Main Function

START
enqueue(5)
enqueue(2)
enqueue(8)
display()
dequeue()
peek()
END

Enqueue / Insert element into queue

Insertion in a queue is done from the rear. That means we must have a pointer to control
the insertion. The name of the pointer here is rear.

front = null //setting empty queue


rear = null //setting empty queue
enqueue(num){
newNode -> data = num //add data to the new node
newNode -> next = null //make newly created node the last node

IF(front == null AND rear == null)THEN


front = newNode //point front pointer to newly created node
rear = newNode //point rear pointer to newly created node
ELSE
rear -> next = newNode //add the new node to the list
rear = newNode //point rear pointer to newly created node
ENDIF
}

Display elements in the queue

To display the elements of the queue, we need to maintain an extra pointer to help us to
traverse the queue. The name of this pointer in the example below is temp.

display(){
IF(front == null AND rear == null)THEN //check if queue is empty
DISPLAY “Queue is empty”
ELSE
temp = front //point temp to first node
WHILE(temp != null)
DISPALY temp -> data //display element
temp = temp -> next //move temp pointer to next node
ENDWHILE
ENDIF
}

Dequeue / Delete element from queue

Deletion in a queue is done from the front. That means we must have a pointer to control
the deletion. The name of the pointer here is front. We need an extra pointer to allow us to
completely delete the node. This pointer is temp in the example below.

dequeue(){
temp = front //point temp pointer to first node
IF(front == null AND rear == null)THEN //check if queue is empty
DISPLAY “Queue is empty”
ELSE
DISPLAY “The deleted element is: ” front -> data
front = front -> next //move front pointer to next node
temp = free //or use free(temp) to free the unused node
ENDIF
}
Peek the queue

peek(){
IF(front == null AND rear == null)THEN //check if queue is empty
DISPLAY “Queue is empty”
ELSE
DISPLAY “Front element is: ” front -> data //display element
ENDIF
}
Additional resources
1. https://www.geeksforgeeks.org/introduction-to-
queue-data-structure-and-algorithm-tutorials/

2. https://www.studytonight.com/data
structures/tests/2

3. https://ocw.mit.edu/courses/1-00-introduction-to-
computers-and-engineering-problem-solving-spring-
2012/86ffe1bca574b95472301b05d8469aad_MIT1_00S
12_Lec_35.pdf

You might also like