-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedStack.py
77 lines (55 loc) · 1.52 KB
/
LinkedStack.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
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Stack:
def __init__(self):
self.head=None
def is_empty(self):
if self.head == None:
return True
else:
return False
def push(self,data):
if self.head == None:
self.head = Node(data)
else:
new_node=Node(data)
new_node.next = self.head
self.head=new_node
def pop(self):
if self.is_empty():
return None
else:
popped_node=self.head
self.head=self.head.next
popped_node.next=None
return popped_node.data
def peek(self):
if self.is_empty():
return None
else:
return self.head.data
def display(self):
iternode=self.head
if self.is_empty():
print("Stack Undeflow")
else:
while iternode != None:
print(iternode.data,end=" ")
iternode=iternode.next
if iternode != None:
print("->",end=" ")
return
if __name__=="__main__":
MyStack=Stack()
MyStack.push(11)
MyStack.push(22)
MyStack.push(33)
MyStack.push(44)
MyStack.display()
print("\n Top element is ",MyStack.peek())
MyStack.pop()
MyStack.pop()
MyStack.display()
print("\n Top element is ",MyStack.peek())