-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.stackUsingLinkedList.py
58 lines (46 loc) · 1.2 KB
/
7.stackUsingLinkedList.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.start = None
def delete_from_start(self):
if self.start is None:
return None
elif self.start.next is None:
data = self.start.data
self.start = None
return data
else:
data = self.start.data
self.start = self.start.next
return data
def add_to_start(self, data):
temp = Node(data)
temp.next = self.start
self.start = temp
class Stack:
def __init__(self):
self.L1 = LinkedList()
def push(self, data):
self.L1.add_to_start(data)
def pop(self):
data = self.L1.delete_from_start()
return data
def main():
S1 = Stack()
print('1.push \n2.pop \n3.Done')
flag = 1
while flag:
inp = input('Enter Your Option: ')
if inp == '1':
data = input('Enter data: ')
S1.push(data)
elif inp == '2':
data = S1.pop()
print('poped Data is: ', data)
else:
flag = 0
if __name__ == '__main__':
main()