Python single-Link list operation

Source: Internet
Author: User

Linked list is the most basic commonly used in the data structure, the single-linked list in C + + language is implemented by pointer operation, Python as object-oriented programming, can use to create a node class to implement the linked list, using the class's attribute reference to replace the pointer operation.

Below we create a node class and then write a few list operations, including create, insert, delete, output, etc.:

1 classNode (): # Initialize constructor2def __init__ (self,value,next=None):3Self.value=value4self.next=Next5   6 def creatlist (n):7     ifn<=0:  8         returnFalse9     ifn==1:  Ten         returnNode (1) # Only one node One     Else:   ARoot=node (1)   -tmp=Root -          forIinchRange2, n+1): # Adds a node to one thetmp.next=Node (i) -tmp=Tmp.next -     returnRoot # Returns the root node -    + def printlist (head): # Print List -p=Head +      whilep!=None: A Print P.value atp=P.next -    - def Listlen (head): # list Length -C=0   -p=Head -      whilep!=None: inc=c+1   -p=P.next to     returnC +    - def insert (head,n): # Inserts an element in front of n the     ifn<1or n>Listlen (head): *         return   $   Panax Notoginsengp=Head -      forIinchRange1, N-1): # loop four times to reach5   thep=P.next +A=raw_input ("Enter a value:")   AT=node (value=a) thet.next=P.next # Notice here +p.next=T -     returnHead # put 6 behind T and put it behind the original P $    $ def dellist (head,n): # Delete Linked list -     ifn<1or n>Listlen (head): -         returnHead theElif N is 1:   -Head=Head.next # Delete HeaderWuyi     Else:   thep=Head -          forIinchRange1, N-1):     Wup=P.next # loop reaches 2 times -q=P.next Aboutp.next=Q.next # put 5 in the back of 3 $     returnHead -    -    - def main (): APrint"Create a linklist"   +Head=creatlist (7)   the printlist (head) - Print $Print"___________________________"   the    theN1=raw_input ("Enter the index to insert")   then1=int(N1) the Insert (HEAD,N1) - printlist (head) in Print thePrint"___________________________"   the    AboutN2=raw_input ("Enter the index to delete")   theN2=int(n2) the dellist (HEAD,N2) the printlist (head) +    -    the if__name__=='__main__': Main () # main function call

The results of the operation are as follows:

run c:\\anaconda\\node.pycreate a linklist1234567___________________________enter the index to insert6Enter a value: About12345 About67___________________________enter the index to delete41235 About67

Reference: http://blog.csdn.net/u010786109/article/details/40650609

52988861

Python single-Link list operation

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.