python3實現連結串列

jiuyang發表於2017-11-30

1、連結串列的實現

  a、連結串列的結構為:

  b、連結串列的實現方法;

#連結串列結構實現  私有屬性_pro_item是指向下個節點的指標,_item為此節點的值
class ChainDemo():

    def __init__(self,item = None,pos_item=None):

        self._item = item
        self._pos_item = pos_item


if __name__ == '__main__':
    chain = ChainDemo('A',(ChainDemo('B',ChainDemo('C',ChainDemo('D')))))
    while True:
        print(chain._item)
        if chain._pos_item != None:
            chain = chain._pos_item
        else:
            break

 2、實現對連結串列的操作(增刪)

#連結串列節點結構實現  私有屬性_pro_item是指向下個節點的指標,_item為此節點的值
class Node():

    def __init__(self,item = None,pos_item=None):

        self._item = item
        self._next = pos_item

    def __repr__(self):
        '''
        用來定義Node的字元輸出,
        print為輸出item
        '''
        return str(self._item)

#單連結串列實現
class Chain():

    def __init__(self):
        self._head = None
        self.length = 0

    #判空
    def isEmpty(self):
        return self.length == 0

    #連結串列結尾插入
    def append(self,item):

        if isinstance(item,Node):
            node = item
        else:
            node = Node(item)


        if self._head == None:
            self._head = node
        else:
            be_node = self._head
            while be_node._next:
                be_node = be_node._next
            be_node._next = node
        self.length += 1


    #插入資料
    def insert(self,index,item):

        if self.isEmpty():
            print('this chain table is empty')
            return

        if index<0 or index >= self.length:
            print("error: out of index")
            return

        in_node = Node(item)
        node  = self._head
        count = 1

        while True:
            node = node._next
            count += 1
            if count == index:

                next_node = node._next
                node._next = in_node
                in_node._next = next_node
                self.length += 1
                return


            # node = s


    #刪除資料
    def delete(self,index):

        if self.isEmpty():
            print('this chain table is empty')
            return

        if index<0 or index >= self.length:
            print("error: out of index")
            return
        # if index == 0
        #     self._head = None
        else:
            node = self._head
            count = 0
            while True:
                count += 1
                if index == count:
                    node._next = node._next._next
                    break
                node = node._next


        self.length -= 1

    def __repr__(self):
        if self.isEmpty():
            print("the chain table is empty")
            return
        nlist = ""
        node = self._head
        while node:
            nlist += node._item +''
            node = node._next
        return nlist


if __name__ == '__main__':
    chain = Chain()
    chain.append('A')
    chain.append('B')
    chain.append('C')
    chain.append('D')
    chain.append('E')
    chain.append('F')
    chain.append('G')
    chain.insert(4,'p')
    chain.delete(3)
    print(chain,chain._head._item,chain.length)

 

相關文章