Last active
March 22, 2016 10:24
-
-
Save SammyHerring/76bffedba940d2d26153 to your computer and use it in GitHub Desktop.
Stack (LIFO) and Queue (FIFO) Algorithmic Sorter
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
### Lists as Stacks (LIFO) | |
stack = ["a", "b", "c"] | |
# add an element to the end of the list | |
stack.append("e") | |
stack.append("f") | |
print (stack) | |
# pop operation | |
stack.pop() | |
print (stack) | |
# pop operation | |
stack.pop() | |
print (stack) | |
# push operation | |
stack.append("d") | |
print (stack) | |
### Lists as Queues (FIFO) | |
from collections import deque | |
dq = deque(['b','c','d']) | |
print (dq) | |
# adding an element to the right of the queue | |
dq.append('e') | |
print (dq) | |
# adding an element to the left of the queue | |
dq.appendleft('a') | |
print (dq) | |
# iterate over deque's elements | |
for elt in dq: | |
print(elt) | |
# pop out an element at from the right of the queue | |
dq.pop() | |
print (dq) | |
# pop out an element at from the right of the queue | |
dq.popleft() | |
print (dq) | |
# print as list | |
print (list(dq)) | |
# reversed list | |
print (list(reversed(dq))) | |
# empty the deque element | |
dq.clear() | |
### Pseudo Code | |
## Lists as Stacks (LIFO) | |
#stack <- ["a", "b", "c"] | |
## add an element to the end of the list | |
#stack.append("e") | |
#stack.append("f") | |
#OUTPUT (stack) | |
## pop operation | |
#stack.pop() | |
#OUTPUT (stack) | |
## pop operation | |
#stack.pop() | |
#OUTPUT (stack) | |
## push operation | |
#stack.append("d") | |
#OUTPUT (stack) | |
##Lists as Queues (FIFO) | |
#FROM collections IMPORT deque | |
#dq <- deque(['b','c','d']) | |
#OUTPUT (dq) | |
## adding an element to the right of the queue | |
#dq.append('e') | |
#OUTPUT (dq) | |
## adding an element to the left of the queue | |
#dq.appendleft('a') | |
#OUTPUT (dq) | |
## iterate over deque's elements | |
#FOR elt in dq: | |
# OUTPUT elt | |
## pop out an element at from the right of the queue | |
#ENDFOR | |
#dq.pop() | |
#OUTPUT (dq) | |
## pop out an element at from the right of the queue | |
#dq.popleft() | |
#OUTPUT (dq) | |
## OUTPUT as list | |
#OUTPUT (list(dq)) | |
## reversed list | |
#OUTPUT (list(reversed(dq))) | |
## empty the deque element | |
#dq.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment