Python/data_structures/queue/queue_on_list.py

53 lines
1.2 KiB
Python
Raw Normal View History

"""Queue represented by a Python list"""
2019-10-05 05:14:13 +00:00
class Queue:
2018-10-19 12:48:28 +00:00
def __init__(self):
self.entries = []
self.length = 0
2019-10-05 05:14:13 +00:00
self.front = 0
2018-10-19 12:48:28 +00:00
def __str__(self):
2019-10-05 05:14:13 +00:00
printed = "<" + str(self.entries)[1:-1] + ">"
2018-10-19 12:48:28 +00:00
return printed
"""Enqueues {@code item}
@param item
item to enqueue"""
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
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"""
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
def get(self):
self.length = self.length - 1
dequeued = self.entries[self.front]
2019-10-05 05:14:13 +00:00
# self.front-=1
# self.entries = self.entries[self.front:]
self.entries = self.entries[1:]
2018-10-19 12:48:28 +00:00
return dequeued
"""Rotates the queue {@code rotation} times
@param rotation
number of times to rotate queue"""
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
def rotate(self, rotation):
for i in range(rotation):
self.put(self.get())
"""Enqueues {@code item}
@return item at front of self.entries"""
2019-10-05 05:14:13 +00:00
def get_front(self):
2018-10-19 12:48:28 +00:00
return self.entries[0]
"""Returns the length of this.entries"""
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
def size(self):
return self.length