In: Computer Science
in python please
Q1) Create a Singly link list and write Python Programs for the following tasks: a. Delete the first node/item from the beginning of the link list b. Insert a node/item at the end of the link list c. Delete a node/item from a specific position in the link list
Q2) Create a Singly link list and write a Python Program for the following tasks: a. Search a specific item in the linked list and return true if the item is found and display the node position otherwise return false. b. Reverse the link list.
Python code
Q1)
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class PLinkedList:
def __init__(self):
self.head = None
# Function to delete node at first
def DeleteAtFirst(self):
if ( self.head is None):
print('No node')
else:
temp=self.head
self.head=self.head.next
temp=None
print('Deleted')
# Function to insert newnode at end
def InsertAtEnd(self, newdata):
NewNode = Node(newdata)
if self.head is None:#no node in the list
self.head = NewNode
return
temp = self.head
while(temp.next):#traverse the list up to the end of the node
temp = temp.next
temp.next=NewNode
# Function to remove node at given poistion
def DeleteNodeAtpos(self, pos):
temp = self.head
count=0 # finding position
if (self.head is not None and pos==1):# first node, positon=1
self.head = self.head.next #assign first node to next
temp = None
print('Deleted')
return
prev=self.head
flag=0
while (temp is not None):
count=count+1
if count == pos:
prev.next=temp.next
temp=None
print('Deleted')
flag=1
break
prev = temp
temp = temp.next
if flag==0:
print('No node at this position')
# Function to display the linked list
def Listdisplay(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next
Plist = PLinkedList()
Plist.InsertAtEnd(5)
Plist.InsertAtEnd(6)
Plist.InsertAtEnd(7)
Plist.Listdisplay()
Plist.DeleteAtFirst()
Plist.Listdisplay()
Plist.DeleteNodeAtpos(2)
Plist.Listdisplay()
Plist.DeleteNodeAtpos(2)
output
5
6
7
Deleted
6
7
Deleted
6
No node at this position
screenshort of output
Q2)
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class PLinkedList:
def __init__(self):
self.head = None
# Function to insert newnode at end
def InsertAtEnd(self, newdata):
NewNode = Node(newdata)
if self.head is None:#no node in the list
self.head = NewNode
return
temp = self.head
while(temp.next):#traverse the list up to the end of the node
temp = temp.next
temp.next=NewNode
# This Function checks whether the value present in the linked
list
def Listsearch(self, val):
# Initialize temp to head
temp = self.head
count=0
# loop till temp not equal to None
while temp != None:
count=count+1
if temp.data == val:
print('Found at position',count)
return True # data found
temp = temp.next
return False # Data Not found
# Function to reverse the linked list
def Reverselist(self):
prev = None
temp = self.head
while(temp is not None):
next = temp.next
temp.next = prev
prev = temp
temp = next
self.head = prev
# Function to display the linked list
def Listdisplay(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next
Plist = PLinkedList()
Plist.InsertAtEnd(5)
Plist.InsertAtEnd(6)
Plist.InsertAtEnd(7)
Plist.Listdisplay()
if Plist.Listsearch(6)==False:
print('Not Found')
Plist.Reverselist()
print('List after reverse')
Plist.Listdisplay()
screenshort of output
Q1
Q2