- LinkList
Singly linked

Linked list is the most popular and popular concept in data structures and linked list is working like train, A linked list is a sequence of data structures, which are linked together through a link.
Originally linked list The head stores the value address and the value stores the next value address, this process is working continuously with the array item, this process depends on the array item, such as image,we are write the code, show how to create python programe in this concept,
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class node(): | |
def __init__(self,data=None): | |
self.data=data; | |
self.next=None; | |
class SinglyLinklist(): | |
def __init__(self): | |
self.head=None; | |
def display(self): | |
show=self.head; | |
while show: | |
print(show.data); | |
show=show.next; | |
obj=SinglyLinklist(); | |
obj.head=node(int(input('enter the item=='))); | |
e1=node(int(input('enter the item=='))); | |
e2=node(int(input('enter the item=='))); | |
e3=node(int(input('enter the item=='))); | |
e4=node(int(input('enter the item=='))); | |
obj.head.next=e1; | |
e1.next=e2; | |
e2.next=e3; | |
e3.next=e4; | |
print('----------'); | |
obj.display(); |
Insert node in Singly Link List:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insertnode(self,newnode): | |
Newnode=node(newnode); | |
Newnode.next=self.head; | |
self.head=Newnode; | |
obj.insertnode(int(input('enter the item=='))); |
Add node in middle:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def middleinsert(self,mid,newnode): | |
Newnode=node(newnode); | |
Newnode.next=mid.next; | |
mid.next=Newnode | |
obj.middleinsert(obj.head.next,int(input('enter the item in middle in list=='))); | |
obj.display() |
Add node in End:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def endinsert(self,newnode): | |
Newnode=node(newnode); | |
if self.head is None: | |
self.head=Newnode; | |
return True; | |
last=self.head; | |
while last.next is not None: | |
last=last.next; | |
last.next=Newnode; | |
#calling function | |
obj.endinsert(int(input('enter the number'))); | |
obj.display(); |
Delete Node in Link List:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def delete(self,remove): | |
Head=self.head; | |
if Head is not None: | |
if Head.data==remove: | |
self.head=Head.next; | |
Head=None; | |
return True; | |
while Head is not None: | |
if Head.data==remove: | |
break; | |
pre=Head; | |
Head=Head.next; | |
if Head==None: | |
return; | |
pre.next=Head.next; | |
Head=None; | |
#calling function | |
obj.delete(int(input('enter the number=='))); | |
obj.display() |
complete code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class node(): | |
def __init__(self,data=None): | |
self.data=data; | |
self.next=None; | |
class SinglyLinklist(): | |
def __init__(self): | |
self.head=None; | |
def Insert(self,newnode): | |
Newnode=node(newnode); | |
Newnode.next=self.head; | |
self.head=Newnode; | |
def middleinsert(self,mid,newnode): | |
Newnode=node(newnode); | |
Newnode.next=mid.next; | |
mid.next=Newnode | |
def endinsert(self,newnode): | |
Newnode=node(newnode); | |
if self.head is None: | |
self.head=Newnode; | |
return True; | |
last=self.head; | |
while last.next is not None: | |
last=last.next; | |
last.next=Newnode; | |
def delete(self,remove): | |
Head=self.head; | |
if Head is not None: | |
if Head.data==remove: | |
self.head=Head.next; | |
Head=None; | |
return True; | |
while Head is not None: | |
if Head.data==remove: | |
break; | |
pre=Head; | |
Head=Head.next; | |
if Head==None: | |
return; | |
pre.next=Head.next; | |
Head=None; | |
def display(self): | |
show=self.head; | |
while show: | |
print(show.data); | |
show=show.next; | |
obj=SinglyLinklist(); | |
obj.head=node(int(input('enter the item=='))); | |
e1=node(int(input('enter the item=='))); | |
e2=node(int(input('enter the item=='))); | |
e3=node(int(input('enter the item=='))); | |
e4=node(int(input('enter the item=='))); | |
obj.head.next=e1; | |
e1.next=e2; | |
e2.next=e3; | |
e3.next=e4; | |
print('----------'); | |
obj.display(); | |
obj.Insert(int(input('enter the item=='))); | |
obj.display() | |
obj.middleinsert(obj.head.next,int(input('enter the item in middle in list=='))); | |
obj.display() | |
obj.endinsert(int(input('enter the number'))); | |
obj.display(); | |
obj.delete(int(input('enter the number=='))); | |
obj.display() |