Python 데이터 구조의 링크 실현 (1)

체인 테이블
사내 의 박문 에 감 사 드 립 니 다.
https://blog.csdn.net/Tonywu2018/article/details/88853533
Python 은 단일 체인 표 소스 코드 를 실현 하여 모두 가 함께 공부 할 수 있 도록 제공 합 니 다.
'''      '''

class Node():
    '''        '''
    def __init__(self,data,next=None):
        self.data = data
        self.next = next

class LinkedList():
    '''      '''
    def __init__(self):
        #             
        self.head = None
        self.length = 0
    
    def demo(self):
        '''  '''
        node1 = None
        node2 = Node('hei', None)
        node3 = Node('ha', node2)
        print(node1, node2.data, node3.data, node3.next, node3.next.data)
    
    def createlinkedlist(self):
        '''     '''
        for count in [11, 14, 15, 22, 232]:
            self.head = Node(count, self.head)
            self.length += 1
        # print(self.head.data, self.length)
        return self.head,self.length #          

    def printcreatelinkedlist(self):
        '''     '''
        head = self.head
        length = self.length
        node_list = []
        while length > 0:
            node_list.append(head.data)
            head = head.next
            length -= 1
        print(node_list)
    
    def traversal(self):
        '''     '''
        head = None
        for count in range(10, 28, 5):
            head = Node(count, head)
        while head != None:
            print(head.data)
            head = head.next
    
    def searchtarget(self,target): 
        '''          '''
        temp = self.head
        print(type(temp))
        while temp.data != target and temp != None:
            # print(temp.data)
            temp = temp.next
            if temp == None:
                break
        if temp != None:
            print(str(target) + '   ')
        else:
            print(str(target) + '   ')

    def searchindex(self,index):
        '''        '''
        head = self.head
        while index > 0:
            head = head.next
            index -= 1
        print(head.data)
    
    def replacetarget(self,target,new_target):
        '''          '''
        head = self.head
        while head.data != target and head != None:
            head = head.next
            if head == None:
                break
        if head != None:
            head.data = new_target
            print('     '+ str(head.data))
        else:
            print('   '+str(target))
    
    def replaceindex(self,index,new_target):
        '''          '''
        head = self.head
        while index > 0:
            head = head.next
            index -= 1
        head.data = new_target
        print('   '+ str(head.data))

    def inserttarget(self,target):
        '''         '''
        if self.head == None: #         
            self.head = Node(target, self.head)
        else: #         
            head = self.head
            while head.next != None:
                head = head.next
            head.next = Node(target,None)
        self.length += 1
    
    def insert_anywhere_target(self,index,target):
        '''             '''
        if self.head == None or index <= 0:
            self.head = Node(target,self.head)
        else:
            head = self.head
            while index >= 1 and head.next != None:
                head = head.next
                index -= 1
            head.next = Node(target,head.next)
        self.length += 1
    
    def deletetarget(self):
        '''         '''
        if self.head.next == None:
            self.head = None
        else:
            head = self.head
            while head.next != None:
                head = head.next
            head.next = None
        self.length -= 1
    
    def delete_index_target(self,index):
        '''          '''
        if self.head.next == None or index <= 0:
            self.head = self.head.next
        else:
            head = self.head
            while head.next != None and index > 1:
                head = head.next
                index -= 1
            head.next = head.next.next
        self.length -= 1

if __name__ == '__main__':
    Li = LinkedList()
    Li.demo()
    Li.createlinkedlist()
    Li.printcreatelinkedlist()
    Li.traversal()
    Li.searchtarget(999)
    Li.searchindex(4)
    Li.replacetarget(22,999)
    Li.searchtarget(999)
    Li.replaceindex(4,666)
    Li.printcreatelinkedlist()
    Li.inserttarget(888)
    Li.printcreatelinkedlist()
    Li.insert_anywhere_target(0,520)
    Li.printcreatelinkedlist()
    Li.deletetarget()
    Li.printcreatelinkedlist()
    Li.delete_index_target(0)
    Li.printcreatelinkedlist()

좋은 웹페이지 즐겨찾기