Python one-way loop linked list

Source: Internet
Author: User

Operation
    • Is_empty () Determine if the linked list is empty
    • Length () returns the lengths of the linked list
    • Travel () traversal
    • Add (item) adds a node to the head
    • Append (item) Adds a node to the tail
    • Insert (POS, item) adds a node at the specified location pos
    • Remove (item) deletes a node
    • Search (item) to find out if the node exists


ClassNode (object):"""node""" def __init__(self, item): Self.item=Item Self.next=NoneclassSincyclinkedlist (object):"""one-way circular linked list""" def __init__(self): Self._head=NonedefIs_empty (self):"""determine if the linked list is empty""" returnSelf._head = =NonedefLength (self):"""returns the length of the linked list""" #returns the length 0 if the linked list is empty ifself.is_empty ():return0 Count= 1cur=Self._head whileCur.next! =Self._head:count+ = 1cur=Cur.nextreturnCountdefTravel (self):"""traversing a linked list""" ifself.is_empty ():returncur=Self._headPrintCur.item, whileCur.next! =Self._head:cur=Cur.nextPrintCur.item,Print "" defAdd (Self, item):"""Adding a node to the head"""node=Node (item)ifself.is_empty (): Self._head=node Node.next=Self._headElse: #added nodes point to _headNode.next =Self._head#move to the tail of the list and point to the next nodeCur =Self._head whileCur.next! =Self._head:cur=Cur.next Cur.next=node#_head point to add nodeSelf._head =nodedefAppend (self, item):"""Trailing Add node"""node=Node (item)ifself.is_empty (): Self._head=node Node.next=Self._headElse: #move to the tail of the listCur =Self._head whileCur.next! =Self._head:cur=Cur.next#point the tail node to nodeCur.next =node#point node to head _headNode.next =Self._headdefInsert (self, POS, item):"""Add a node at the specified location""" ifPOS <=0:self.add (item)elifPOS > (Self.length ()-1): Self.append (item)Else: Node=Node (item) cur=Self._head Count=0#move to the previous position in the specified position whileCount < (pos-1): Count+ = 1cur=Cur.next Node.next=Cur.next Cur.next=nodedefRemove (self, item):"""Delete a node""" #If the linked list is empty, return directly ifself.is_empty ():return #Point cur to the head nodeCur =Self._head Pre=None#if the element of the head node is the element to be found, item ifCur.item = =Item:#If a linked list has more than one node ifCur.next! =Self._head:#Find the tail node and point next to the second node whileCur.next! =Self._head:cur=Cur.next#cur points to the tail nodeCur.next =Self._head.next Self._head=Self._head.nextElse: #A linked list has only one nodeSelf._head =NoneElse: Pre=Self._head#The first node is not the one you want to delete whileCur.next! =Self._head:#the element to delete was found ifCur.item = =Item:#DeletePre.next =Cur.nextreturn Else: Pre=cur cur=Cur.next#cur pointing to the tail node ifCur.item = =Item:#trailing DeletePre.next =Cur.nextdefSearch (self, item):"""find whether a node exists""" ifself.is_empty ():returnFalse cur=Self._headifCur.item = =Item:returnTrue whileCur.next! =Self._head:cur=Cur.nextifCur.item = =Item:returnTruereturnFalseif __name__=="__main__": LL=sincyclinkedlist () Ll.add (1) Ll.add (2) Ll.append (3) Ll.insert (2, 4) Ll.insert (4, 5) Ll.insert (0,6) Print "Length:", Ll.length () ll.travel ()PrintLl.search (3) PrintLl.search (7) Ll.remove (1) Print "Length:", Ll.length () ll.travel ()

Python one-way loop linked list

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.