
#Python queue operations code#
They occupy the in-between world of highly performant machine code on one hand, and easy-to-comprehend abstracted functions on the other.ĭata structures describe how data should be organized so that it solves a computational problem while making its storage (space) complexity and time complexity as optimized as possible. To be a part of these competitive and most lucrative fields, one must fulfill the requirement of having a well-rounded understanding of Python.Data structures sit at the intersection between computer science and data science. The ever-expanding applications of large-scale Data Science and Artificial Intelligence have become two of the most aspiring fields in the 21st century. So, this is not a surprise that Python has become one of the fastest-growing programming languages according to Stack Overflow. Dropbox is created in Python, and hundreds of other big companies are also adapting Python. Big companies like Netflix and IBM use Python. class CircularQueue: def _init_(self, maxSize): self.queue = list() self.maxSize = maxSize self.head = 0 self.tail = 0 def enqueue(self, data): if self.size() = (self.maxSize - 1): return("Queue is full!") else: (data) self.tail = (self.tail+1) % self.maxSize return True def dequeue(self): if self.size() = 0: return("Queue is empty!") else: data = self.queue self.head = (self.head+1) % self.maxSize return data def size(self): if self.tail >= self.head: qSize = self.tail - self.head else: qSize = self.maxSize - (self.head - self.tail) return qSize size = input("Enter the size of the Circular Queue") q = CircularQueue(int(size)) print(q.enqueue(10)) print(q.enqueue(20)) print(q.enqueue(30)) print(q.enqueue(70)) print(q.enqueue(80)) print(q.dequeue()) print(q.dequeue()) print(q.dequeue()) print(q.dequeue()) The output will be: Enter the size of the Circular Queue 4 4 True True True Queue is full! Queue is full! 10 20 30 Queue is empty!įurther, check out our Python interview questions by experts. This reduces the actual size of the queue. Here, you can use the indexes 0 and 1 only after you reset the queue by deleting all its elements. In a regular queue, after some insertion and deletion, there exists empty space which is non-usable. It is an extended version of a normal queue.Ī circular queue solves one of the major problems of a regular queue. In a circular queue, the last element is connected to the first element, forming a circle-like structure. Now, if you want to know why Python is the preferred language for data science, you can go through this blog on Python Data Science tutorial. This brings us to the end of this module in Python Tutorial. But again, if two elements are of the same value, then the order is taken into consideration while removing the elements out of the Python queue. import queueĪs we can see here, the item with the lowest value gets out first. We will be using the q.qsize() function (returns the size of the queue) in order to run the for loop function.

Now that we have the items in the queue, let us use the Python for loop to remove the items. Let us add 5 elements into a queue using the priority queue function. Let us see how this works with the help of an example. But, how is that going to work? Well, the logic behind this policy is that the lowest valued item gets out first.Īgain, to be able to perform priority queue operations, we have to use another function shown below.

What really matters is the value of the items. Here, the order in which we put the items it doesn’t matter. The priority queue in Python is a bit interesting one.
