0% found this document useful (0 votes)
25 views4 pages

07-11-2022

The document defines a Node class to create nodes for a linked list with data and next pointer attributes. It also defines a LinkedList class with methods to add nodes, display the list, count nodes, and find a node by data. It creates sample linked lists, adds nodes to them, counts and finds nodes to demonstrate the class methods.

Uploaded by

Abhay Pratap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

07-11-2022

The document defines a Node class to create nodes for a linked list with data and next pointer attributes. It also defines a LinkedList class with methods to add nodes, display the list, count nodes, and find a node by data. It creates sample linked lists, adds nodes to them, counts and finds nodes to demonstrate the class methods.

Uploaded by

Abhay Pratap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Problem 1:

class Node:
def __init__(self,data):
self.__data=data
self.__next=None

def get_data(self):
return self.__data

def set_data(self,data):
self.__data=data

def get_next(self):
return self.__next

def set_next(self,next_node):
self.__next=next_node

class LinkedList:
def __init__(self):
self.__head=None
self.__tail=None

def get_head(self):
return self.__head

def get_tail(self):
return self.__tail

def add(self,data):
new_node=Node(data)
if(self.__head is None):
self.__head=self.__tail=new_node
else:
self.__tail.set_next(new_node)
self.__tail=new_node

def display(self):
temp=self.__head
while(temp is not None):
print(temp.get_data())
temp=temp.get_next()

#You can use the below __str__() to print the elements of the DS object while
debugging
def __str__(self):
temp=self.__head
msg=[]
while(temp is not None):
msg.append(str(temp.get_data()))
temp=temp.get_next()
msg=" ".join(msg)
msg="Linkedlist data(Head to Tail): "+ msg
return msg

list1=LinkedList()
list1.add("Sugar")
print("Element added successfully")
#Similarly add all the specified element(s)

Problem 2:

#lex_auth_012742478130135040816

class Node:
def __init__(self,data):
self.__data=data
self.__next=None

def get_data(self):
return self.__data

def set_data(self,data):
self.__data=data

def get_next(self):
return self.__next

def set_next(self,next_node):
self.__next=next_node

class LinkedList:
def __init__(self):
self.__head=None
self.__tail=None

def get_head(self):
return self.__head

def get_tail(self):
return self.__tail

def add(self,data):
new_node=Node(data)
if(self.__head is None):
self.__head=self.__tail=new_node
else:
self.__tail.set_next(new_node)
self.__tail=new_node

def display(self):
temp=self.__head
while(temp is not None):
print(temp.get_data())
temp=temp.get_next()

#You can use the below __str__() to print the elements of the DS object while
debugging
def __str__(self):
temp=self.__head
msg=[]
while(temp is not None):
msg.append(str(temp.get_data()))
temp=temp.get_next()
msg=" ".join(msg)
msg="Linkedlist data(Head to Tail): "+ msg
return msg

def count_nodes(biscuit_list):
count=0
# Write your logic here
temp=biscuit_list.get_head()
while(temp is not None):
count+=1
temp=temp.get_next()

return count

biscuit_list=LinkedList()
biscuit_list.add("Goodday")
biscuit_list.add("Bourbon")
biscuit_list.add("Hide&Seek")
biscuit_list.add("Nutrichoice")

print(count_nodes(biscuit_list))

Problem 3:

class Node:
def __init__(self,data):
self.__data=data
self.__next=None

def get_data(self):
return self.__data

def set_data(self,data):
self.__data=data

def get_next(self):
return self.__next

def set_next(self,next_node):
self.__next=next_node

class LinkedList:
def __init__(self):
self.__head=None
self.__tail=None

def get_head(self):
return self.__head

def get_tail(self):
return self.__tail

def add(self,data):
new_node=Node(data)
if(self.__head is None):
self.__head=self.__tail=new_node
else:
self.__tail.set_next(new_node)
self.__tail=new_node
def display(self):
temp=self.__head
while(temp is not None):
print(temp.get_data())
temp=temp.get_next()

def find_node(self,data):
temp=self.__head
while(temp is not None):
if(temp.get_data()==data):
return temp
else:
temp=temp.get_next()
return None

#You can use the below __str__() to print the elements of the DS object while
debugging
def __str__(self):
temp=self.__head
msg=[]
while(temp is not None):
msg.append(str(temp.get_data()))
temp=temp.get_next()
msg=" ".join(msg)
msg="Linkedlist data(Head to Tail): "+ msg
return msg

list1=LinkedList()
#Add all the required element(s)
list1.add("Milk")
list1.add("Salt")
list1.add("Biscuit")
list1.add("Apple")
list1.add("Juice")
list1.add("Pomegranate")
list1.add("Watermelon")

#Search for the required node


node=list1.find_node("Milk")
if(node!=None):
print("Node found")30

else:
print("Node not found")

You might also like