Program:
class node:
def __init__(self,data=None):
self.data=data
self.next=None
class linked_list:
def __init__(self):
self.head=node()
# Adds new node containing 'data' to the end of the linked list.
def append(self,data):
new_node=node(data)
cur=self.head
while cur.next!=None:
cur=cur.next
cur.next=new_node
print("\n\t--->>>",data,"is successfully added in the Node")
# Returns the length (integer) of the linked list.
def length(self):
cur=self.head
total=0
while cur.next!=None:
total+=1
cur=cur.next
return total
# Prints out the linked list in traditional Python list format.
def display(self):
elems=[]
cur_node=self.head
while cur_node.next!=None:
cur_node=cur_node.next
elems.append(cur_node.data)
print("\n\t--->>> Linked List-",elems,"\n")
# Returns the value of the node at 'index'.
def get(self,index):
if index>=self.length() or index<0: # added 'index<0' post-video
print("ERROR: 'Get' Index out of range!")
return None
cur_idx=0
cur_node=self.head
while True:
cur_node=cur_node.next
if cur_idx==index: return cur_node.data
cur_idx+=1
# Deletes the node at index 'index'.
def erase(self,index):
if index>=self.length() or index<0: # added 'index<0' post-video
print("ERROR: 'Erase' Index out of range!")
return
cur_idx=0
cur_node=self.head
while True:
last_node=cur_node
cur_node=cur_node.next
if cur_idx==index:
last_node.next=cur_node.next
return
cur_idx+=1
#object
L_list=linked_list()
#adding node
L_list.append(1)
L_list.append(2)
L_list.append(3)
L_list.append(4)
L_list.append(5)
#length of the Node
print(L_list.length())
#Displaying Linked List
L_list.display()
#Display Node at index 1
print(L_list.get(1))
#delete Node at index 1
L_list.erase(1)
#Displaying Linked List
L_list.display()
def __init__(self,data=None):
self.data=data
self.next=None
class linked_list:
def __init__(self):
self.head=node()
# Adds new node containing 'data' to the end of the linked list.
def append(self,data):
new_node=node(data)
cur=self.head
while cur.next!=None:
cur=cur.next
cur.next=new_node
print("\n\t--->>>",data,"is successfully added in the Node")
# Returns the length (integer) of the linked list.
def length(self):
cur=self.head
total=0
while cur.next!=None:
total+=1
cur=cur.next
return total
# Prints out the linked list in traditional Python list format.
def display(self):
elems=[]
cur_node=self.head
while cur_node.next!=None:
cur_node=cur_node.next
elems.append(cur_node.data)
print("\n\t--->>> Linked List-",elems,"\n")
# Returns the value of the node at 'index'.
def get(self,index):
if index>=self.length() or index<0: # added 'index<0' post-video
print("ERROR: 'Get' Index out of range!")
return None
cur_idx=0
cur_node=self.head
while True:
cur_node=cur_node.next
if cur_idx==index: return cur_node.data
cur_idx+=1
# Deletes the node at index 'index'.
def erase(self,index):
if index>=self.length() or index<0: # added 'index<0' post-video
print("ERROR: 'Erase' Index out of range!")
return
cur_idx=0
cur_node=self.head
while True:
last_node=cur_node
cur_node=cur_node.next
if cur_idx==index:
last_node.next=cur_node.next
return
cur_idx+=1
#object
L_list=linked_list()
#adding node
L_list.append(1)
L_list.append(2)
L_list.append(3)
L_list.append(4)
L_list.append(5)
#length of the Node
print(L_list.length())
#Displaying Linked List
L_list.display()
#Display Node at index 1
print(L_list.get(1))
#delete Node at index 1
L_list.erase(1)
#Displaying Linked List
L_list.display()
Expected O/P:
--->>> 1 is successfully added in the Node
--->>> 2 is successfully added in the Node
--->>> 3 is successfully added in the Node
--->>> 4 is successfully added in the Node
--->>> 5 is successfully added in the Node
5
--->>> Linked List- [1, 2, 3, 4, 5]
2
--->>> Linked List- [1, 3, 4, 5]
--->>> 2 is successfully added in the Node
--->>> 3 is successfully added in the Node
--->>> 4 is successfully added in the Node
--->>> 5 is successfully added in the Node
5
--->>> Linked List- [1, 2, 3, 4, 5]
2
--->>> Linked List- [1, 3, 4, 5]
0 Comments
Post a Comment