Definition of linked list:
A linked list is a data structure composed of a group of data elements called nodes. Each node contains information about the node and the address pointing to the next node. Since each node contains URL Information, a variable can be used to access the entire node sequence. That is to say, a node contains two parts of information: one is used to store the value of the data element, which is called the information field, and the other is used to store the pointer of the next data element address, which is called the pointer field. The address of the first node in the linked list is stored in a separate node, which is called the header node or the first node. The last node in the linked list has no successor element, and its pointer field is null.
Python single-chain table implementation code:
Copy codeThe Code is as follows:
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Class Node (object ):
Def _ init _ (self, val, p = 0 ):
Self. data = val
Self. next = p
Class LinkList (object ):
Def _ init _ (self ):
Self. head = 0
Def _ getitem _ (self, key ):
If self. is_empty ():
Print 'linklist is empty .'
Return
Elif key <0 or key> self. getlength ():
Print 'the given key is error'
Return
Else:
Return self. getitem (key)
Def _ setitem _ (self, key, value ):
If self. is_empty ():
Print 'linklist is empty .'
Return
Elif key <0 or key> self. getlength ():
Print 'the given key is error'
Return
Else:
Self. delete (key)
Return self. insert (key)
Def initlist (self, data ):
Self. head = Node (data [0])
P = self. head
For I in data [1:]:
Node = Node (I)
P. next = node
P = p. next
Def getlength (self ):
P = self. head
Length = 0
While p! = 0:
Length + = 1
P = p. next
Return length
Def is_empty (self ):
If self. getlength () = 0:
Return True
Else:
Return False
Def clear (self ):
Self. head = 0
Def append (self, item ):
Q = Node (item)
If self. head = 0:
Self. head = q
Else:
P = self. head
While p. next! = 0:
P = p. next
P. next = q
Def getitem (self, index ):
If self. is_empty ():
Print 'linklist is empty .'
Return
J = 0
P = self. head
While p. next! = 0 and j <index:
P = p. next
J + = 1
If j = index:
Return p. data
Else:
Print 'Target is not exist! '
Def insert (self, index, item ):
If self. is_empty () or index <0 or index> self. getlength ():
Print 'linklist is empty .'
Return
If index = 0:
Q = Node (item, self. head)
Self. head = q
P = self. head
Post = self. head
J = 0
While p. next! = 0 and j <index:
Post = p
P = p. next
J + = 1
If index = j:
Q = Node (item, p)
Post. next = q
Q. next = p
Def delete (self, index ):
If self. is_empty () or index <0 or index> self. getlength ():
Print 'linklist is empty .'
Return
If index = 0:
Q = Node (item, self. head)
Self. head = q
P = self. head
Post = self. head
J = 0
While p. next! = 0 and j <index:
Post = p
P = p. next
J + = 1
If index = j:
Post. next = p. next
Def index (self, value ):
If self. is_empty ():
Print 'linklist is empty .'
Return
P = self. head
I = 0
While p. next! = 0 and not p. data = value:
P = p. next
I + = 1
If p. data = value:
Return I
Else:
Return-1
L = LinkList ()
L. initlist ([1, 2, 3, 4, 5])
Print l. getitem (4)
L. append (6)
Print l. getitem (5)
L. insert (4, 40)
Print l. getitem (3)
Print l. getitem (4)
Print l. getitem (5)
L. delete (5)
Print l. getitem (5)
L. index (5)
Result:
5
6
4
40
5
6