PPT Lecture 3.3.1 Queue
PPT Lecture 3.3.1 Queue
Course Objectives
3
Scheme of Evaluation
4
• A Queue is "an abstract data type in which
elements are added to the rear and removed
QUEUE from the front; a 'first in, first out' (FIFO)
structure."
A queue is a data structure
that stores data in such a way
that the last piece of data
stored, is the last one
retrieved
also called First-In, First-
Out (FIFO)
Only access to the stack is the
first and last element http://www.cs.uregina.ca/Links/class-info/210/Queue/
X A M
enqueue(F)
X A M F A M F
item = dequeue()
item = X
ALGORITHM FOR INSERTION
1. If REAR=MAX then: write: Overflow & Exit
2. Read ITEM
If FRONT=0 then:
a) FRONT=FRONT+1
b) REAR=REAR+1
c) Q[REAR]=ITEM
else
REAR=REAR+1
Q[REAR]=ITEM
3. Exit
8
ALGORITHM FOR DELETION
• If FRONT=0 then: write: “UNDERFLOW and Exit”
• If FRONT=REAR, then:
Q[FRONT]=NULL
FRONT=0
REAR=0
else
Q[FRONT]=NULL
FRONT=FRONT+1
• Exit
9
Circular Queue
• Circular queue is a linear data structure in which the operations are
performed based on FIFO (First In First Out) principle and the last
position is connected back to the first position to make a circle. It is
also called ‘Ring Buffer’.
10
• In a normal Queue, we can insert elements until queue becomes full.
But once queue becomes full, we can not insert the next element
even if there is a space in front of queue.
11
Operations on Circular Queue:
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
• enQueue(value) This function is used to insert an element into the
circular queue. In a circular queue, the new element is always
inserted at Rear position.
• Steps:Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) ||
(rear == front-1)).
• If it is full then display Queue is full. If queue is not full then, check if (rear ==
SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.
• deQueue() This function is used to delete an element from the
circular queue. In a circular queue, the element is always deleted
from front position.
12
Steps:
Step 1:Check whether queue is Empty means check (front==-1).
Step 2:If it is empty then display Queue is empty. If queue is not empty then
step 3
Step 3:Check if (front==rear) if it is true then set front=rear= -1 else check if
(front==size-1), if it is true then set front=0 and return the element.
Time Complexity: Time complexity of enQueue(), deQueue() operation is
O(1) as there is no loop in any of the operation.
Applications:
• Memory Management: The unused memory locations in the case of
ordinary queues can be utilized in circular queues.
• Traffic system: In computer controlled traffic system, circular queues are
used to switch on the traffic lights one by one repeatedly as per the time
set.
• CPU Scheduling: Operating systems often maintain a queue of processes
that are ready to execute or that are waiting for a particular event to occur.13
FAQ
14
Applications of Queue
• Print lines of a document
• Simulation
• Printer sharing between computers
15
REFERENCES
• https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%2
0Queues.html
• Data Structures with C/ schaum outline series/ volume 2
• https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
• https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/
• http://www.yashcode.com/2017/11/prefix-to-postfix-conversion-using-stack.html
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of
India.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in
C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data Structures and Algorithms”, Addison
Wesley
16
THANK YOU