-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-structures-stack.py
84 lines (62 loc) · 1.53 KB
/
data-structures-stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# advance data structures
# stack
# implementing using list
# stack = []
# append() function to push
# element in the stack
# stack.append('g')
# stack.append('f')
# stack.append('g')
# print('initial stack')
# print(stack)
# pop() funtion to pop
# element from stack in
# LIFO
# print('\nelements popped from stack: ')
# print(stack.pop())
# print(stack.pop())
# print(stack.pop())
# print('\nstack after elements are popped')
# print(stack)
# uncommenting print(stack.pop())
# will cause an IndexError
# as the stack is now empty
# implementing using collectio.deque
from collections import deque
# stack = deque()
# append() function to push
# element in the stack
# stack.append('g')
# stack.append('f')
# stack.append('g')
# print('initial stack')
# print(stack)
# pop() funtion to pop
# element from stack in
# LIFO
# print('\nelements popped from stack: ')
# print(stack.pop())
# print(stack.pop())
# print(stack.pop())
# print('\nstack after elements are popped')
# print(stack)
# implementing using queue
from queue import LifoQueue
# initializing a stack
# stack = LifoQueue(maxsize=3)
# qsize() show the number of elements in the stack
# print(stack.qsize())
# put() function to push
# element in the stack
# stack.put('g')
# stack.put('f')
# stack.put('g')
# print('full:L ', stack.full())
# print('size: ', stack.qsize())
# get() function to pop element from stack in
# LIFI order
# print('\nelements popped from the stack')
# print(stack.get())
# print(stack.get())
# print(stack.get())
# print('\nEmpty: ', stack.empty())