Python implementation chain list (i)

Source: Internet
Author: User

Simple structure of single linked list, composed of nodes

Node implementation method We use classes to encapsulate

def __init__ (Self,item):    self.item=Item    self.next=none

Here we can refer to the operation of the list when we implement the operation of the linked table

Empty () checks if the length () check is empty () for travel () traversal add (), insert (), append (0 implementation added, add to head operation, insert () to specify the position, append () to implement the team tail operation. Search () Find node

Class Singlelinklist (Object):
def __init__ (self):
Initialization
Self._head=none

def empty (self):
#判断是否为空
Return Self.head==none

def length (self):
#判断长度
C=self._head
Cou=0
While C!=none:
Cou+=1
C=c.next
Return cou

def travel (self):
#遍历
C=self._head
While Cur!=none:
Print C.item
c = C.next

Python implementation chain list (i)

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.