Linked_List with Python

#this is the part-B of c_b_i ,this module will insert a new node at the begining of head node
#this is node class
class Node:
def __init__(self,data):
self.data=data
self.next=None
#this is linkedlist class which takes all the data from node class
class link_list:
def __init__(self):
self.head=None



#this function is the main function which creates node by the user input

def print(self):
if self.head is None:
#it is first node
n_1=input("enter input for head node::")

self.head = Node(n_1)
#it is second node
n_2=input("enter input for node_2::")
second=Node(n_2)
#it is third node
n_3 = input("enter input for node_3::")
third=Node(n_3)
#now creat ing link from fist node to second node
self.head.next=second
#now creating link from second node to third
second.next=third









#to insert a element in the begining of a node
def insert_at_begining(self):
# assigning temperory head value in temp and using loop for traversing all the node and its data
t_i = input("enter value for a node that will insert before the head node::")
new_node = Node(t_i)
new_node.next = self.head
self.head = new_node
temp = self.head
print(temp)






#to delete a node
def deleting_node(self):

key=input("enter value for key to be deleted:::")
temp = self.head
# If head node itself holds the key to be deleted
if (temp is not None):
if (temp.data == key):
self.head = temp.next
temp = None
return

# Search for the key to be deleted, keep track of the
# previous node as we need to change 'prev.next'
while (temp is not None):
if temp.data == key:
break
prev = temp
temp = temp.next

# if key was not present in linked list
if (temp == None):
return

# Unlink the node from linked list
prev.next = temp.next

temp = None







#this function move the last element into the initial position
def moveToFront(self):
tmp = self.head
sec_last = None # To maintain the track of
# the second last node

# To check whether we have not received
# the empty list or list with a single node
if not tmp or not tmp.next:
return

# Iterate till the end to get
# the last and second last node
while tmp and tmp.next:
sec_last = tmp
tmp = tmp.next

# point the next of the second
# last node to None
sec_last.next = None

# Make the last node as the first Node
tmp.next = self.head
self.head = tmp





#adding element in the end of linklist
def add_last(self):
v_1=input("enter a value for a node that will be in the End:::")

temp = self.head
# 1. Create a new node
# 2. Put in the data
# 3. Set next as None
new_node=Node(v_1)

# 4. If the Linked List is empty, then make the
# new node as head
if self.head is None:
self.head = new_node
return

# 5. Else traverse till the last node
last = self.head
while (last.next):
last = last.next

# 6. Change the next of last node
last.next = new_node




#this function replace the selected element with the user input
def rep(self):
v_1 = input("select value:::")

v_2=input("enter new value:::")

temp = self.head
while temp.next is not None:
if temp.data==v_1:
temp.data=v_2
temp=temp.next







#it display the final output
def display(self):
temp = self.head
while (temp):
print("--->",temp.data)
temp = temp.next


if __name__=="__main__":
obj=link_list()

obj.print()
obj.display()
obj.insert_at_begining()

obj.display()
obj.deleting_node()
obj.display()
obj.moveToFront()
print("moving last element to front of link_list")
obj.display()
obj.add_last()
obj.display()
obj.rep()
obj.display()







 

Comments