A list consists of a series of structures that do not have to be connected in memory, and these objects are sorted in a linear order. Each structure contains table elements and pointers to subsequent elements. The pointer to the last cell points to null. To facilitate the deletion and insertion of the linked list, you can add a table header to the linked list.
The delete operation can be implemented by modifying a pointer.
The insert operation requires a two-step pointer adjustment.
1. The realization of one-way linked list
1.1 Node implementation
Each node is divided into two parts. Part of the element that contains the list, which can be called the data field, and the other part is a pointer to the next node.
Class Node (): __slots__=[' _item ', ' _next '] #限定Node实例的属性 def __init__ (self,item): Self._item=item Self._next=none #Node的指针部分默认指向None def getItem (self): return Self._item def getNext (self): return self._next def setitem (self,newitem): self._item=newitem def setnext (self,newnext): Self._next=newnext
The realization of 1.2 singlelinkedlist
Class Singlelinkedlist (): def __init__ (self): self._head=none #初始化链表为空表 self._size=0
1.3 Checking if the linked list is empty
def isEmpty (self):
1.4 add element in the front of the list
def add (self,item): temp=node (item) Temp.setnext (self._head) self._head=temp
1.5 Append adding elements to the tail of the list
def append (self,item): temp=node (item) if Self.isempty (): self._head=temp #若为空表, set the added element to the first element else: current=self._head while current.getnext ()!=none: current=current.getnext () #遍历链表 Current.setnext (temp) #此时current为链表最后的元素
1.6 Search retrieves whether the element is in a linked list
def search (Self,item): current=self._head Founditem=false while Current!=none and not Founditem: if Current.getitem () ==item: founditem=true Else: current=current.getnext () return Founditem
1.7 The position of the index element in the linked list
def index (self,item): current=self._head count=0 found=none while Current!=none and not found: Count+=1 if Current.getitem () ==item: found=true Else: current=current.getnext () if found: return Count else: raise ValueError, '%s is not in LinkedList '%item
1.8 Remove removes an element from a linked list
def remove (Self,item): current=self._head pre=none while current!=none: if current.getitem () = =item: If not pre: self._head=current.getnext () Else: pre.setnext (Current.getnext ()) Break Else: pre=current Current=current.getnext ()
Inserting elements into the 1.9 insert list
def insert (Self,pos,item): If Pos<=1:self.add (item) elif pos>self.size (): Self.append (item) else:temp= Node (item) count=1 Pre=none Current=self._head while Count
All code
Class Node (): __slots__=[' _item ', ' _next '] def __init__ (self,item): Self._item=item self._next=none def getItem (SE LF): Return Self._item def getNext (self): return self._next def setitem (Self,newitem): Self._item=newitem def Setnext (self,newnext): Self._next=newnext class Singlelinkedlist (): Def __init__ (self): Self._head=none #初始化为空 List def isEmpty (self): return self._head==none def size (self): Current=self._head count=0 while Current!=none : Count+=1 Current=current.getnext () return count Def travel (self): Current=self._head while current!= None:print Current.getitem () current=current.getnext () def add (Self,item): Temp=node (item) Temp.setnext ( Self._head) self._head=temp def append (self,item): Temp=node (item) if Self.isempty (): Self._head=temp #若为 An empty table that sets the added element to the first element else:current=self._head while Current.getnext ()!=none:current=current.getnext () #遍 Calendar link List Current.setnext(temp) #此时current为链表最后的元素 def search (self,item): Current=self._head founditem=false while Current!=none and not Founditem:if Current.getitem () ==item:founditem=true Else:current=current.getnext () return F Ounditem def index (self,item): Current=self._head count=0 Found=none while Current!=none and not found: Count+=1 if Current.getitem () ==item:found=true else:current=current.getnext () if found: Return count Else:raise valueerror, '%s is not in LinkedList '%item def remove (self,item): Current=self. _head Pre=none while Current!=none:if Current.getitem () ==item:if not pre:self._head=current. GetNext () Else:pre.setNext (Current.getnext ()) Break Else:pre=current Current=c Urrent.getnext () def insert (Self,pos,item): If Pos<=1:self.add (item) elif pos>self.size (): Self.append (item) Else:temp=node (item) count=1 Pre=none Current=self._head while Count