singly linked in python

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.

img

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:

view raw singly Linklist hosted with ❤ by GitHub

Insert node in Singly Link List:

def insertnode(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
self.head=Newnode;
obj.insertnode(int(input('enter the item==')));
view raw gistfile1.txt hosted with ❤ by GitHub

Add node in middle:

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()
view raw gistfile1.txt hosted with ❤ by GitHub

Add node in End:

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();
view raw gistfile1.txt hosted with ❤ by GitHub

Delete Node in Link List:

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()
view raw gistfile1.txt hosted with ❤ by GitHub

complete code:

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()
view raw gistfile1.txt hosted with ❤ by GitHub