Python implementation singly linked list data add, delete, insert operation

Python achieve singly linked list data add, delete, insert operation

List of definitions:

  Chain (linked list) is a data structure consisting of a set of data elements is referred to as nodes, each of which contains the node address of the node itself and information pointing to a node. Since each node contains the address information can be linked, so with a variable node can access the whole sequence. That is, the node contains two pieces of information: a part for storing data element values, called the domain information; another portion for storing a pointer to the next data element address, called pointer field. Storing a first address of a node in a linked list in a single node, referred to as the first node or the first node. The last node in the list is no subsequent element, which pointer field is empty. 

 

When the sequence contains only a link to its successor node, it is referred to the list as a single linked list.

 

Single chain structural diagram as follows:

 

 

 

 

 code show as below:

# Junction defined 
class the Node (Object):
     DEF  the __init__ (Self, Val, P = 0): 
        self.data = Val 
        self.next = P 

# chain operation 
class LinkList (Object):
     # define the first node 
    DEF  the __init__ (Self ): 
        self.head = 0 

    DEF  the __getitem__ (Self, Key): 

        IF self.is_empty ():
             Print ( ' ! list is empty ' )
             return 

        elif Key <0   or Key>self.getlength ():
             Print ( ' Type the value of the error! ' )
             return 

        the else :
             return self.getitem (Key) 



    DEF  __setitem__ (Self, Key, value): 

        IF self.is_empty ():
             Print ( ' ! list is empty ' )
             return 

        elif Key <0   or Key> self.getlength ():
             Print ( ' ! typing error value ' )
             return 

        the else : 
            self.delete (Key) 
            return self.insert(key)

    #初始化链表
    def initlist(self,data):

        self.head = Node(data[0])

        p = self.head

        for i in data[1:]:
            node = Node(i)
            p.next = node
            p = p.next

    #获取链表长度
    def getlength(self):

        p =  self.head
        length = 0
        while p!=0:
            length+=1
            p = P.next 

        return length 

    # determines whether the list is empty 
    DEF : is_empty (Self) 

        IF () == self.getlength 0:
             return True
         the else :
             return False 


    DEF Clear (Self): 

        self.head = 0 

    # single chain add operation added at the end nodes 
    DEF the append (Self, Item):
         # Q to be added to the node 
        Q = the node (Item)
         IF self.head == 0: 
            self.head = Q
         the else : 
            P =self.head
             the while ! p.next = 0: 
                P = p.next 
            p.next = Q 

    # acquired node data field value 
    DEF getItem (Self, index): 

        IF self.is_empty ():
             Print ( ' list is empty ! ' )
             return 
        J = 0 
        P = self.head 

        the while p.next = 0! and J < index: 
            P = p.next 
            J + =. 1 IF J ==

        index:
             return p.data 

        the else :
             Print ( ' object does not exist! ' ) 

    # linked list data insertion         
    DEF INSERT (Self, index, Item): 

        IF self.is_empty () or index <0 or index> self.getlength () :
             Print ( ' list is empty! ' )
             return 

        IF index == 0: 
            Q = the Node (Item, self.head) 
            self.head = Q 

        P = self.head 
        POST  = self.head
        j = 0
        while p.next!=0 and j<index:
            post = p
            p = p.next
            j+=1

        if index ==j:
            q = Node(item,p)
            post.next = q
            q.next = p

    #链表数据删除操作
    def delete(self,index):

        if self.is_empty() or index<0 or index >self.getlength():
            print('链表为空!')
            return

        if index ==0:
            q = Node(item,self.head)
            self.head = q

        p = self.head
        post  = self.head
        j = 0
        while p.next!=0 and j<index:
            post = p
            p = p.next
            j+=1

        if index ==j:
            post.next = p.next


    def index(self,value):
        if self.is_empty():
            print('链表为空!')
            return

        p = self.head
        i = 0
        while p.next!=0 and not p.data ==value:
            p = p.next
            i+=1

        if p.data == value:
            return i
        else:
            return -1


link = LinkList()
link.initlist([1,2,3,4,5])
print(link.getitem(4))
link.append(6)
print(link.getitem(5))

link.insert(4,40)
print(link.getitem(3))
print(link.getitem(4))
print(link.getitem(5))

link.delete(5)
print(link.getitem(5))

link.index(5)

 

operation result:

 

 

 

References:

https://www.cnblogs.com/yupeng/p/3413763.html

Guess you like

Origin www.cnblogs.com/BIXIABUMO/p/11965411.html