Detailed description of a single-chain table of Python data structure; a single-chain python Data Structure
The example in this article shares the code of a single-chain table of Python data structure for your reference. The details are as follows:
# Node class Node (): _ slots __= ['_ item',' _ next'] # define the Node instance attribute 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 # single-chain table class SingleLinkedList (): def _ init _ (self): self. _ head = None # The initialization linked list is empty and always points to the head self of the linked list.. _ Size = 0 # linked list size # Return linked list size def size (self): current = self. _ head count = 0 while current! = None: count + = 1 current = current. getNext () return count # traversal chain table def travel (self): current = self. _ head while current! = None: print (current. getItem () current = current. getNext () # Check whether the linked list is empty def isEmpty (self): return self. _ head = None # add the element def add (self, item): temp = Node (item) at the front end of the linked list # create a new Node temp. setNext (self. _ head) # The newly created next Pointer Points to _ head self. _ head = temp # _ head points to the newly created pointer # Add the element def append (self, item): temp = Node (item) if self to the end of the linked list. isEmpty (): self. _ head = temp # insert else: current = self directly if the table is empty. _ head while current. get Next ()! = None: current = current. getNext () # traverse the list current. setNext (temp) # at this time, current is the last element of the linked list. insert it at the end. # Check whether the retrieved element is 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 # position of the index element in the table 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: return-1 # return-1 indicates no # delete an element def remove (self, item) in the table: 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 () # insert the element 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 <pos: count + = 1 pre = current. getNext () pre. setNext (temp) temp. setNext (current) if _ name __= = '_ main _': a = SingleLinkedList () for I in range (1, 10):. append (I) print ('linked table size',. size (). travel () print (. search (6) print (. index (5). remove (4). travel (). insert (1, 4,100). travel ()
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.