forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add Queue [python] * Fix Queue title in readme.md * Small code fix & reordering Queue line in readme.md * Resolving conflict in readme
- Loading branch information
1 parent
59f3727
commit af47a99
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Queue(object): | ||
|
||
def __init__(self): | ||
self._list = [] | ||
|
||
def count(self): | ||
return len(self._list) | ||
|
||
def is_empty(self): | ||
return self.count() == 0 | ||
|
||
def enqueue(self, item): | ||
self._list.append(item) | ||
|
||
def dequeue(self): | ||
try: | ||
return self._list.pop(0) | ||
except IndexError: | ||
raise IndexError('pop from empty stack') | ||
|
||
|
||
def main(): | ||
queue = Queue() | ||
n = 100 | ||
|
||
print('Empty queue: {0}'.format(queue.is_empty())) | ||
|
||
while queue.count() < 5: | ||
print('pushing elements: {0}'.format(n)) | ||
queue.enqueue(n) | ||
n = n + 100 | ||
|
||
print('Number of items: {0}'.format(queue.count())) | ||
print('Empty queue: {0}'.format(queue.is_empty())) | ||
|
||
while True: | ||
try: | ||
print('Removing element: {0}'.format(queue.dequeue())) | ||
except Exception as e: | ||
print('Exception: {0}'.format(e)) | ||
break | ||
|
||
print('Number of items: {0}'.format(queue.count())) | ||
print('Empty queue: {0}'.format(queue.is_empty())) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |