mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-21 21:34:28 -04:00
add link to the new revision of my book, organize the folders to add further examples
This commit is contained in:
parent
6d78f5e3fd
commit
aa5010594e
140 changed files with 13 additions and 15 deletions
src/examples_book/adt/queues
39
src/examples_book/adt/queues/queue.py
Normal file
39
src/examples_book/adt/queues/queue.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
|
||||
class Queue(object):
|
||||
''' a class for a queue '''
|
||||
def __init__(self):
|
||||
self.items = []
|
||||
|
||||
def isEmpty(self):
|
||||
return self.items == []
|
||||
|
||||
def enqueue(self, item):
|
||||
self.items.insert(0, item)
|
||||
|
||||
def dequeue(self):
|
||||
return self.items.pop()
|
||||
|
||||
def size(self):
|
||||
return len(self.items)
|
||||
|
||||
def peek(self):
|
||||
return self.items[-1]
|
||||
|
||||
|
||||
def main():
|
||||
queue = Queue()
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(2)
|
||||
queue.enqueue(3)
|
||||
print(queue.size())
|
||||
print(queue.peek())
|
||||
print(queue.dequeue())
|
||||
print(queue.peek())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue