Created
August 5, 2014 13:46
-
-
Save selfboot/39ca59ff3defad7c8207 to your computer and use it in GitHub Desktop.
Queue 的简单实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# This is a Queue implementing in list. | |
class Queue(): | |
def __init__(self): | |
self._content = list() | |
def __len__(self): | |
return len(self._content) | |
def _is_empty(self): | |
return len(self) == 0 | |
def __str__(self): | |
return str(self._content) | |
def enqueue(self, element): | |
self._content.append(element) | |
def dequeue(self): | |
assert not self._is_empty() | |
self._content = self._content[1:] | |
# The following shows how to use class Queue | |
if __name__ == "__main__": | |
queue_demo = Queue() | |
for i in range(5): | |
queue_demo.enqueue(i ** 4) | |
print "Original queue is:", queue_demo | |
print "Enter your choice:\n\ | |
1: enqueue a element;\n\ | |
2: dequeue a element:\n\ | |
3: print the queue;\n\ | |
EOF: exit the program." | |
choice = "3" | |
while(choice != EOFError): | |
try: | |
choice = raw_input("->") | |
if choice == "1": | |
queue_demo.enqueue(raw_input("Enter the element: ")) | |
print "After enqueue: ", queue_demo | |
elif choice == "2": | |
queue_demo.dequeue() | |
print "After dequeue: ", queue_demo | |
elif choice == "3": | |
print queue_demo | |
else: | |
print "Wrong input!" | |
continue | |
except EOFError: | |
print "Exit......" | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment