Python data structures and algorithms--linked list

Source: Internet
Author: User

The specific data structure can refer to the following two blog posts:

The implementation of a single linked list of Python data structures:

Http://www.cnblogs.com/yupeng/p/3413763.html

The implementation of a bidirectional linked list of Python data structures:

Http://www.cnblogs.com/yupeng/p/3413800.html

I only implemented the single-linked list type here, and the code is relatively streamlined:

First construct the class about the node:

1 class Node: 2     def __init__ (self,data=none,next=None): 3         Self.data = Data4         self.next = Next

The node has only two members, one is the data of the node, the other is the key value of the linked list, which is used to find the element, and the other refers to a reference to the next node.

Through the node, the following implements the list of classes, provides two methods of inserting and deleting in the list

1 classChain:2     def __init__(self):3Self.head =None4Self.tail =None5Self.count =06         7     defAppend (self,node):8         """append an element to the end of the list"""9Self.count + = 1Ten         ifSelf.head = =None: OneSelf.head =node ASelf.tail =Self.head -         Else: -Self.tail.next =node theSelf.tail =Self.tail.next -   -     def __repr__(self): -         """Refactoring : outputting a linked list""" +temp =Self.head -CStr ="" +          forIinchRange (self.count-1): ACStr + = str (temp.data) +' -' attemp =Temp.next -CStr + =Str (temp.data) -         returnCStr -  -     defInsert (self,n): -         """inserting elements in an ordered list in the data value of the INPUT element -         """ toSelf.count + = 1 +t = Self.head#The staging head pointer, not its reference -p =Node (n) the         ifT.data >N: *P.next = t#can be written p.next = Self.head $Self.head = P#can't write T=p .Panax Notoginseng         Else: -              whilet!=None: the                 ifT.next==noneorT.next.data >N: +P.next = T.next#Save the following node AT.next =P the                      Break +t =T.next -      $     defDelete (self,data): $         """To delete a specified element in a linked list - Enter the data value of the primitive - Note: If you have multiple identical elements, delete only one the         """ -t = Self.head#find from the beginning of the node to cache the parent node of the current nodeWuyi         ifself.head.data==Data: theSelf.head =Self.head.next -Self.count-= 1 Wu             returnTrue -  About          whilet.next!=None: $             ifT.next.data==data:#Locate the target node for the next node. -                 #means there's bound to be a next node. -T.next = T.next.next#The next node of the current node is straight down to the next node . -Self.count-= 1 A                 returnTrue +             Else: thet = T.next#keep looking for the next knot. -         Else: $             #While-else structure that invokes the else statement when the while loop does not encounter a break the             #No data found the             returnFalse

Test results:

if __name__=="__main__": L= [1,2,3,4,5] Chain=Chain () forIinchl:n=Node (i) chain.append (n) chain.insert (0)Printchain>>> 0, 1, 2, 3, 4, 5Chain.delete (3)#Delete Intermediate Data    Printchain>>> 0, 1, 2, 4, 5chain.delete (0)#Delete header Data    Printchain>>> 1, 2, 4, 5Chain.delete (5)#Delete tail data    Printchain>>> 1, 2-4

Python data structures and algorithms--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.