Define a one-way linked list in Python3

Source: Internet
Author: User

A linked list is made up of nodes, a pointer represents a direction, and if a node that makes up a list contains only one pointer, the linked list is a one-way list.

Nodes in a one-way list have not only pointer variables that represent directions, but also value variables. So we define the linked list, that is, to define the nodes in the list, the operation of the linked list is finally the operation of the node.

These nodes, which contain data, are connected under a given structure and become a data structure-one-way linked list. The above is my understanding of one-way linked list.

Here is an implementation of the data structure that I use Python3 for a one-way list:

" "Python3 version One-way linked list-one-way chain list is simply the basic operation contained in a single-linked list list: Initialize create generate print display call procedure calculate length null get delete insert Modify Append" "classNode (object):#Node Initialization    def __init__(Self, value, p=None): Self.value=value Self.next=Pclasslinklist (object):#initializing a single-linked list    def __init__(self): Self.head=None#Create a single linked list    defCreate (self, value): Self.head=Node (value[0]) p=Self.head forIinchValue[1:]: P.next=Node (i) P=P.next#generate a single linked list    def_yield (self): P=Self.head whileP! =None:yieldP.value P=P.next#print a single linked list    def Print(self):Print([I forIinchSelf._yield ()]) #show a single linked list before and after a method call    defShow (func):defWrapper (Self, *args):Print("method {Func_name} before execution". Format (Func_name=func.__name__) Self .Print()            Print("method {Func_name} in execution". Format (Func_name=func.__name__) func (self,*args)Print("after the method {Func_name} executes". Format (Func_name=func.__name__) Self .Print()        returnwrapper#get the length of a single linked list    defLength (self): P=self.head Length=0 whileP! =None:length+ = 1P=P.nextreturnlength#determine if a single-linked list is empty    defIs_null (self):returnSelf.length () = =0#gets the single-linked-list offset element to return and print its node value    #supports sequential and reverse index: 0 for index One, 1 for the last digit, 2 for the second-lowest    #gets the non-existent bit returns none    defget (self, offset): P=Self.head Index=0 Length=self.length ()ifOffset > Length-1:            Print(None)returnNoneifOffset < 0 andOffset + Length <0:Print(None)returnNoneifOffset < 0 andOffset + length >=0:offset= length +Offset whileIndex < Length-1 andIndex <offset:p=P.next Index+ = 1Print("get index {index} bit node-value = {value}". Format (Index=index, value=p.value))returnP#Delete single-linked list offset elements and print    #supports sequential and reverse index: 0 for the first, 1 for the penultimate, 2 for the second    #Delete non-existent bit returns none@showdefRemove (self, offset): P=self.head Length=self.length () index=0ifOffset > Length-1:            Print(None)returnNoneifoffset = = 0orOffset + length = =0:Print("Delete Index {index} bit node-value = {value}". Format (Index=index, value=self.head.value)) Self.head=P.nextreturnNoneifOffset < 0 andLength + offset >0:offset= length +Offset whileIndex < Length-1 andIndex < Offset-1: P=P.next Index+ = 1Print("Delete Index {index} bit node-value = {value}". Format (Index=index + 1, value=p.next.value)) P.next=P.next.next#Insert node at specified index bit-value    #What is insert--join between two nodes is called Insert    #so inserting at the end means inserting between the second and last digits of the index.@showdefInsert (self, offset, value): Length=self.length ()#if the offset corresponds to an index bit that is not in the list corresponding to the index bit range-returns none        ifOffset > Length-1orOffset + Length <0:returnNoneifOffset <0:offset= offset +Length Node=Node (value)ifoffset = = 0orOffset + length = =0:p=Self.head Self.head=node Node.next=PElse: Previous_node= Self.get (offset-1) Current_node=self.get (offset) Previous_node.next=node Node.next=Current_nodePrint("Insert node at index {index} bit-value = {value}". Format (Index=offset, value=value)) #Append one node at the bottom of the list index-value@showdefAppend (self, value): Last_node= Self.get (Self.length ()-1) Last_node.next=Node (value)#To modify the value of a linked list index bit node@showdefModify (self, offset, value): Self.get (offset). Value=valueif __name__=='__main__': List= [1, 2, 33, 4, 55, 6, 76, 78]    #Initialize the list of linksLink_list =linklist ()#Create a linked listlink_list.create (list)#Get nodeLink_list.get (-1)    #Delete a nodelink_list.remove (0)#inserting NodesLink_list.insert (-2, 0.2)    #Append node at the bottomLink_list.append (3)    #Modifying node valuesLink_list.modify (-1, 666)

Define a one-way linked list in Python3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.