A Queue is a linear data structure that follows a First-In-First-Out (FIFO) principle. In a queue, the first element inserted is the first element to be removed. It works like a real-life queue of people waiting to do something.
The two primary operations that can be performed on a queue are enqueue and dequeue.
enqueue: This operation adds an element to the end of the queue.
dequeue: This operation removes the element from the front of the queue.
Other operations that are often provided by a queue data structure include:
peek: This operation returns the element at the front of the queue without removing it.
isEmpty: This operation returns true if the queue is empty, and false otherwise.
size: This operation returns the number of elements currently in the queue.
Queues can be implemented using arrays, linked lists, or other data structures.
Application of Queue:
The queue data structure has many practical applications, some of which are:
- Job scheduling: Queues can be used in operating systems to manage the order in which tasks are executed. Each task is added to a queue, and the operating system schedules tasks for execution in the order they were added.
- Print spooling: Queues are often used to manage print jobs in computer systems. Each print job is added to a queue, and the printer processes them in the order they were added.
- Call center routing: In a call center, queues are used to manage incoming calls. Calls are added to a queue, and agents take calls from the front of the queue in a first-come, first-served manner.
- Breadth-first search: In graph theory, breadth-first search algorithms use queues to explore all the vertices in a graph in order of their distance from a starting vertex.
- Resource sharing: Queues can be used to manage shared resources, such as network connections. Requests for the resource are added to a queue, and the resource is granted in the order the requests were received.
- CPU scheduling: In computer operating systems, queues are used to schedule CPU time for processes. Each process is added to a queue, and the operating system allocates CPU time to processes in the order they were added.
- Buffering: In networking and telecommunications, queues are used to buffer packets of data as they are transmitted between devices. The packets are added to a queue, and the receiving device processes them in the order they were received.
These are just a few examples of the many practical applications of the queue data structure.
Implementation of Queue in Python:
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
def peek(self):
if not self.is_empty():
return self.items[0]
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
q= Queue()
n= int(input("How many elements youn want?"))
for i in range(n):
elt=input("Enter element:")
q.enqueue(elt)
print(q.is_empty())
print(q.dequeue())
print(q.size())
print(q.peek())
No comments: