-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackLL.py
40 lines (37 loc) · 926 Bytes
/
stackLL.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
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Stack:
def __init__(self):
self.head=None
def is_empty(self):
return self.head is None
def push(self,data):
new_node=Node(data)
new_node.next=self.head
self.head=new_node
def pop(self):
if self.is_empty():
return None
popped = self.head.data
self.head=self.head.next
return popped
def peek(self):
if self.is_empty():
return None
return self.head.data
def display(self):
current = self.head
while current:
print(current.data,end="->")
current=current.next
print("None")
stack=Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.display()
print("popped:",stack.pop())
print("peek:",stack.peek())
stack.display()