One-way circular linked list implementation in Python

Source: Internet
Author: User

Introduction

The so-called one-way circular list, but on the basis of a one-way list, such as Rattlesnake-like to connect its end and end, there are many similarities and must pay attention to the point. In particular, it may involve the operation of the head end node, not negligence.

What are the conditions for a lot of the traversal that must be done? And where should we stop?

What to do if the node to be deleted is the head or tail node when the delete operation is done? If the list has only one node, what should I do with it?

Code implementation
classNode (object):def __init__(self, value):#element FieldsSelf.value =value#link DomainSelf.next =NoneclassCircularlinkedlistoneway (object):def __init__(Self, node=None):#when constructing a non-empty chain, let its address field point to itself        ifNode:node.next=node self.__head=nodedefIs_empty (self):#The head node is not a none and is not empty        returnSelf.__head==Nonedef __len__(self): Count= 1cur= self.__head        ifself.is_empty ():return0 whileCur.next! = self.__head: Count+ = 1cur=Cur.nextreturnCountdefTraversal (self):ifself.is_empty ():returncur= self.__head         whileCur.next! = self.__head:            Print(cur.value) cur=Cur.next#cur is the tail node when exiting a loop        Print(Cur.value)defAdd (self, value):"""Head Insertion Method"""node=Node (value)ifSelf.is_empty (): Self.__head=node self.__head. Next = self.__head            returncur= self.__head         whileCur.next! = self.__head: Cur=Cur.next#the next pointer to the new node points to the original head nodeNode.next = self.__head        #point the new node to the head nodeSelf.__head=node#tail node next pointer to new head nodeCur.next = self.__head    defAppend (self, value): Node=Node (value) cur= self.__head        ifSelf.is_empty (): Self.__head=node self.__head. Next = self.__head            return         whileCur.next! = self.__head: Cur=Cur.next Node.next=Cur.next Cur.next=nodedefInsert (self, POS, value):ifPOS <=0:self.add (value)elifPOS > Len (self)-1: Self.append (value)Else: Node=Node (value) cur= self.__headCount=0 whileCount < Pos-1: Count+ = 1cur=Cur.next Node.next=Cur.next Cur.next=nodedefSearch (self, value):ifself.is_empty ():returnFalse cur= self.__head         whileCur.next! = self.__head:            ifCur.value = =Value:returnTrueElse: Cur=Cur.next#don't forget the tail node outside the while loop        ifCur.value = =Value:returnTruereturnFalsedefRemove (self, value): Cur= self.__headPrior=Noneifself.is_empty ():return         whileCur.next! = self.__head:            #node to delete if found            ifCur.value = =Value:#the node to be deleted is in the head                ifCur = = self.__head: Rear= self.__head                     whileRear.next! = self.__head: Rear=Rear.next self.__head=Cur.next Rear.next= self.__head                #the node to be deleted is in the middle                Else: Prior.next=Cur.next
# This is not a break to jump out of the loop, but a return of the Exit function, because it's done.return #If you haven't found it yet , Else: Prior=cur cur=Cur.next#the node to be deleted at the tail ifCur.value = =Value:#If there is only one element in the list, then the prior is None,next attribute will be error #just make the head element none at this point ifCur = = self.__head: Self.__head=NonereturnPrior.next= Cur.next

One-way circular linked list implementation in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.