This article mainly for you in detail the Python data structure of the linked list of related data, with a certain reference value, interested in small partners can refer to
Data structure is a science that must be mastered, many of the textbooks are used in the C language to achieve the linked list, because C has pointers, can be very convenient to control the memory, it is convenient to implement the linked list, other languages, it is not so convenient, there are a lot of analog linked list, but this time, I do not use the simulation linked list Because Python is a dynamic language, you can assign an object to a new variable directly.
Well, before I use Python, let's talk about the list. When we store a large wave of data, we use arrays very often, but when we do insert operations it is very troublesome to look at the following example, there is a bunch of data 1,2,3,5,6,7 we want to insert 4 between 3 and 5, what would we do if we used an array? Of course, after 5 of the data back one, and then insert 4, this is very troublesome, but if you use the linked list, I will directly between 3 and 5 inserted 4 on the line, listening is very convenient.
So what is the structure of the list? As the name implies, the list is, of course, like a chain, linked together by a node, forming a data chain.
The nodes of the linked list are structured as follows:
Data is custom, and next is the address of the next node.
The structure of the list is, head holds the address of the first node:
Next, we'll use Python to implement the linked list.
Python implementation linked list
First, the node class is defined:
Class node: ' Data: node-saved _next: Save the next Node Object ' def __init__ (self, data, Pnext=none): Self.data = data Self._next = Pnext def __repr__ (self): "" to define the character output of node, print output Data ' ' return str (self.data)
Then, define the linked list class:
List of links to include:
Property:
Linked list head: Head
Chain table Length:
Method:
Determine if empty: IsEmpty ()
def isEmpty (self): return (self.length = = 0
Add a node (added at the end of the list): Append ()
def append (self, dataornode): item = None if isinstance (Dataornode, Node): item = Dataornode Else: item = node (dataornode) if not self.head: self.head = Item self.length + = 1 else: node = Self.head while node._next: node = node._next Node._next = Item self.length + = 1
Delete a node: delete ()
#删除一个节点之后记得要把链表长度减一def Delete (self, index): if Self.isempty (): print "This chain table is empty." Return If index < 0 or index >= self.length: print ' error:out of index ' return #要注意删除第一个节点的情况 #如果有空的头节点就不用这样 #但是我不喜欢弄头节点 If index = = 0: self.head = self.head._next self.length-= 1 return< c13/> #prev为保存前导节点 #node为保存当前节点 #当j与index相等时就 #相当于找到要删除的节点 j = 0 node = self.head Prev = Self.head while Node._next and J < index: prev = node node = Node._next J + = 1 if j = = Index: prev._next = Node._next Self.length-= 1
Modify a node: Update ()
def update (self, Index, data): if Self.isempty () or index < 0 or index >= self.length: print ' Error:out o F index ' return j = 0 node = self.head while Node._next and J < index: node = Node._next J + = 1 If j = = Index: node.data = Data
Find a node: GetItem ()
def getItem (self, index): if Self.isempty () or index < 0 or index >= self.length: print "Error:out of index " return j = 0 node = self.head while Node._next and J < index: node = node._next J + = 1 re Turn Node.data
To find the index of a node: GetIndex ()
def getindex (self, data): j = 0 if Self.isempty (): print "This chain table is empty" return node = Self.head while node: if Node.data = = data: return J node = node._next J + = 1 if j = = Self . Length: print "%s not found"% str (data) return
Insert a node: Insert ()
def insert (self, Index, dataornode): if Self.isempty (): print "This chain Tabale is empty" return if Index < 0 or index >= self.length: print "Error:out of index" return item = None if isinstance (data Ornode, node): item = Dataornode Else: item = Node (Dataornode) If index = = 0: item._next = Self.head self.head = Item self.length + = 1 return j = 0 node = self.head prev = self.head While Node._next and J < index: prev = node node = Node._next J + = 1 If j = = Index: item . _next = node prev._next = Item self.length + = 1
Clear list: Clear ()
def clear (self): self.head = None self.length = 0