This program is stacks using linked list. the program covers the following funtions:
1.Push
2.Pop
3.Display
Source Code:
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Stackll:
def __init__(self):
self.start=None
self.top=None
def push(self):
data=int(input("enter data"))
newnode=Node(data)
if self.start==None:
self.start=newnode
self.top=newnode
else:
temp=self.start
while temp.next!=None:
temp=temp.next
temp.next=newnode
top=newnode
def pop(self):
temp=self.start
if temp.next ==None:
print ("last element of stack deleted is %d" %(temp.data))
self.start=None
del self.top
self.top=None
else:
temp=self.start
prev=self.start
while temp.next != None:
prev=temp
temp=temp.next
prev.next=None
del temp
def display(self):
if self.top==None:
print("stack empty")
else:
temp=self.start
print("elements in stack are")
while temp!=None:
print ("%d" %(temp.data))
temp=temp.next
def menu():
print("1.push\n2.pop\n3.display\n4.quit")
def stop():
print("you are about to terminate the program")
exit(0)
s=Stackll()
def default():
print("check your input")
menu()
while True:
menu= {
1: s.push,
2: s.pop,
3: s.display,
4: stop}
option = int(input("Please enter your choice"))
menu.get(option,default)()
Output:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
1.push
2.pop
3.display
4.quit
Please enter your choice1
enter data10
Please enter your choice1
enter data20
Please enter your choice1
enter data30
Please enter your choice3
elements in stack are
10
20
30
Please enter your choice2
Please enter your choice3
elements in stack are
10
20
Please enter your choice2
Please enter your choice3
elements in stack are
10
Please enter your choice2
last element of stack deleted is 10
Please enter your choice3
stack empty
Please enter your choice4
Now my new Blog on Fundamentals of Python can be found at https://fundamentalsofpython.blogspot.com/2020/02/list-manipulations.html
1.Push
2.Pop
3.Display
Source Code:
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Stackll:
def __init__(self):
self.start=None
self.top=None
def push(self):
data=int(input("enter data"))
newnode=Node(data)
if self.start==None:
self.start=newnode
self.top=newnode
else:
temp=self.start
while temp.next!=None:
temp=temp.next
temp.next=newnode
top=newnode
def pop(self):
temp=self.start
if temp.next ==None:
print ("last element of stack deleted is %d" %(temp.data))
self.start=None
del self.top
self.top=None
else:
temp=self.start
prev=self.start
while temp.next != None:
prev=temp
temp=temp.next
prev.next=None
del temp
def display(self):
if self.top==None:
print("stack empty")
else:
temp=self.start
print("elements in stack are")
while temp!=None:
print ("%d" %(temp.data))
temp=temp.next
def menu():
print("1.push\n2.pop\n3.display\n4.quit")
def stop():
print("you are about to terminate the program")
exit(0)
s=Stackll()
def default():
print("check your input")
menu()
while True:
menu= {
1: s.push,
2: s.pop,
3: s.display,
4: stop}
option = int(input("Please enter your choice"))
menu.get(option,default)()
Output:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
1.push
2.pop
3.display
4.quit
Please enter your choice1
enter data10
Please enter your choice1
enter data20
Please enter your choice1
enter data30
Please enter your choice3
elements in stack are
10
20
30
Please enter your choice2
Please enter your choice3
elements in stack are
10
20
Please enter your choice2
Please enter your choice3
elements in stack are
10
Please enter your choice2
last element of stack deleted is 10
Please enter your choice3
stack empty
Please enter your choice4
Now my new Blog on Fundamentals of Python can be found at https://fundamentalsofpython.blogspot.com/2020/02/list-manipulations.html
No comments:
Post a Comment