-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
49 lines (43 loc) · 1.14 KB
/
stack.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
#IMPLEMENTATION USING ARRAY
class stack:
stack = list()
top = -1
def push(self,item):
self.top=self.top+1
self.stack.append(item)
print('item inserted at the top')
def pop(self):
if(self.top==-1):
print("stack is empty")
else:
print(self.stack[self.top],'\n')
self.top=self.top-1
def display(self):
while(self.top!=-1):
self.pop()
#IMPLEMENTATION USING LINKEDLIST
class node:
def __init__(self,data=None,next=None):
self.data = data
self.next = next
class stack:
head = None
def push(self,data):
newnode = node()
newnode.data = data
if(self.head==None):
self.head = newnode
else:
newnode.next = self.head
self.head = newnode
def pop(self):
if(self.head==None):
print('stack is empty')
else:
print(self.head.data,'\n')
self.head = self.head.next
def display(self):
if(self.head==None):
print('stack is empty')
while(self.head!=None):
self.pop()