Unit-2: Linear Data Structure Queue
Unit-2: Linear Data Structure Queue
Unit-2: Linear Data Structure Queue
GTU # 3130702
Unit-2
Linear Data Structure
Queue
Deletion Insertion
Front Rear
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 2
Applications of Queue
Queue of people at any service point such as ticketing etc.
Queue of air planes waiting for landing instructions.
Queue of processes in OS.
Queue is also used by Operating systems for Job Scheduling.
When a resource is shared among multiple consumers. E.g., in case of printers the first one to
be entered is the first to be processed.
When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes. Examples include IO Buffers, pipes, file IO, etc.
Queue is used in BFS (Breadth First Search) algorithm. It helps in traversing a tree or graph.
Queue is used in networking to handle congestion.
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 3
Procedure: Enqueue (Q, F, R, N,Y)
This procedure inserts Y at rear end of Queue. R
FR F R F R
FR F R F R
Insert ‘B’ R=3 Delete ‘B’ (R=4) >= (N=4) (Size of Queue)
R=2 F=3 Queue Overflow
F=1 A B B C
Queue Overflow, but space is
there with Queue, this leads to
FR F R the memory wastage
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 6
Circular Queue
A more suitable method of representing simple queue which prevents an excessive use of
memory is to arrange the elements Q[1], Q[2]….,Q[n] in a circular fashion with Q[1] following
Q[n], this is called circular queue.
In circular queue the last node is connected back to the first node to make a circle.
Circular queue is a linear data structure. It follows FIFO principle.
It is also called as “Ring buffer”.
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 7
Procedure: CQINSERT (F, R, Q, N, Y)
This procedure inserts Y at rear end of the Circular Queue. F R
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 8
Function: CQDELETE (F, R, Q, N)
This function deletes & returns an element from front end of the F R
Circular Queue.
6
Queue is represented by a vector Q containing N elements.
F is pointer to the front element of a queue. R F
FR F R F R
FR F R F R
FR F R
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 10
DQueue
A DQueue (double ended queue) is a linear list in which insertion and deletion are performed
from the either end of the structure.
There are two variations of Dqueue
Input restricted dqueue – allows insertion at only one end
Output restricted dqueue – allows deletion from only one end
Insertion Deletion
Deletion Insertion
Dqueue Algorithms
Front Rear
DQINSERT_REAR is same as QINSERT (Enqueue)
DQDELETE_FRONT is same as QDELETE (Dequeue)
DQINSERT_FRONT
DQDELETE_REAR
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 11
Procedure: DQINSERT_FRONT (Q,F,R,N,Y)
This procedure inserts Y at front end of the Circular Queue.
Queue is represented by a vector Q containing N elements.
F is pointer to the front element of a queue.
R is pointer to the rear element of a queue.
1. [Overflow?]
F R
If F = 0
Then Write(‘Empty’)
Return Overflow
10 89 7
If F = 1
Then Write(‘Overflow’)
Return F R
2. [Decrement front Pointer]
F F - 1
3. [Insert Element?] 50 10 89 7
Q[F] Y
Return
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 12
Function: DQDELETE_REAR(Q,F,R)
This function deletes & returns an element from rear end of the Queue.
Queue is represented by a vector Q containing N elements.
F is pointer to the front element of a queue.
R is pointer to the rear element of a queue.
1. [Underflow?] F R
If R = 0
Then Write(‘Underflow’)
Return(0) 7
2. [Delete Element]
Y Q[R]
F R
3. [Queue Empty?]
IF R = F
Then R F 0
10 89 7
Else R R – 1
4. [Return Element]
Return(Y)
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 13
Priority Queue
A queue in which we are able to insert & remove items from any position based on some
property (such as priority of the task to be processed) is often referred as priority queue.
Below fig. represent a priority queue of jobs waiting to use a computer.
Priorities are attached with each Job
Priority 1 indicates Real Time Job
Priority 2 indicates Online Job
Priority 3 indicates Batch Processing Job
Therefore if a job is initiated with priority i, it is inserted immediately at the end of list of other
jobs with priorities i.
Here jobs are always removed from the front of queue.
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 14
Priority Queue Cont…
Task R1 R2 … Ri-1 O1 O2 … Oj-1 B1 B2 … Bk-1 …
Priority 1 1 … 1 2 2 … 2 3 3 … 3 …
Ri Oj Bk
Priority Queue viewed as a single queue with insertion allowed at any position
Priority - 1 R1 R2 … Ri-1 Ri
Priority - 2 O1 O2 … Oj-1 Oj
Priority - 3 B1 B2 … Bk-1 Bk
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Queue) 15
Data Structures (DS)
GTU # 3130702
Thank
You