Linear Queue
Linear Queue
1 Queue 9/11/2023
• Nowadays, several virtual messaging applications serve the obligation of
keeping in touch with friends and family.
• These applications maintain the hierarchy of text messages irrespective of
whether the person is online or offline.
• Question: how are these messaging applications maintaining the order of text messages?
• The answer to this question lies in the queue of data structures.
• All messaging applications maintain a queue for each user, containing messages to be delivered to user.
• When user connects to network, messages in queue get delivered.And later, empty queue is deleted.
2 Queue
9/11/2023
QUEUE
3 Queue 9/11/2023
QUEUE
❖ A queue is an ordered list which enables insert operations to be performed at one end
called REAR and delete operations to be performed at another end called FRONT .
❖ Queue is referred as First In First Out list(FIFO) or Last in Last out (LILO) .
❖ For example, people waiting in line for a rail ticket form a queue.
QUEUE
4 Queue 9/11/2023
MAIN QUEUE OPERATIONS
❖ EnQueue: The act of adding a new element to a queue.
5 Queue 9/11/2023
MAIN QUEUE OPERATIONS
❖ EnQueue: When an element is inserted to a queue, the concept is called EnQueue.
❖ Enqueue adds an element to the queue, similar to the Push operation of stack.
❖ New item enters at Rear/Tail of the queue and is added at Rear end of the queue.
❖ EnQueuing an element in a full queue is called overflow.
6 Queue 9/11/2023
EnQueue
Steps to enqueue (insert) data into a queue −
❖ Step 1− Check if the queue is full.
❖ Step 2− If queue is full, produce overflow error and exit.
❖ Step 3− If queue is not full, increment rear pointer to point to the next empty space.
❖ Step 4− Add data element to the queue location, where the rear is pointing.
❖ Step 5− Return success.
Algorithm Program
procedure enqueue(data) int enqueue(int data)
if REAR = MAX - 1 { if(isfull())
return Overflow {return 0;}
endif (FRONT = -1 and REAR = -1) elseif(front=-1 && rear=-1)
SET FRONT = REAR = 0 {front=rear=0;}
else else
SET REAR = REAR + 1
{rear = rear + 1;}
queue[REAR] ← data
queue[rear] = data;
return true
return 1;
end
Queue procedure 9/11/2023
7 }
EnQueue
8 Queue 9/11/2023
MAIN QUEUE OPERATIONS
❖ DeQueue:When an element is removed from a queue, the concept is called DeQueue.
❖ Dequeue deletes an element to the queue, similar to the Pop operation of stack.
❖ Dequeue operation returns and deletes the Front/Head element from the queue.
❖ DeQueueing an empty queue is called underflow.
9 Queue 9/11/2023
DeQueue
Steps to dequeue (delete) data from a queue −
❖ Step 1 − Check if the queue is empty.
❖ Step 2 − If the queue is empty, produce underflow error and exit.
❖ Step 3 If the queue is not empty, access the data where front is pointing.
❖ Step 4 − Increment front pointer to point to the next available data element.
❖ Step 5 − Returns success.
Algorithm C program
procedure dequeue int dequeue()
if FRONT = -1 or FRONT > REAR {
return underflow
if(isempty())
end if
return 0;
data = queue[FRONT]
int data = queue[front];
FRONT ← FRONT + 1
front = front + 1;
return true
return data;
end procedure }
10 Queue 9/11/2023
DeQueue
11 Queue 9/11/2023
C Implementation of EnQueue and DeQueue
12 9/11/2023
Queue
PERFORMANCE
Let “n” be the number of elements in the queue.
The complexities of queue operations are:-
13 Queue 9/11/2023
AUXILIARY QUEUE OPERATIONS
14 Queue 9/11/2023
ISFULL
- To check whether the queue is full or not.
Algorithm C program
begin procedure isfull bool isfull()
if rear equals to MAXSIZE {
return true if(rear == MAXSIZE)
else return true;
return false else
endif return false;
end procedure }
15 Queue 9/11/2023
ISEMPTY
- To check if queue is empty
Algorithm C program
begin procedure isempty
bool isempty()
if front is less than MIN OR
{
front is greater than rear
if(front=-1 && rear=-1)
return true
return true;
else
else
return false
return false;
endif
}
end procedure
16 Queue 9/11/2023
PEEK
-To see the data at the front of the queue, without removing it.
Algorithm C program
begin procedure peek int peek()
return queue[front] { return queue[front];
end procedure }
17 Queue 9/11/2023
APPLICATIONS
• Printers: To maintain the order of pages while printing.
• Asynchronous data transfer (file IO, pipes, sockets).
• Interrupt handling in computers: The interrupts are operated in the same order as they
arrive, i.e., interrupt which comes first, will be dealt with first.
• Process scheduling in Operating systems: To implement round-robin scheduling
algorithms in Multiprogramming.
• Switches and Routers: Both switch and router interfaces maintain ingress (inbound)
and egress (outbound) queues to store packets.
• Customer service systems: Simulation of real-world queues such as call center phone
systems. lines at a ticket counter or other first-come first-served scenario requires a queue.
18 Queue 9/11/2023
CIRCULAR QUEUE
❖ In a Linear queue, once the queue is completely full, it's not
possible to insert more elements.
Deletions and insertions can only be performed only at front
and rear end respectively, as far as linear queue is concerned.
19 Queue 9/11/2023
CIRCULAR QUEUE
❖ Circular queue tries to overcome the problems of Linear Queue.
❖ In Circular Queue, memory is utilized when any element is deleted by using that free position.
❖ In the circular queue, the first index comes right after the last index.
❖ Circular Queue is also a linear data structure, which follows the principle of FIFO(First In First
Out), but instead of ending the queue at the last position, it again starts from the first position after the
last, hence making the queue behave like a circular data structure.
❖ In circular queue, once Queue is full the "First" element of the Queue becomes the "Rear“ element,
if and only if the "Front" has moved forward; otherwise it will again be a "Queue Overflow" state.
❖ It is also called as “Ring buffer”.
20 Queue 9/11/2023
21 Queue 9/11/2023
OPERATIONS ON CIRCULAR QUEUE
1. Front: Get the front item from queue.
2. Rear: Get the last item from queue.
3. enQueue(value) To insert a new element into the circular queue at Rear position -
i. Check whether queue is Full – Check((rear == SIZE-1 && front == 0) || (rear==front-1)).
ii. If Queue is full then display Queue is Full.
iii. If Queue is not full then, if (rear == SIZE – 1 && front != 0) then set rear=0 and insert
element.
4. deQueue() To delete an element from the front position of the circular queue-
i. Check whether queue is Empty means check (front==-1).
ii. If Queue is empty then display Queue is empty else Go to step 3
iii. If (front==rear) then set front=rear= -1 elseif (front==size-1) then set front=0 and return
the element.
22 Queue 9/11/2023
EnQueue
There are three scenarios of inserting an element in a queue.
1.If (rear + 1)%maxsize== front then queue is full. Overflow occurs and insertion can not be performed.
2.If rear != maxsize - 1, then rear will be incremented to the mod(maxsize) and the new value will be
inserted at the rear end of the queue.
3.If front!=0 and rear==maxsize-1, then queue is not full, set rear value to 0 and insert new element.
Algorithm C program
Step 1: If (REAR+1)%MAX=FRONT void enqueue(int x)
Then Write " Overflow" { If(front==-1 && rear==-1)
Go to Step 4 [End OF IF] { front=rear=0;
Step 2: If FRONT=-1 and REAR=-1 queue[rear]=x; }
Set FRONT=REAR=0 else if((rear+1)%N==front)
Else if REAR=MAX-1 and FRONT!=0 {printf(“Overflow”);}
Set REAR=0 else
Else { rear=(rear+1)%N;
Set REAR=(REAR+1)%MAX queue[rear]=x;
[End of If] }
Step 3: Set QUEUE[REAR]=VAL }
Step 4: Exit
23 Queue 9/11/2023
DeQueue
To delete an element from the circular queue, check the following three conditions:-
1. If front = -1, then there are no elements in the queue, i.e. an underflow condition.
2. If only one element in the queue, i.e. rear = front then set rear = front = -1 and queue is
deleted completely.
3. If front = max -1 then, the value is deleted from the front end and front is set to 0. Otherwise,
the front is incremented by 1 and then delete the element at the front end.
Algorithm C program
Step 1: If FRONT = -1 void dequeue()
Then Write " UNDERFLOW “ {
Goto Step 4 {END of IF] if(front==-1&&rear==-1)
Step 2: SET VAL = QUEUE[FRONT] {printf(“underflow”); }
Step 3: IF FRONT = REAR else if(front==rear)
SET FRONT = REAR = -1 {front=rear=-1
ELSE IF FRONT = MAX -1 }
SET FRONT = 0 else
ELSE { //front++
SET FRONT = FRONT + 1 front=(front+1)%N;
[END OF IF] }
Queue 9/11/2023
24 Step 4: EXIT }
DISPLAY
C program
void display()
{ int i=front;
26 Queue 9/11/2023
TYPES OF DEQUE
❖ Input restricted Deque
❖ Elements can be inserted only at one end.
❖ Elements can be removed from both the ends.
27 Queue 9/11/2023
DEQUE AS STACK AND QUEUE
❖As STACK
❖ When insertion and deletion is made at the same side
❖As Queue
❖ When items are inserted at one end and removed at the other end.
28 Queue 9/11/2023
DEQUE OPERATIONS
❖ addFront(item) adds a new item to front of deque; needs item as parameter and returns
nothing.
❖ addRear(item) adds a new item to rear of deque; needs item as parameter and returns
nothing.
❖ removeFront() removes front item from the deque; needs no parameters and returns the
item.
❖ removeRear() removes rear item from the deque; needs no parameters and returns the item.
❖ isEmpty() tests whether the deque is empty; needs no parameters and returns a boolean
value.
29 Queue 9/11/2023
DEQUE OPERATIONS
EnQueue
In a linear array implementation of DEQUE, if the array is full, no more elements can be inserted.
In each of the insertion operations, if the array is full, "overflow message" is thrown.
Before performing the insertion operations, these steps are followed.
1. Take an array (deque) of size n.
2. Set two pointers at the first position and set front = -1 and rear = 0.
30 Queue 9/11/2023
EnQueueFRONT(INT DATA)
Algorithm C program
Insert at the Front
Step 1:Check the position of front.
Enquefront(int data){
if((==N-1) || (front==rear+1))
{ printf(“Q is full”);}
elseif (front==-1 && rear==-1)
Step 2:If front < 1, reinitialize front = n-1 (last index). { front=0;rear=0;
deque[front]=data;
}
elseif(front==0)
Step 3:Else, decrease front by 1. { front=N-1;deque[front]=data;
}
else
Step 4: Add the new key 5 into array[front].
{ front=front-1;deque[front]=data;
}
}
31 9/11/2023
Queue
EnQueueREAR(INT DATA)
Algorithm C program
Insert at the Rear Enquerear(int data){
Step 1:Check if the array is full. if((front==0 && rear==N-1) || (front==rear+1))
{ printf(“Q is full”);}
elseif (front==-1 && rear==-1)
{ front=0;rear=0;
Step 2: If the deque is full, reinitialize rear = 0.
deque[rear]=data;
Step 3:Else increase rear by 1. }
elseif(rear==N-1)
{ Rear=0;deque[rear]=data;
}
Step 4: Add the new key 5 into array[rear].
else
{ Rear=rear+1; dque[rear]=data;
}}
32 Queue 9/11/2023
DISPLAY()
C program
void display()
{ int i;i=f;
while(i!=rear)
{
printf(“%d”,deque[i]);
i=(i+1)%n;
}
printf(“%d”,deque[rear]);
}
33 Queue 9/11/2023
DEQUE OPERATIONS
DeQueue
In a linear array implementation of DEQUE, if the array is empty, deletion is not possible.
In each of the deletion operations, if the array is empty, “underrflow message" is displayed.
Before performing the deletion operations, these steps are followed.
1. Take an array (deque) of size n.
2. Set two pointers at the first position and set front = -1 and rear = 0.
34 Queue 9/11/2023
DeQueueFRONT()
Algorithm C program
Delete from the Front
Step 1:Check if the deque is empty. Dequefront()
{ if(front==-1 && rear==-1)
{ printf(“Q is MT”);
}
Step 2: If the deque is empty (i.e. front = -1), deletion
cannot be performed (underflow condition). elseif(front==rear)
Step 3: If deque has only one element (front= rear), { front==-1,rear==-1;}
set front = -1 and rear = -1. elseif(front==n-1)
Step 4: Else if front is at the end (i.e. front = n - 1), set { front=0;}
go to the front( front = 0). else
Step 5: Else, front = front + 1. { front=front+1;
}
}
35 Queue 9/11/2023
DeQueREAR()
Algorithm C program
Delete from the Rear
Step 1:Check if the deque is empty.
DequeRear()
{ if(front==-1 && rear==-1)
{ printf(“Q is MT”);
}
Step 2: If the deque is empty (i.e. front = -1), deletion
elseif(front==rear)
cannot be performed (underflow condition).
{ front==-1,rear==-1;
Step 3: If deque has only one element (front= rear), }
set front = -1 and rear = -1. elseif(rear==0)
Step 4: Else If rear is at the front (i.e. rear = 0), set go { rear=n-1;
to the front ( rear = n – 1). }
else
Step 5: Else, rear=rear-1.
{ rear=rear-1;
}
}
36 Queue 9/11/2023
APPLICATIONS
Palindrome Checker
Madam, Radar, Malayalam
37 Queue 9/11/2023
APPLICATIONS
A-Steal job scheduling algorithm
A-Steal algorithm implements task scheduling for several processors(multiprocessor scheduling).
– The processor gets the first element from the deque.
– When one of the processor completes execution of its own threads it can steal a thread from another
processor.
– The processor gets the last element from the deque of another processor and executes it.
38 Queue 9/11/2023
APPLICATIONS
Undo - Redo operation in software applications
39 Queue 9/11/2023
PRIORITY QUEUE
❖ Priority queue is an extension of the “normal” queue.
❖ It is an abstract data type that contains a group of items.
❖ The “normal” queue follows a pattern of first-in-first-out. It dequeues elements in the same order
followed at the time of insertion operation.
❖ Order of the element in a priority queue depends on element’s priority in that queue.
❖ Priority queue moves the highest priority element at the beginning of the priority queue and the
lowest priority element at the back of the priority queue.
❖ Priority order dequeues those items first that have the highest priority.
40 Queue 9/11/2023
CHARACTERISTICS
41 Queue 9/11/2023
TYPES OF PRIORITY QUEUE
Ascending Order Priority Queue
❖ An ascending order priority queue gives highest priority to the lowest number
in the queue.
❖ For example, six numbers in the priority queue are 4, 8, 12, 45, 35, 20.
❖ Firstly, arrange these numbers in ascending order.
❖ The new list is : 4, 8, 12, 20. 35, 45.
❖ The ascending order priority queue treats 4 as the highest priority since 4 is
the smallest number and 45 as the lowest priority.
42 Queue 9/11/2023
TYPES OF PRIORITY QUEUE
Descending Order Priority Queue
❖ A descending order priority queue gives highest priority to the highest
number in the queue.
❖ For example, six numbers in the priority queue that are 4, 8, 12, 45, 35,
20.
❖ Firstly, arrange these numbers in ascending order.
❖ The new list is : 45, 35, 20, 12, 8, 4.
❖ the descending order priority queue treats 45 as the highest priority
since 45 is the highest number and 45 as the lowest priority.
43 Queue 9/11/2023
BASIC OPERATIONS
❖ insert / enqueue −
❖ add an item to the rear of the queue.
❖ When an element is inserted into queue, priority queue
inserts the item according to its order.
❖ Assuming that data with high value has low priority.
❖ remove / dequeue −
❖ remove an item from the front of the queue.
❖ When an element is removed from queue, queue get
the element using item count.
❖ Once element is removed. Item count is reduced by
one.
44 Queue 9/11/2023
THANKYOU
45 Queue 9/11/2023