Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

Queue Data Structure

 

Queue


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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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())


In this implementation, the Queue class has a list instance variable called items to store the elements of the queue.

The enqueue() method adds an element to the end of the list using the append() method.

The dequeue() method removes and returns the first element from the list using the pop() method with an index of 0.

The peek() method returns the first element of the list without removing it.

The is_empty() method returns True if the list is empty, and False otherwise.

The size() method returns the number of elements in the list using the built-in len() function.




Queue Data Structure Queue Data Structure Reviewed by Asst. Prof. Sunita Rai on April 14, 2023 Rating: 5

No comments:

Powered by Blogger.