mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-25 05:51:08 +00:00
7e26755140
Front variable is added so that dequeue can be done any number of times.Without front,it displays only self.entries[0] as dequeued.
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
"""Queue represented by a python list"""
|
|
class Queue():
|
|
def __init__(self):
|
|
self.entries = []
|
|
self.length = 0
|
|
self.front=0
|
|
|
|
def __str__(self):
|
|
printed = '<' + str(self.entries)[1:-1] + '>'
|
|
return printed
|
|
|
|
"""Enqueues {@code item}
|
|
@param item
|
|
item to enqueue"""
|
|
def put(self, item):
|
|
self.entries.append(item)
|
|
self.length = self.length + 1
|
|
|
|
|
|
"""Dequeues {@code item}
|
|
@requirement: |self.length| > 0
|
|
@return dequeued
|
|
item that was dequeued"""
|
|
def get(self):
|
|
self.length = self.length - 1
|
|
dequeued = self.entries[self.front]
|
|
self.front-=1
|
|
self.entries = self.entries[self.front:]
|
|
return dequeued
|
|
|
|
"""Rotates the queue {@code rotation} times
|
|
@param rotation
|
|
number of times to rotate queue"""
|
|
def rotate(self, rotation):
|
|
for i in range(rotation):
|
|
self.put(self.get())
|
|
|
|
"""Enqueues {@code item}
|
|
@return item at front of self.entries"""
|
|
def front(self):
|
|
return self.entries[0]
|
|
|
|
"""Returns the length of this.entries"""
|
|
def size(self):
|
|
return self.length
|