Single-linked list implementation in Python

Source: Internet
Author: User

Introduction

The data structure refers to the way in which it is organized. From single data to one-dimensional structures (linear tables), two-dimensional structures (trees), three-dimensional structures (graphs), are different ways of organizing data.

Why do I need a linked list?

Sequential table construction needs to know the size of the data in advance to apply for continuous storage space, and in the expansion of the need for data relocation, so it is not very flexible to use.

Linked list structure can make full use of computer memory space, and realize flexible memory dynamic management.

A linked list (Linked list) is a common basic data structure that is a linear table, but does not store data continuously like sequential tables, but instead stores the location information (that is, the address) of the next node in each node (data storage unit).

What is a single linked list?

One-way linked list, also known as a single-linked list, is the simplest form of a list, with each node containing two domains, an information field (element field), and a linked domain. This link points to the next node in the list, and the last node's link field points to a null value.

What are the basic operations that a single-linked list should support?
    • Is_empty () linked list is empty
    • Length () Chain table lengths
    • Travel () traverse the entire list
    • add element to list header
    • Append add elements to the end of the list (item)
    • Insert (POS, item) to specify the location to add elements
    • Remove node (item)
    • Search (item) to find out if the node exists
Operation diagram:

Add

Insert

Remove

 

Code implementation
classNode (object):def __init__(self, value):#element FieldsSelf.value =value#link DomainSelf.next =NoneclassSinglelinklist (object):def __init__(Self, node=None): Self.__head=nodedef __len__(self):#a cursor used to traverse a linked listcur = self.__head        #Record traversal timesCount =0#The current node is none indicates that the traversal is complete         whileCur:count+ = 1cur=Cur.nextreturnCountdefIs_empty (self):#The head node is not a none and is not empty        returnSelf.__head==NonedefAdd (self, value):"""The head interpolation method first lets the new node next point to the head node and then replaces the head node with the new node order is not wrong, must first ensure the chain of the original chain continuously, otherwise the chain behind the head node is lost"""node=Node (value) Node.next= self.__headSelf .__head=nodedefAppend (self, value):"""tail interpolation method"""node=Node (value) cur= self.__head        ifSelf.is_empty (): Self.__head=nodeElse:             whileCur.next:cur=Cur.next Cur.next=nodedefInsert (self, POS, value):#dealing with Special situations        ifPOS <=0:self.add (value)elifPOS > Len (self)-1: Self.append (value)Else: Node=Node (value) prior= self.__headCount=0#stop at the previous node in the insertion position             whileCount < (pos-1): Prior=Prior.next Count+ = 1#first, the insertion node and node after the node connection, to prevent the linked list off, first link behind, and then link the front of theNode.next =Prior.next Prior.next=nodedefRemove (self, value): Cur= self.__headPrior=None whilecur:ifValue = =Cur.value:#determine if this node is a head node                ifCur = = self.__head: Self.__head=Cur.nextElse: Prior.next=Cur.next Break            #the node has not been found and has continued traversal            Else: Prior=cur cur=Cur.nextdefSearch (self, value): Cur= self.__head         whilecur:ifValue = =Cur.value:returnTrue cur=Cur.nextreturnFalsedefTraversal (self): cur= self.__head         whilecur:Print(cur.value) cur= Cur.next

Single-linked list implementation in Python

Related Article

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.