The basic elements of a single linked list are  2  individual , Data items and connection items . These two items are in  Python  Can be realized through objects and their properties .

 class Node:
def __init__ (self, data):
self.data = data
self.next = None def add (self, data):
p = self while p.next != None:
p = p.next p.next = Node (data) def subtract (self):
p = self if p.next == None:
print (r'Oh, I can't')
return while p.next.next != None:
p = p.next p.next = None def show (self):
p = self while p != None:
print (p.data, end=' ')
p = p.next
>>> t = Node (1)
>>> t.show ()
1
>>> t.add (2)
>>> t.show ()
1 2
>>> t.subtract ()
>>> t.show ()
1

But at first  add  This is how it works :

 def add (self, data):
p = self.next while p != None:
p = p.next p = Node (data)

In the end, the test failed , reflection ing

p  and  self  What's the relationship ? Reference or copy ?

I inquired about ,Python  All variables in are references .( There is another well-known sentence :Python  All data are objects in )

If you change the reference , branch 2 Consider two situations :

If the reference object is immutable :

Create new objects

If the reference object is a mutable object :

Modify the original object

And up there  add  invalid , Because  p = Node (data)  Directly solved the citation , Instead of modifying the original object

--data: 2015-05-25

Python Simple single linked list of more related articles

  1. python Realize data structure single linked list

    #python Realize data structure single linked list # -*- coding: utf-8 -*- class Node(object): """ node """ ...

  2. Python Single linked list of data structure

    Python Single linked list of data structure A single linked list has subsequent nodes , There is no preceding node . The following implementation : Create a single chain table Print single linked list Get the length of the single linked list Determine whether the single chain table is empty Insert data after single linked list Get the data of the specified location in the single linked list Get the specified element of single linked list ...

  3. python The realization of single linked list of data structure

    Definition of linked list : Linked list (linked list) Is a data structure composed of a set of data elements called nodes , Each node contains its own information and the address to the next node . Because each node contains address information that can be linked , So with one variable, it's just ...

  4. Python The linear table —— Single chain list

    1. Introduction to linear table A linear table is a linear structure , It is a finite sequence of zero or more data elements . The characteristic of linear table is in a sequence , Except for the head and tail elements , Each element has and only has a direct precursor , There is and only one direct successor , The sequence head element has no direct precursor ...

  5. python—— The realization of single linked list of data structure

    Definition of linked list : Linked list (linked list) Is a data structure composed of a set of data elements called nodes , Each node contains its own information and the address to the next node . Because each node contains addresses that can be linked Information , So with a variable ...

  6. Python And data structure [0] -> Linked list /LinkedList[0] -> Single linked list and single linked list with header Python Realization

    Single chain list / Linked List Catalog Single chain list Single linked list with header Linked list is a basic linear data structure , stay C In language , This data structure is implemented through pointers , Because storage space does not require continuity , So the insert and delete operations will be very fast . The following will benefit ...

  7. python Data structure and algorithm —— Linked list

    Specific data structure can refer to the following two blogs : python The realization of single linked list of data structure : http://www.cnblogs.com/yupeng/p/3413763.html python Two way data structure ...

  8. data structure : Single linked list structure string (python edition ) Three new features have been added

    #!/urs/bin/env python # -*- coding:utf-8 -*- # Exception class class stringTypeError(TypeError): pass # Node class class ...

  9. data structure : Single linked list structure string (python edition ) improvement

    The purpose of this article is replace Realize multiple string class matching , But there are still some shortcomings . because python String objects are invariant objects , therefore replace Method does not modify the original string , Instead, it returns the modified string . This string object uses a single linked list ...

Random recommendation

  1. matrix_world_final_2012

    B http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98759#problem/B The question : The bottle is lying on its side , Where is the bottom of the bottle xlow, The mouth of the bottle is xhigh ...

  2. ckeditor Word limit

    var maxlength = 200; _editor=CKEDITOR.replace("ckeditor",{height:'130px'}); _editor.on('ke ...

  3. LF will be replaced by CRLF The solution to the problem

    [GIT] warning: LF will be replaced by CRLF The solution to the problem development environment : operating system : windows xp ruby 1.9.2 rails 3.1.3 git v ...

  4. node.js( 6、 ... and ) UTIL modular

    1.inspect The basic usage of function util.inspect(object,[showHidden],[depth],[colors]) Is a function that converts any object to a string , Usually used for debugging and error output . It's at least ...

  5. Oracle 11g XE yes Oracle A free version of the database

    Oracle 11g XE yes Oracle A free version of the database , Supports most of the features of the Standard Version ,11g XE Provide Windows and Linux edition . As free Oracle Database version ,XE The restrictions are ...

  6. noi 1.8 11 Image rotation

    The water problem doesn't explain Actually, I'm lazy Direct output ,,,,,,, personal QQ:757394026 The team QQ:466373640 Personal blog :www.doubleq.winc++/noi/ Informatics Olympiad blog :http://www. ...

  7. java The beauty of code (10)---Java8 Map Medium computeIfAbsent Method

    Map Medium computeIfAbsent Method Map Interface implementation classes such as HashMap,ConcurrentHashMap,HashTable And so on , In this way, under specific needs , Make your code more concise . ...

  8. wireless DOS attack

    1. Wireless connection status IEEE 802.11 A client state mechanism is defined , Used to track workstation authentication and association status . Wireless client and AP be based on IEEE Standards implement this state mechanism . Successfully associated customer stations stay in the State 3, To communicate wirelessly . It's about ...

  9. LeetCode How to solve the problem Longest Palindromic Substring

    1. Title Description 2. Problem analysis Calculate the palindrome substring of each character string . 3. Code string longestPalindrome(string s) { ; ; bool is_odd = false ...

  10. RF The feature subset selection strategy based on (spark ml)

    Supports continuous variables and category variables , A category variable is an attribute with three values ,a,b,c, Need to use Feature Transformers Medium vectorindexer Handle It's a bunch of parameters setMaxDepth: Maximum tree depth ...