Notes On Queue
Notes On Queue
A Queue is defined as a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end,
and all deletions from the list are made at the other end. The element which is first
pushed into the order, the operation is first performed on that.
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the
Queue is implemented using the array. Variables used in this case are
int front(): This operation will return the element at the front without removing it and it
take O(1) time.
int rear(): This operation will return the element at the rear without removing it, Its
Time Complexity is O(1).
int isEmpty(): This operation indicates whether the queue is empty or not. This
Operation also done in O(1).
int size(): This operation will return the size of the queue i.e. the total number of
elements present in the queue and it’s time complexity is O(n).
A Queue is a linear data structure. This data structure follows a particular order in
which the operations are performed. The order is First In First Out (FIFO). It means
that the element that is inserted first in the queue will come out first and the element
that is inserted last will come out last. It is an ordered list in which insertion of an
element is done from one end which is known as the rear end and deletion of an
element is done from the other which is known as the front end. Similar to stacks,
multiple operations can be performed on the queue. When an element is inserted in
a queue, then the operation is known as Enqueue and when an element is deleted
from the queue, then the operation is known as Dequeue. It is important to know
that we cannot insert an element if the size of the queue is full and cannot delete an
element when the queue itself is empty. If we try to insert an element even after the
queue is full, then such a condition is known as overflow whereas, if we try to delete
an element even after the queue is empty then such a condition is known as
underflow.
Implementation of Queue:
Sequential allocation: A queue can be implemented using an array. It can organize a
limited number of elements.
Linked list allocation: A queue can be implemented using a linked list. It can
organize an unlimited number of elements.
Applications of Queue:
Multi programming: Multi programming means when multiple programs are running
in the main memory. It is essential to organize these multiple programs and these
multiple programs are organized as queues.
Network: In a network, a queue is used in devices such as a router or a switch.
another application of a queue is a mail queue which is a directory that stores data
and controls files for mail messages.
Job Scheduling: The computer has a task to execute a particular number of jobs that
are scheduled to be executed one after another. These jobs are assigned to the
processor one by one which is organized using a queue.
Shared resources: Queues are used as waiting lists for a single shared resource.
Advantages of Queue:
A large amount of data can be managed efficiently with ease.
Operations such as insertion and deletion can be performed with ease as it follows
the first in first out rule.
Queues are useful when a particular service is used by multiple consumers.
Queues are fast in speed for data inter-process communication.
Queues can be used in the implementation of other data structures.
Disadvantages of Queue:
The operations such as insertion and deletion of elements from the middle are time
consuming.
Limited Space.
In a classical queue, a new element can only be inserted when the existing elements
are deleted from the queue.
Searching an element takes O(N) time.
Maximum size of a queue must be defined prior.