- LinkList
Circular linked list.
Circular linked list is linked list is connected to form Circular,there are no null at the end point,the singly linked list and doubly linked list is made a Circular linked list,Circular lists are useful in applications to repeatedly go around the list,in this image head and last node is point the same value,This is Circular linked list,
Code
class node():
def __init__(self,data=None):
self.data=data;
self.next=None;
class CircularLinkdList():
def __init__(self):
self.head=None;
def preappend(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
if self.head is None:
Newnode.next=Newnode;
else:
last=self.head;
while last.next != self.head:
last=last.next;
last.next=Newnode;
self.head=Newnode;
def append(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
if self.head is None:
self.head = node(newnode);
self.head.next=self.head;
else:
last=self.head;
while last.next != self.head:
last=last.next;
last.next=Newnode;
Newnode.next = self.head;
def AddMiddle(self,mid,newnode):
Newnode=node(newnode);
Newnode.next=mid.next;
mid.next=Newnode;
def AddLast(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
if self.head is None:
self.head =Newnode.next;
return;
else:
last=self.head;
while last.next != self.head:
last=last.next;
last.next=Newnode;
self.head=Newnode;
def show(self):
pri=self.head;
while pri:
print(pri.data);
pri=pri.next;
if pri==self.head:
break;
obj=CircularLinkdList();
obj.preappend(int(input('enter the element==>>')));
obj.preappend(int(input('enter the element==>>')));
obj.preappend(int(input('enter the element==>>')));
obj.preappend(int(input('enter the element==>>')));
obj.show();
obj.append(int(input('enter the element for insert==>>')));
obj.show();
obj.AddMiddle(obj.head.next,int(input('enter the element for add node middle==>')));
obj.show();
obj.AddLast(int(input('enter the element for add node last')));
obj.show();
Append None:
def append(self,newnode):
Newnode=node(newnode);
Newnode.next=self.head;
if self.head is None:
self.head = node(newnode);
self.head.next=self.head;
else:
last=self.head;
while last.next != self.head:
last=last.next;
last.next=Newnode;
Newnode.next = self.head;
Add node Middle:
def AddMiddle(self,mid,newnode):
Newnode=node(newnode);
Newnode.next=mid.next;
mid.next=Newnode;
Add node last
def AddMiddle(self,mid,newnode):
Newnode=node(newnode);
Newnode.next=mid.next;
mid.next=Newnode;
Display Node
def show(self):
pri=self.head;
while pri:
print(pri.data);
pri=pri.next;
if pri==self.head:
break;