0% found this document useful (0 votes)
2 views

programs in python in data structure

The document contains multiple Python classes and functions for data structures including stacks, queues, and linked lists, along with operations like push, pop, enqueue, and dequeue. It also includes functions for converting infix expressions to postfix, evaluating postfix expressions, binary search, and polynomial addition. Each section provides a menu-driven interface for user interaction with the respective data structure.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

programs in python in data structure

The document contains multiple Python classes and functions for data structures including stacks, queues, and linked lists, along with operations like push, pop, enqueue, and dequeue. It also includes functions for converting infix expressions to postfix, evaluating postfix expressions, binary search, and polynomial addition. Each section provides a menu-driven interface for user interaction with the respective data structure.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

class stack():
def __init__(self):
self.items=[]

def is_empty(self):
return len(self.items)==0

def push(self,item):
self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()
else:
print("The stack is empty")

def peek(self):
if not self.is_empty():
return self.items[-1]
else:
print("Peek from an empty stack")

def size(self):
return len(self.items)

def display(self):
print(self.items)

#main
s=stack()
while True:
print("MENU")
print('1. Push\n 2. Pop\n 3. Peek\n 4. Size\n 5. Display\n 6. Exit\n')
c=int(input("Enter your choice:"))
if c==1:
item=input("Enter your item:")
s.push(item)
elif c==2:
print("The element to be popped:",s.pop())
elif c==3:
print("Top element is",s.peek())
elif c==4:
print("The size of the element is:", s.size())
elif c==5:
print("The final element to be displayed:", end=" ")
s.display()
elif c==6:
print("Exit")
break
2.
class queue:
def __init__(self):
self.items=[]

def is_empty(self):
return self.items==[]
def enqueue(self,items):
self.items.insert (0,item)

def dequeue(self):
if not self.is_empty():
return self.items.pop()
else:
print("The stack is empty")

def queue_first(self):
if not self.is_empty():
return self.items[-1]
else:
print("Queue first from an empty stack")

def size(self):
return len(self.items)

def display(self):
print(self.items)

s=queue()
while True:
print("MENU")
print("1.Enqueue")
print("2.Dequeue")
print("3.Size")
3.
class stack():
def __init__(self):
self.items=[]

def is_empty(self):
return len(self.items)==0

def push(self,item):
self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()
else:
print("The stack is empty")

def peek(self):
if not self.is_empty():
return self.items[-1]
else:
print("Peek from an empty stack")

def size(self):
return len(self.items)

def display(self):
print(self.items)

def infix_to_postfix(infix_expr):
prec={'(':0,'+':1,'-':1,'*':2,'/':2,'^':3}
op_stack=stack()
postfix_list=[]
token_list=infix_expr.split()
print(token_list)
for token in token_list:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789":
postfix_list.append(token)
elif token=='(':
op_stack.push(token)
elif token==')':
top_token=op_stack.pop()
while top_token!='(':
postfix_list.append(top_token)
top_token=op_stack.pop()
else:
while(not op_stack.is_empty())and(prec[op_stack.peek()]>=prec[token]):
postfix_list.append(op_stack.pop())
op_stack.push(token)
while not op_stack.is_empty():
postfix_list.append(op_stack.pop())
return " ".join(postfix_list)
infix_expr=input("Enter the infix expression with opening and closing parenthesis")
print(infix_expr)
print(infix_to_postfix(infix_expr))

4.
class stack():
def __init__(self):
self.items=[]

def is_empty(self):
return len(self.items)==0

def push(self,item):
self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()
else:
print("The stack is empty")

def peek(self):
if not self.is_empty():
return self.items[-1]
else:
print("Peek from an empty stack")

def size(self):
return len(self.items)

def display(self):
print(self.items)

def postfix_eval(postfix_expr):
required_stack=stack()
token_list=postfix_expr.split()
for token in token_list:
if token in '0123456789':
required_stack.push(token)
elif token in'+-/*':
op2=required_stack.pop()
op1=required_stack.pop()
result=do_math(token,op1,op2)
required_stack.push(result)
ans=required_stack.pop()
return ans
def do_math(token,op1,op2):
if token=='+':
return (int(op1)+int(op2))
if token=='-':
return (int(op1)-int(op2))
if token=='*':
return(int(op1)*int(op2))
if token=='/':
return(int(op1)/int(op2))
#main
postfix_expr=input("Enter the postfix expr:")
x=postfix_eval(postfix_expr)
print ("\n",x)

5.
def bin_search(arr,target):
low=0
high=len(arr)-1
while low<=high:
mid=(low+high)//2
if arr[mid]==target:
return mid
elif arr[mid]<target:
low=mid+1
else:
high=mid-1
#main
bin_search
arr=eval(input("Enter the elements:"))
target=int(input("Enter the target element:"))
index=bin_search(arr,target)
print("The target element is found at index:",index)

6.sll add front


class Node:
def __init__(self,init_data):
self.data=init_data
self.next=None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self,new_data):
self.data=new_data
def set_next(self,new_next):
self.next=new_next

temp=Node(56)
print(temp.get_data())
class unorderedlist:
def __init__(self):
self.head=None
def is_empty(self):
return self.head==None
def add(self,item):
temp=Node(item)
temp.next=self.head
self.head=temp
def display(self):
print("The current list of items:")
current=self.head
s_data=' '
while current !=None:
print(current.get_data())
s_data=s_data+str(current.get_data())+"->"
current=current.get_next()
print(s_data)

#main

my_list=unorderedlist()
my_list.add(18)
my_list.add(78)
my_list.add(67)
my_list.add(23)
my_list.display()

7.
class Node:
def __init__(self,init_data):
self.data=init_data
self.next=None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self,new_data):
self.data=new_data
def set_next(self,new_next):
self.next=new_next

temp=Node(56)
print(temp.get_data())

class unorderedlist:
def __init__(self):
self.head=None
def is_empty(self):
return self.head==None
def add(self,item):
temp=Node(item)
temp.next=self.head
self.head=temp
def add_rear(self,item):
temp=Node(item)
current=self.head
while current.get_next()!=None:
current=current.get_next()
current.next=temp
def display(self):
print("The current list of items:")
current=self.head
s_data=' '
while current !=None:
print(current.get_data())
s_data=s_data+str(current.get_data())+"->"
current=current.get_next()
print(s_data)

#main

my_list=unorderedlist()
my_list.add(18)
my_list.add(78)
my_list.add(67)
my_list.add(23)
my_list.add_rear(34)
my_list.add_rear(45)
my_list.add_rear(56)
my_list.add_rear(78)
my_list.display()
8.sll md

class Node:
def __init__(self,init_data):
self.data=init_data
self.next=None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self,new_data):
self.data=new_data
def set_next(self,new_next):
self.next=new_next

class unorderedlist:
def __init__(self):
self.head=None
def is_empty(self):
return self.head==None
def add_front(self,item):
temp=Node(item)
temp.next=self.head
self.head=temp
def add_rear(self,item):
temp=Node(item)
current=self.head
while current.get_next()!=None:
current=current.get_next()
current.next=temp

def del_front(self):
print("The current list of items:")
current=self.head.next
s_data=''
while current!=None:
print(current.get_data())
s_data=s_data+str(current.get_data())+"-->"
current=current.get_next()
print(s_data)
def del_rear(self):
current=self.head
previous=None
while current.get_next()!=None:
previous=current
current=current.get_next()
previous.set_next(None)
def remove(self,item):
current=self.head
previous=None
found=False
while current and not found:
if current.get_data()==item:
found=True
else:
previous=current
current=current.get_next()
if found:
if previous==None:
self.head=current.get_next()
else:
previous.set_next(current.get_next())
def search(self,item):
current=self.head
found=False
while current!=None and not found:
if current.get_data()==item:
found=True
else:
current=current.get_next()
return found
def display(self):
print("The current list of items:")
current=self.head
s_data=' '
while current !=None:
print(current.get_data())
s_data=s_data+str(current.get_data())+"->"
current=current.get_next()
print(s_data)

#main

s=unorderedlist()
while True:
print("\n Menu")
print("1.Add Front")
print("2.Add Rear")
print("3.Delete Front")
print("4.Delete Rear")
print("5.Remove Any item")
print("6.Search the element")
print("7.Display")
print("8.Exit")
n=int(input("Enter a choice:"))
if(n==1):
item=int(input("Enter the element to be added in the front:"))
s.add_front(item)
s.display()
elif(n==2):
item=int(input("Enter the element to be added in the rear:"))
s.add_rear(item)
s.display()
elif(n==3):
print(s.del_front())
elif(n==4):
s.del_rear()
s.display()
elif(n==5):
item=int(input("Enter the element to ba removed"))
s.remove(item)
s.display()

elif(n==6):
item=int(input("Enter the element to be searched"))
print(s.search(item))
elif(n==7):
s.display()
else:
print("Exit")
break

9.class node:
def __init__(self,coeff,deg):
self.coeff=coeff
self.deg=deg
self.next=None
def get_data(self):
return(self.coeff,self.deg)
def get_next(self):
return self.next
def set_next(self,new_next):
self.next=new_next

class polynomial:
def __init__(self):
self.head=None
def add_rear(self,item1,item2):
temp=node(item1,item2)
if self.head==None:
self.head=temp
else:
current=self.head
while current.get_next()!=None:
current=current.get_next()
current.next=temp
def display(self):
current=self.head
while current!=None:
p,v=current.get_data()
print(p,"x^",v,"+",end=" ")
current=current.get_next()
def add_poly(self,p1,p2):
a=p1.head
b=p2.head
poly3=polynomial()
while a!=None and b!=None:
if(a.deg==b.deg):
h=a.coeff+b.coeff
poly3.add_rear(h,a.deg)
a=a.get_next()
b=b.get_next()
elif a.deg>b.deg:
poly3.add_rear(a.coeff,a.deg)
a=a.get_next()
elif a.deg<b.deg:
poly3.add_rear(b.coeff,b.deg)
b=b.get_next()
while a!=None:
poly3.add_rear(a.coeff,a.deg)
a=a.get_next()
while b!=None:
poly3.add_rear(b.coeff,b.deg)
b=b.get_next()
poly3.display()
p1=polynomial()
x=int(input("no of item in first polynomial:"))
for i in range(1,x+1):
a,h=input("give the coeff of the degree:").split()
p1.add_rear(int(a),int(h))
p2=polynomial()
y=int(input("no of terms in second polynomial="))
for i in range(1,x+1):
b,g=input("give the coefficient of the degree").split()
p2.add_rear(int(b),int(g))
print('\n polynomial:\n',end=" ")
p1.display()
print('\n polynomial2:\n',end=" ")
p2.display()
print("\n addition:",end=" ")
p2.add_poly(p1,p2)

You might also like