'How to dequeue and display queue?
I have learn python for a while and I have some questions. How can I dequeue and display queue? Also, other function may have some problem. Here is my code. I am using google colab.
class Queue:
def __init__(self) :
# implement a queue with an array
self.stack = []
self.size = 0
def enqueue(self, data):
# insert data at the end of queue
self.stack.append(data)
self.size += 1
def dequeue(self):
# take out data at the beginning of the queue
if(self.size > 0):
self.size -= 1
return self.stack.dequeue
else:
return("")
# remove it and return the data taken out
def display(self):
# show all the data in the queue
Solution 1:[1]
There you go
class Queue:
def __init__(self):
# implement a queue with an array
self.stack = []
self.size = 0
def enqueue(self, data):
# insert data at the end of queue
self.stack.append(data)
self.size += 1
def dequeue(self):
# take out data at the beginning of the queue
if(self.size > 0):
self.size -= 1
return self.stack.pop(0)
else:
return None
# remove it and return the data taken out
def display(self):
# show all the data in the queue
if(self.size > 0):
for i in range(self.size):
print(self.stack[i])
q = Queue()
for i in range(3):
q.enqueue(i)
q.display()
print('dequeue : ',q.dequeue())
q.display()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dj0ulo |
