DEV Community

Cover image for Circular Queue using python..!
rohit-br-k
rohit-br-k

Posted on • Edited on

Circular Queue using python..!

Queue is similar to stack but it follows F.I.F.O method.

Queue can developed by using normal python list and also with linked-list.Here,Queue with linked-list is preferred because time-complexity and space-complexity is good compared to Queue with normal python list.

In Queue with python list, the enqueue and dequeue will end in worst case with increase in inputs.So,the better version of this is circular queue.

Circular Queue is used in Traffic System,Memory Management,CPU Scheduling But here with increase in data's the time-complexity will become worst but space complexity can be reduced with fixed size.So it's always good to implement Queue using linked list if time-complexity is preferred.

We can look into the implementation of Circular Queue in python:
Here we go,

#we need Queue class to initialize fixed empty list with start and top
class CircularQ:
    def __init__(self,maxSize):
        self.data = maxSize *[None]
        self.maxSize = maxSize
        self.start = -1
        self.top = -1
    def __str__(self):
        return str(self.data)

    def isEmpty(self,value):
        if self.top and self.star == -1:
            return True
        else:
            return False

    def isFull(self):
        if self.top+1 == self.maxSize and self.start == 0:
            return True
        elif self.start+1 == self.top:
            return True
        else:
            return False

    def insert(self,value):
        self.top+=1
        self.data[self.top] = value
        self.start=0

    def delete(self):
        first = self.data[self.start]
        start = self.start

        if self.start == self.top:
            self.start =-1
            self.top=-1

        elif self.start+1 == self.maxSize:
            self.start = 0

        else:
            self.start+=1
        self.data[start] = None
        return first



circular = CircularQ(5)
circular.insert(1)
circular.insert(2)
circular.insert(3)
circular.insert(4)
circular.insert(5)
circular.delete()
circular.delete()
print(circular)
Enter fullscreen mode Exit fullscreen mode

Thank You,
I hope everyone will have a good health and a better future...!!

You Can Support Me..πŸ‘‡
Buy Me A Coffee

Top comments (6)

Collapse
 
bdelespierre profile image
Benjamin Delespierre • Edited
class CircularQ:
    def isEmpty(self, value):
        return self.top and self.star == -1

    def isFull(self):
        return (self.top+1 == self.maxSize and self.start == 0)\
            or (self.start+1 == self.top)
Enter fullscreen mode Exit fullscreen mode

Sorry couldn't help myself πŸ˜…

Collapse
 
thumbone profile image
Bernd Wechner

In the original article just use three backticks followed by py, as in:

```py
your python code here
```
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rohitbrk profile image
rohit-br-k

thank you for sharing this information...!!😊

Collapse
 
rohitbrk profile image
rohit-br-k

that's cool broπŸ˜‚

Collapse
 
cyebukayire profile image
Peace

You did great:) Keep it up

Collapse
 
rohitbrk profile image
rohit-br-k

Thank you bro...😊