-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_LL.py
More file actions
74 lines (58 loc) · 1.65 KB
/
Stack_LL.py
File metadata and controls
74 lines (58 loc) · 1.65 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
#defining a Node class for linked list
class Node:
def __init__(self,data):
self.data=data
self.next=None
#defining a stack class
class Stack:
def __init__(self):
self.head=None
self.c=0
def push(self,data):
newnode=Node(data)
newnode.next=self.head
self.head=newnode
self.c+=1
def pop(self):
if self.head==None:
return -1
else:
a=self.head.data
self.head=self.head.next
self.c-=1
return a
def n(self): #count function
return self.c
def display(self):
start=self.head
while(start!=None):
print(start.data)
start=start.next
def isempty(self):
return self.head==None
def peek(self):
if self.head==None:
return -1
else:
return self.head.data
if __name__=='__main__':
s=Stack()
while(True):
op=input("ENTER OPERATION NAME IN UPPERCASE. PRESS 0 TO STOP: ")
if op=="PUSH":
n=int(input("ENTER UR DATA: "))
s.push(n)
elif op=="POP":
print(s.pop())
elif op=="DISPLAY":
s.display()
elif op=="PEEK":
print(s.peek())
elif op=="COUNT":
print(s.n())
elif op=="ISEMPTY":
print(s.isempty())
elif op=="0":
break
else:
print("IMPROPER OPERATION REQUEST!!!!")