美文网首页
链表--python

链表--python

作者: snowpigppp | 来源:发表于2019-08-07 11:48 被阅读0次
    class LinkNode(object):
        def __init__(self, data, p = None):
            self.data = data
            self.next = p
    
    class LinkList(object):
        def __init__(self):
            self.head = None
    
        #链表初始化函数,尾插法,插入data
        def initlist_tail(self, data):
            #创建头结点,其实是第一个有值节点
            self.head = LinkNode(data[0])
            p = self.head
            for i in data[1:]:
                #print(i)
                node = LinkNode(i)
                p.next = node
                p = p.next
    
        #判断链表是否为空
        def isEmpty(self):
            p = self.head
            if p.next == None:
                print('The LinkList is Empty')
                return 1 #感觉是为了方便后续调用
            else:
                #print('The LinkList is not Empty')
                return 0
    
        #遍历链表并输出,用','隔开
        def ReadList(self):
            if self.isEmpty():
                exit(0)
            p = self.head
            while p:
                print(p.data, end = ',')
                p = p.next
            print('')
    
        #取链表长度
        def getLength(self):
            if self.isEmpty():
                exit(0)
            p = self.head
            lenth = 0
            while p:
                #print(p.data, end = ',')
                lenth += 1
                p = p.next
            print('lenth of the LinkList:', lenth)
            return lenth
    
        #在索引值为 index 的结点后插入结点key
        def insertElem(self, key, index):
            p = self.head
            j = 1
            while p and j < index:
                p = p.next
                j += 1
            if(p == 0 or j > index): #若出错则退出
                exit(0)
                print('insert error')
            node = LinkNode(key)
            node.next = p.next
            p.next = node
            print('inserted LinkList:')
            self.ReadList()
    
        #删除第 index个 结点后的那一个节点
        def deleteElem(self, index):
            p = self.head
            j = 1
            while p and j < index:
                p = p.next
                j += 1
            if(p == 0 or j > index): #若出错则退出
                exit(0)
                print('insert error')
            q = p.next
            p.next = q.next
            print('deleted LinkList:')
            self.ReadList()
    
        #链表逆序
        def reverseList(self):
            pre, next, current = None, None, None
            current = self.head
            # 当前节点指向头节点
            next = current.next
            # next 指向当前节点的下一个节点,即将后面的节点存储起来
            current.next = None
            # 令当前节点的next 指向None,即设为最后一个节点
            pre = current
            # 当前节点的前一个节点为当前节点
            current = next
            # 当前节点指向下一个节点开始下一轮循环
            while current.next != None:
                next = current.next
                # 存储后面的链表
                current.next = pre
                # 将原本的前面的链表设为后面的
                pre = current
                current = next
                # 遍历到链表最后一个节点时候,将其指向前驱节点
            current.next = pre
            # 头节点指向原来链表的最后一个节点
            self.head = current
    
    
    data1 = [1, 2, 3, 4, 5]
    data2 = [2, 0]
    a = LinkList()
    a.initlist_tail(data1)
    a.reverseList()
    a.ReadList() #输出结果为1,2,3,4,5,
    a.getLength() #输出结果为lenth of the LinkList: 5
    a.insertElem(6,3) #输出结果为1,2,3,6,4,5,
    a.deleteElem(3)#1,2,3,4,5, 
    

    相关文章

      网友评论

          本文标题:链表--python

          本文链接:https://www.haomeiwen.com/subject/uycodctx.html