Queue 14 Oct 2019 PDF
Queue 14 Oct 2019 PDF
INTRODUCTION TO QUEUE
Introduction
o Queues as an abstract data type
o Representation of a Queue as an array
Types of Queue
o Circular Queue
o Double Ended Queue
o Priority Queue
o Dequeues
Applications of Queue
Introduction To Queue
Queue is a linear data structure used to represent a linear list. Queue is a linear list of elements in which
deletion of an element can take place only at one end, called as the FRONT and insertion can take place
only at the other end, called the REAR.
The first element in a queue will be the first one to be removed from the list. Therefore, queues are also called
FIFO (First In First Out) lists.
Defination
Queue is a linear data structure which follow First in First out (FIFO) princliple, where elements are added at
REAR end and deleted from FRONT end.
ABSTRACT DATA TYPE .
The definition of an abstract data type in data structure must have following properties
There should be a particular way in which elements are related to each other.
A statement of the operations that can be performed on elements of the ADT should be specified.
QUEUE AS AN ABSTRACT DATA TYPE .
A queue of elements of type A is a finite sequence of elements. Thus, a queue, as an ADT, can be defined
with following properties:
Initialize a queue to be empty.
Determine if a queue is empty or not.
Determine if a queue is full or not.
Insert a new element after the last element in a queue, if it is not full. [Overflow]
Retrieve the element of FRONT position, from a queue, if it is not empty. [Underflow]
Delete the FRONT element from the queue, if it is not empty.
Programming Point of view stack as ADT has define as follow.
Array implementation of QUEUE, which contains integer type of elements
Int QUEUE[10]; // Here QUEUE is an array of Integer type with SIZE 10.
Int FRONT=-1; // FRONT is variable (initially NULL) used to show the position of element to delete.
Int REA R=-1; // REAR is variable (initially NULL) used to show the position of element to insert.
Subject: Data Structure (Queue) Print Date:14/Oct/2019 Page 1 of 11
Int MAX=10; //MAX is a variable shows the maximum size of queue.
Operations…..
void init(); // This procedure initialise the QUEUE to empty state
(FRONT=-1, REAR=-1,MAX=10).
void insert(int item); // This procedure add new element into the QUEUE at REAR position.
void remove( ); // This procedure remove FRONT element from the QUEUE.
int peek( ); // This procedure returns TOP element from the STACK for processing.
int isEmpty(); // This function return 1(TRUE) if QUEUE is empty, otherwise 0.
int isFull(); // This function return 1(TRUE) if QUEUE is full, otherwise 0.
void show(); // This function show elements of QUEUE as they are inserted.
REPRESENTATION OF QUEUE.
1. Queue, being the linear data structure, which follow the First in First out (FIFO) principle.
2. In Queue elements are added at REAR end and deleted from only FRONT end of Queue.
3. A Queue can be implemented by using arrays, structure, pointer and linked lists.
4. Queue can either be a fixed size (Using Array) or it may have a sense of dynamic resizing (Using
Linked List).
Q[0] Q[1] Q[2] Q[3] Q[4] Q[5] Q[6] Q[7] Q[8] Q[9]
10 20 30 40 50 60 70 80 90 100
Front = 0 Rear = 9
CIRCULAR QUEUES.
Defination
“A queue, in which the last node is connected back to the first node to form a cycle, is called as circular queue”.
Explanation:
Circular queue are the queues implemented in circular form rather than in a straight line.
Circular queues overcome the problem of unutilized space in linear queue implemented as an array.
The main disadvantage of linear queue using array is that when elements are deleted from the queue,
new elements cannot be added in their place in the queue, i.e. the position cannot be reused.
After REAR reaches the last position, i.e. MAX-1 in order to reuse the vacant positions, we can bring
rear back to the 0th position, if it is empty, and continue incrementing REAR in same manner as earlier.
Thus rear will have to be incremented circularly.
Rear can be incremented circularly by the following code.
If ((REAR == MAX-1) and (FRONT! =0)
REAR =0;
Else
Rear= rear +1;
For deletion, front will also have to be incremented circularly. (In case FRONT != REAR)
If ((FRONT == MAX-1)
FRONT =0;
Else
FRONT= FRONT +1;
Various methods to check queue is Full or not.
o Use a counter to keep track of the number of elements in the queue. If this counter reaches to
MAX the queue is full.
o If FRONT==0 and REAR=MAX-1 then, we will say that QUEUEis FULL. (Normal Queue)
o If FRONT== REAR+1 then, we will say that QUEUEis FULL. (Circular Queue)
o By checking (REAR+1) % MAX== FRONT.
Following figure explain circular queue in detail
Subject: Data Structure (Queue) Print Date:14/Oct/2019 Page 6 of 11
Representation No 1 …..
Representation No 2 …..
Fig. No 1: Observe that Empty Queue, and FRONT and REAR both are -1.
Fig. No 2: Observe queue with 4 elements, where FRONT is set to 0th position and REAR is set to 3rd
position.
Fig. No. 3: Observe queue is full, because FRONT is 0 and REAR is on last position. If you look in
circular manner REAR is just next to FRONT.
Now Delete Two Elements and Observe from Fig. No 4. that the value of REAR becomes MAX -1 but
still Queue is Not Full.
Now Again Delete Three Elements and Observe from Fig. No 5.
Now Add Two More Elements 1000 & 2000 and Observe Fig. No 6.
Now Delete All 5 Elements and Observe Fig. No 7 and observe that, Front becomes Rear + 1, so it is
also Empty State for Circular Queue.
PRIORITY QUEUES.
A priority queue is a collection of elements.
A priority queue is a queue in which the intrinsic ordering among the elements decides the result of its
basic operations.
The ordering among the elements decides the manner in which Add and Delete operations will be
performed.
The priority queue has following properties.
Each element is assigning a priority number.
An element of higher priority is processed before any elements of lower priority.
An elements with the same priority are processed according to the order in which they are added to
the queue.
Link Representation
We can represent a priority queue in memory dynamically by means of One Way List:
Each node in the list contain three items of information –
An information field INFO,
priority number PRNO
And the link NEXT.