File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Load diff This file was deleted.
Original file line number Diff line number Diff line change 1+ # !/usr/bin/env python3
2+
3+ # author: greyshell
4+ # description: stack using list
5+
6+
7+ def main ():
8+ """
9+ - python list is backed by dynamic array, so occasionally it needs to be resized when item is added / removed
10+ - it does not guarantee the stable O(1) push and pop
11+ - however, it provides the amortize time complexity is O(1) when the item is added / remove at the end of list
12+ - adding / removing from front is much slower and it takes O(n) time as shifting of existing elements are required
13+ - IndexError => pop from an empty list
14+ - no parallel processing support / it handles locking and unlocking
15+ :return:
16+ """
17+ s = list ()
18+ # push: O(1), stack grows from left to right
19+ s .append (9 )
20+ s .append (5 )
21+ s .append (3 )
22+ s .append (2 )
23+
24+ # display the stack elements
25+ print (f"display the stack elements: { s } " )
26+
27+ # peek, O(1)
28+ data = s [- 1 ]
29+ print (f"peek the stack top: { data } " )
30+
31+ # pop: O(1), stack shrinks from right to left
32+ data = s .pop ()
33+ print (f"popped: { data } " )
34+
35+ print (f"display the stack elements: { list (s )} " )
36+
37+
38+ if __name__ == '__main__' :
39+ main ()
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+
3+ # author: greyshell
4+ # description: stack using deque
5+
6+ from collections import deque
7+
8+
9+ def main ():
10+ """
11+ - deque is backed by doubly linked list
12+ - it that guarantees the stable O(1) push and pop
13+ - IndexError => pop from an empty list
14+ - no parallel processing support / it handles locking and unlocking
15+ :return:
16+ """
17+ s = deque ()
18+ # push: O(1), stack grows from left to right
19+ s .append (9 )
20+ s .append (5 )
21+ s .append (3 )
22+ s .append (1 )
23+
24+ # display the stack elements: convert the deque into list
25+ print (f"display the stack elements: { s } " )
26+ print (f"display the stack elements: { list (s )} " )
27+
28+ # peek, O(1)
29+ data = s [- 1 ]
30+ print (f"peek the stack top: { data } " )
31+
32+ # pop: O(1), stack shrinks from right to left
33+ data = s .pop ()
34+ print (f"popped: { data } " )
35+
36+ print (f"display the stack elements: { list (s )} " )
37+
38+
39+ if __name__ == '__main__' :
40+ main ()
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+
3+ # author: greyshell
4+ # description: stack using queue.LifoQueue
5+
6+ from queue import LifoQueue
7+
8+
9+ def main ():
10+ """
11+ - used for parallel computing / multi producer and consumer scenarios
12+ - locking and un-locaking needs to be implemented manually
13+ :return:
14+ """
15+ s = LifoQueue ()
16+ # push: O(1)
17+ s .put (9 )
18+ s .put (2 )
19+
20+ # check if the stack is empty
21+ print (f"{ s .empty ()} " )
22+
23+ # not found any method for peek and display stack elements
24+
25+ # pop: O(1)
26+ data = s .get_nowait () # not wait for producer to add the element if the stack is empty
27+ print (f"popped: { data } " )
28+
29+ data = s .get () # poped out the last stack element
30+ data = s .get () # waits for ever as the stack is empty, producer needs to add an element
31+ print (f"popped: { data } " )
32+
33+
34+ if __name__ == '__main__' :
35+ main ()
You can’t perform that action at this time.
0 commit comments