Вы находитесь на странице: 1из 19

Queue

• A queue is a FIFO (First-In, First-Out) data structure in


which the element that is inserted first is the first one
to be taken out.

• The elements in a queue are added at one end


called the REAR and removed from the other end
called the FRONT of the queue.

• Insertion (ENQUEUE) and deletion (DEQUEUE)


operations takes place at two ends called the REAR
and FRONT of the queue respectively.

• Queues can be implemented by using either arrays or


linked lists.
Problem with array representation of Queue
• Wastage of storage:
A queue may not be actually full, but still a request for
insertion operation may be denied –as the rear pointer
reaches the end of the queue.

• Limited Queue capacity


The queue does not grow and shrink as per our
requirement. Length of the queue is fixed in array
representation – but in reality the length of the queue
cannot be predicted before and it varies abruptly.
So, for efficient management of space, the array
representation of a queue is viewed as a circular
representation (a circular queue).

A circular queue is a queue in which the element


next to the last element is the first element.

The advantage of using a circular queue over


linear queue is efficient usage of memory.
To ENQUEUE in a CIRCULAR QUEUE
If R==N
Set R=1

To DEQUEUE in a CIRCULAR QUEUE


If (F=R)≠ NULL
Set F=0, R=0

If F==N
Set F=1
In a CIRCULAR QUEUE

Queue is empty
F = R = NULL

Queue is full
F = (R mod LENGTH) + 1,
where LENGTH is the size of the queue.
Linked List representation
• Singly linked list
Queue is empty
F = R = Header
Headerlink = NULL

Queue contains at least one element


Header link ≠ NULL
Doubly Linked List representation
Queue is empty
F = R = Header
Headerrlink = NULL

Queue contains at least one element


Header rlink ≠ NULL
Linked-list implementation of queues
• In a queue, insertions occur at one end, deletions at the
other end

• Operations at the front of a singly-linked list (SLL) are


O(1), but at the other end they are O(n) as you have to
find the last element each time

• BUT: there is a simple way to use a singly-linked list to


implement both insertions and deletions in O(1) time

• You always need a pointer to the first thing in the list

• You can keep an additional pointer to the last element in


the list
Linked list diagramatic representation of a
queue:

Header
Queue implementation details

• With an array implementation:


– you can have both overflow and underflow
– you should set deleted elements to null

• With a linked-list implementation:


– you can have underflow
– overflow is a global out-of-memory condition
– there is no reason to set deleted elements to null
TYPES OF QUEUES

• A queue data structure can be classified into


the following types:
1. Circular Queue
2. Deque
3. Priority Queue
Deque
• Double-ended queue
• It is a linear list in which elements can be added
or removed at either end but not in the middle.
• Unless stated, deque is maintained by a circular
array Deque with pointers LEFT and RIGHT, which
points to the two ends of the deque.
• A deque is a general representation of a stack and
a queue. It supports both stack and queue
operations.
Deque contd..
Two variations of a deque:
• Input-restricted deque: is a deque which
allows insertions at only one end of the list ,
but allows deletions at both ends of the list

• Output-restricted deque: is a deque which


allows deletions only at one end of the list,
but allows insertions at both ends of the list.
Priority Queue
A Priority Queue is a collection of elements where
each element has been assigned a priority, such
that the order in which elements are deleted and
processed comes from the following rules:
• An element of higher priority is processed before
any element of lower priority.
• Two elements with the same priority are
processed according to the order in which they
were added to the queue.
Priority Queue contd..
• An element in a priority queue can be inserted or
deleted not only at the ends but at any position on
the queue – hence the priority queue does not strictly
follow the FIFO principle.

• A prototype of a priority queue is a timesharing


system – programs of higher priority are processed,
and programs with the same priority form as standard
queue.

• Maintaining a priority queue in memory– several


ways . Two of them are:
– one-way list
– multiple queues.
Applications of Queue
• Simulation : model a real life situation using
computer
• CPU scheduling in a multiprogramming
environment.
– A multiprogramming environment, where a CPU has to
serve more than one program simultaneously.
• Time sharing systems : Round Robin algorithm. A
circular queue is used to implement a Round
Robin algorithm.
• Categorizing data
Tree
• Tree is a non-linear data structure designated at a
special node called root and elements are
arranged in levels without containing cycles.

• Represents hierarchical relationship between


elements, example records, family and table of
contents.
Diagram of a tree:
Q1. a) Elements 8, 7, 5 are inserted in a queue, one
after another starting at 8. Draw the diagram for the
linked list representation of the queue.
b) Perform ENQUEUE(12) and then DEQUEUE on
the above queue. Draw the diagram.

Q2. a) Elements 6, 8, 7, 5 are pushed onto a stack,


one after the another starting at 6. Draw the diagram
for the linked list representation of the stack.
b) Perform one pop operation on the above
stack. Draw the diagram.

Вам также может понравиться