rushnero.blogg.se

Python queue operations
Python queue operations






  1. #Python queue operations full#
  2. #Python queue operations code#

However, there are also “last-in, first-out” (LIFO) queues, where the last item added to the queue (the “last-in” item) is the first item that can be removed (the “first-out” item). And in the case when the head is at the last element of the array, it will go 1.A queue is a data structure that allows items to be added to one end, called the “rear,” and removed from the other end, called the “front.” Queues are often referred to as “first-in, first-out” (FIFO) data structures because the first item added to the queue (the “first-in” item) is also the first item that can be removed (the “first-out” item). Now, we just have to increase the head pointer by 1. To dequeue, we will first store the item which we are going to delete from the queue in a variable because we will be returning it at last. If the queue is empty, then we will throw an error.

python queue operations

To dequeue, we will first check if the queue is empty or not. Otherwise, we will just increase the tail by 1. While adding the element, it might be possible that we have added the element at the last of the array and in this case, the tail will go to the first element of the array. If the queue is not full, we will add the element to the tail i.e, Q = x.

python queue operations

#Python queue operations full#

To enqueue any item to the queue, we will first check if the queue is full or not i.e., Now, we have to deal with the enqueue and the dequeue operations. Similarly, we will say that if the head of a queue is 1 more than the tail, the queue is full. We can easily check if a queue is empty or not by checking if head and tail are pointing to the same location or not at any time. Initially, the queue will be empty i.e., both head and tail will point to the same location i.e., at index 1. In this case, our tail will point to the first element of the array and will follow a circular order. Suppose tail is at the last element of the queue and there are empty blocks before head as shown in the picture given below. To insert any element, we add that element at tail and increase the tail by one to point to the next element of the array. head will always point to the oldest element which was added and tail will point where the new element is going to be added. We will maintain two pointers - tail and head to represent a queue.

python queue operations

Customers calling a call center are kept in queues when they wait for someone to pick up the calls.It can be also used by an operating system when it has to schedule jobs with equal priority.Queue is used to implement many algorithms like Breadth First Search (BFS), etc.Queues are used in a lot of applications, few of them are:

#Python queue operations code#

IsFull → It is used to check whether the queue is full or not.įront → It is similar to the top operation of a stack i.e., it returns the front element of the queue (but don’t delete it).īefore moving forward to code up these operations, let’s discuss the applications of a queue. IsEmpty → It is used to check whether the queue has any element or not. As stated earlier, any new item enters at the tail of the queue, so Enqueue adds an item to the tail of a queue.ĭequeue → It is similar to the pop operation of stack i.e., it returns and deletes the front element from the queue.

python queue operations

But let’s first discuss the operations which are done on a queue.Įnqueue → Enqueue is an operation which adds an element to the queue. Similar to the stack, we will implement the queue using a linked list as well as with an array. Similar to a queue of day to day life, in Computer Science also, a new element enters a queue at the last (tail of the queue) and removal of an element occurs from the front (head of the queue). For example, a new person enters a queue at the last and the person who is at the front (who must have entered the queue at first) will be served first. It is equivalent to the queues in our general life. A queue follows FIFO (First-in, First out) policy. Similar to stacks, a queue is also an Abstract Data Type or ADT.








Python queue operations