-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
40 lines (40 loc) · 853 Bytes
/
Copy pathstack.py
File metadata and controls
40 lines (40 loc) · 853 Bytes
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,val):
self.data=val
self.next=None
class stack:
def __init__(self):
self.top=None
def push(self,val):
newnode=Node(val)
if self.top is None:
self.top=newnode
else:
newnode.next=self.top
self.top=newnode
def pop(self):
if self.top is None:
print("Stack is Empty")
poppped=self.top.data
self.top=self.top.next
return poppped
def peek(self):
if self.top is None:
print("Stack is empty")
return self.top.data
def display(self):
temp=self.top
while temp:
print(temp.data,end="-->")
temp=temp.next
s=stack()
n=int(input())
for i in range(n):
k=int(input())
s.push(k)
s.display()
dele=s.pop()
print("Deleted element",dele)
s.display()
peeek=s.peek()
print("Peek element",peeek)