The document discusses different types of queues, including simple queues, circular queues, priority queues, and deques. It provides details on how each type of queue is implemented and how elements are inserted and removed. Simple queues use an array and only allow insertion at the rear and removal at the front. Circular queues also use an array but allow wrapping around to the beginning so the queue never fills. Priority queues order elements by priority when inserting and removing. Deques allow insertion and removal from both the front and rear.
2. INTRODUCTION
• It is linear data structure
• It is collection of items – List
• Queue means line of items waiting for their turn
• Queue has two ends
• Elements are added at one end.
• Elements are removed from the other end.
3. QUEUE
• Removal of data item is restricted at one end known as FRONT
• Insertion of data item is restricted at other end known as REAR
• The FRONT and REAR are used in describing a linear list,
when queue is implemented
• First element inserted in list, will be the first to be removed -
FIFO
4. QUEUE AS FIFO
• The element inserted first will be removed first from the Queue
• Thus, Queue is known as FIFO (First In-First Out) or FCFS (First
Come First Serve)
• Examples of Queue
○ People waiting in Queue to purchase tickets at railway station or cinema
hall, where the first person in the queue will be served first
5. REPRESENTATION OF QUEUE
• It has two pointer variables
• ○ FRONT : Containing the location of the front element of the
queue
• ○ REAR : Containing the location of the rear element of the queue
• When queue is empty
• FRONT = -1 and REAR = -1
6. OPERATIONS OF QUEUE
• Insertion
• Adding an element in queue will increased
• value of REAR by 1
• REAR = REAR + 1
• Removal
• Removing an element from queue will increased value of FRONT by 1
• FRONT = FRONT + 1
9. SIMPLE QUEUE
• A simple queue is a list of item which are arranged in a line i.e.
queue.
• So we can do insertion only at rear end and the deleteion can be
done at the front end only.
• This way the insertion and deletion of the simple queue
functions
10. SIMPLE QUEUE :-INSERTION
INSERTION(QUEUE, F, R, N, item)
Step 1. if (R >= N)
print “Stack is full”
return 0;
Step 2. R = R + 1; /* Increment R*/
Step 3. QUEUE [R] =item; /*Insert Element*/
Step 4. if (F=-1)
F=0;
return f;
11. SIMPLE QUEUE :-DELETION
PROCEDURE DELETE(QUEUE, F, R, item)
[Deletes ‘item’ from the ‘stack’, ‘F’ is the Front end pointer and ‘R’ is the rare end pointer]
Step 1. if (F == -1) /* Underflow*/
print “Stack is empty.”
end;
2. item = QUEUE[F]; /* Deleting Element*/
3. F = F + 1; /*Incrementing F*/
Step 4. if (F > R)
then F = R = -1;
end;
12. CIRCULAR QUEUE
• To solve this problem, queues implement wrapping around.
• Such queues are called Circular Queues.
• Both the front and the rear pointers wrap around to the beginning of the
array.
• It is also called as “Ring buffer”.
• Items can inserted and deleted from a queue in O(1) time.
13. CIRCULAR QUEUE
• When a new item is inserted at the rear, the pointer to rear moves
upwards.
• Similarly, when an item is deleted from the queue the front arrow moves
downwards.
• After a few insert and delete operations the rear might reach the end of
the queue and no more items can be inserted although the items from the
front of the queue have been deleted and there is space in the queue.
14. CIRCULAR QUEUE :- INSERTION
• Insert CQueue ( )
• Step 1. If (FRONT == 0 and REAR == N-1) or (FRONT == REAR + 1) Then
• Step 2. Print: Overflow
• Step 3. Else
• Step 4. If (REAR == -1) Then [Check if QUEUE is empty]
• (a) Set FRONT = 0
• (b) Set REAR = 0
• Step 5. Else If (REAR == N-1) Then [If REAR reaches end if QUEUE]
• Step 6. Set REAR = 0
• Step 7. Else
• Step 8. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If]
• Step 9. Set QUEUE[REAR] = ITEM
• Step 10. Print: ITEM inserted
• [End of Step 1 If]
• Step11. Exit
15. CIRCULAR QUEUE :-DELETION
• Delete CircularQueue ( )
• Step 1. If (FRONT == -1) Then [Check for Underflow]
• Step 2. Print: Underflow
• Step 3. Else
• Step 4. ITEM = QUEUE[FRONT]
• Step 5. If (FRONT == REAR) Then [If only element is left]
• (a) Set FRONT = -1
• (b) Set REAR = -1
• Step 6. Else If (FRONT == N-1) Then [If FRONT reaches end if QUEUE]
• Step 7. Set FRONT = 0
• Step 8. Else
• Step 9. Set FRONT = FRONT + 1 [Increment FRONT by 1]
• [End of Step 5 If]
• Step 10. Print: ITEM deleted
• [End of Step 1 If]
• Step 11. Exit
16. PRIORITY QUEUE
Suppose you have to work o few assignment then on which
assignment will you work first ? So here we have decided the priority
of the work to be done
You have set priority on basis of days . And this is known as keys.
17. PRIORITY QUEUE
• It is collection of elements where elements are stored according to the their
priority levels
• Inserting and removing of elements from queue is decided by the priority
of the elements
• The two fundamental methods of a priority queue P:
○ insertItem(k,e): Insert an element e with key k into P.
○ removeMin(): Return and remove from P an element with the
smallest key.
18. PRIORITY QUEUE
• When you want to determine the priority for your assignments,
you need a value for each assignment, that you can compare
with each other.
• key: An object that is assigned to an element as a specific
attribute for that element, which can be used to identify , rank ,
or weight that element.
19. • Example: Student records
• Any of the attributes, Student Name, Student Number, or Final Score
can be used as keys.
• Note: Keys may not be unique (Final Score).
20. RULES TO MAINTAIN A PRIORITY QUEUE
• The elements with the higher priority will be processed before any
element of lower priority
• If there are elements with the same priority, then the element
added first in the queue would get processed
23. PRIORITY QUEUE :- DELETION
• Step 11:else
• Step 12 :set q->front[i]=q->front[i]+1;
• Step 13:End if
• Step 14:End if
• Step 15:Break
• Step 16:End if
• Step 17:Set i=i+1
• Step 18:End while
• Step 19:If flag=0
• Step 20:Print “underflow”
• Step 21:Return 0 and go to step 4
• Step 22:Else
• Step 23:Return del_val
• Step 24:End if
• Step 25:end
24. DEQUE
• Deque stands for double-end queue
• A data structure in which elements can be added or deleted at either the
front or rear
• But no changes can be made in the list
• Deque is generalization of both stack and queue
25. DEQUE :- INSERT AND DELETE
• There are two variations of a deque.
• These are
• Input Restricted Deque
• An input restricted deque restricts the insertion of elements at one
end only, but the deletion of elements can perform at both the
ends.
• Output Restricted Deque
• An output restricted queue, restricts the deletion of elements at
one end only, and allows insertion to be done at both the ends of
deque
26. POSSIBILITIES
• The two possibilities that must be considered while inserting or
deleting elements into the queue are :
• When an attempt is made to insert a element into a deque which is
already full, an overflow occurs.
• When an attempt is made to delete an element from a deque which is
empty , underflow occurs.