This article briefly introduces the knowledge of the linked list, and then simulates a single-chain table using Python. It is relatively simple. For Beginners, refer to it, you can give me some suggestions for improvement. The linked list consists of a series of structures that do not need to be connected in the memory. These objects are sorted in linear order. Each structure contains table elements and pointers to subsequent elements. The pointer to the last unit points to NULL. To facilitate the delete and insert operations on the linked list, you can add a table header for the linked list.
The delete operation can be implemented by modifying a pointer.
The insert operation requires two pointer adjustments.
1. Implementation of one-way linked list
1.1 Node implementation
Each Node is divided into two parts. Some elements that contain a linked list can be called data fields. The other part is a pointer pointing to the next Node.
Class Node (): _ slots __= ['_ item',' _ next'] # define the Node instance attributes def _ init _ (self, item ): self. _ item = item self. _ next = None # the pointer of Node points to None def getItem (self) by default: return self. _ item def getNext (self): return self. _ next def setItem (self, newitem): self. _ item = newitem def setNext (self, newnext): self. _ next = newnext
1.2 Implementation of SinglelinkedList
Class SingleLinkedList (): def _ init _ (self): self. _ head = None # initialize the empty table self. _ size = 0
1.3 check whether the linked list is empty
def isEmpty(self): return self._head==None
1.4 add elements on the front end of the linked list
def add(self,item): temp=Node(item) temp.setNext(self._head) self._head=temp
1.5 append add elements at the end of the linked list
Def append (self, item): temp = Node (item) if self. isEmpty (): self. _ head = temp # If the table is empty, set the added element to the first element else: current = self. _ head while current. getNext ()! = None: current = current. getNext () # current. setNext (temp) of the traversal chain table # current is the last element of the linked list.
1.6 whether search elements are in the 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 position of index elements 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 delete an element from the 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()
1.9 insert elements in the insert linked 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 (self): 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 # initialize to an empty linked list def isEmpty (self): return self. _ head = None def size (self): curren T = 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 # If the table is empty, set the added element to the first element else: current = self. _ head while current. getNext ()! = None: current = current. getNext () # The current of the recurring list. setNext (temp) # current is the final element of the 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 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 our list' % 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. 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