-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_linkedlist.py
72 lines (59 loc) · 1.72 KB
/
stack_linkedlist.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
"""
This program implements stack using linkedlist
"""
# class to represent node
class StackNode:
# constructor to initialize a node
def __init__(self,data):
self.data = data
self.next = None #python uses None instead of NULL
class Stack:
# constructor to initialize root of linkedlist
def __init__(self):
self.root = None
def isEmpty(self):
if self.root is None:
print "-----------------------STACK EMPTY"
return True
else:
return False
def push(self,data):
newNode = StackNode(data)
newNode.next = self.root
self.root = newNode
print "%d pushed to stack" %(data)
def pop(self):
if self.isEmpty():
return float("-inf")
temp = self.root
self.root = self.root.next
popped = temp.data
print "%d poped from stack"%(popped)
return popped
def top(self):
if (self.isEmpty()):
return float("-inf")
print "top of stack - %d"%(self.root.data)
return self.root.data
try:
print "\n--------STACK---------\n"
S = Stack()
done = 1
while(done):
print "\n1.Push\n"
print "2.Pop\n"
print "3.IsEmpty\n"
print "4.Top\n"
choice = int(raw_input("\nEnter your choice- "))
if choice == 1:
data = int(raw_input("\nEnter your data- "))
S.push(data)
if choice == 2:
S.pop()
if choice == 3:
S.isEmpty()
if choice == 4:
S.top()
done = int(raw_input("\n Are you done- 0.Yes 1.No \n"))
except ValueError:
print "\nERROR: Enter Valid Inputs\n"