-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack implementation using List
More file actions
124 lines (111 loc) · 3.21 KB
/
Stack implementation using List
File metadata and controls
124 lines (111 loc) · 3.21 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# A.STACKS:
# A-1:STACK IMPLEMENTATION USING ARRAYS(PTYHON'S LIST):
# #A-1-a:Dynamic implementation(stack can grow to any length)
class EmptyClassException(Exception):
pass
class Stack:
def __init__(self):
self.item=[]
def is_empty(self):
return self.item==[]
def stack_len(self):
return len(self.item)
def pop(self):
if self.is_empty():
raise EmptyClassException("Stack is Empty!!")
return self.item.pop()
def push(self,ele):
self.item.append(ele)
def peek(self):
if self.is_empty():
raise EmptyClassException("List is Empty!!")
return self.item[-1]
def display(self):
print(self.item)
if __name__=="__main__":
stack=Stack()
while True:
print("1.Push.")
print("2.Pop.")
print("3.Peek.")
print("4.Size.")
print("5.Display.")
print("6.Quit")
choice=int(input("Enter a choice: "))
if(choice==1):
ele=int(input("Enter the elements: "))
stack.push(ele)
elif(choice==2):
popped=stack.pop()
print("Popped element is : ",popped)
elif(choice==3):
print("Element at the top is : ",stack.peek())
elif(choice==4):
print("Size of the stack is: ",stack.stack_len())
elif(choice==5):
stack.display()
elif(choice==6):
break
else:
print("Invalid choice!!")
print()
# A-1-b:Static implementation(stack is fixed lengthed)
class StackEmptyError(Exception):
pass
class StackFullError(Exception):
pass
class Stack:
def __init__(self,max_len=10):
self.item=[None]*max_len
self.count=0
def size(self):
return self.count
def is_empty(self):
return self.item==0
def is_full(self):
return self.item==len(self.item)
def push(self,ele):
if self.is_full():
raise StackFullError("List is Full!!")
self.item[self.count]=ele
self.count+=1
def pop(self):
if self.is_empty():
raise StackEmptyError("List is Empty!!")
ele=self.item[self.count-1]
self.item[self.count-1]=None
self.count-=1
return ele
def peek(self):
if self.is_empty():
raise StackEmptyError("List is Empty!!")
return self.item[self.count-1]
def display(self):
print(self.item)
if __name__=="__main__":
stack=Stack()
while True:
print("1.Push.")
print("2.Pop.")
print("3.Peek.")
print("4.Size.")
print("5.Display.")
print("6.Quit")
choice=int(input("Enter a choice: "))
if(choice==1):
ele=int(input("Enter the elements: "))
stack.push(ele)
elif(choice==2):
popped=stack.pop()
print("Popped element is : ",popped)
elif(choice==3):
print("Element at the top is : ",stack.peek())
elif(choice==4):
print("Size of the stack is: ",stack.size())
elif(choice==5):
stack.display()
elif(choice==6):
break
else:
print("Invalid choice!!")
print()