美文网首页
LNode文件

LNode文件

作者: 就是果味熊 | 来源:发表于2020-03-05 21:51 被阅读0次

自己新建两个类
LNode LHead

class LHead:
    def __init__(self):
        self.next = None
    
    def get_ordered_list(self): # 以列表形式打印顺序表
        order_list = []
        if self.next == None or self == None:
            return order_list
        else:
            cur = self.next
            while cur != None:
                order_list.append(cur.data)
                cur = cur.next
            return order_list
        
    def ordered_list_size(self): #获得链表长度
        
        return len(self.get_ordered_list())
    
    def create_ordered_list(self,size): #在新建头结点后面生成有序链表
        i = 0
        tmp = None
        cur = self
        ordered_list = []
         #构造单链表
        while i < size:
             tmp = LNode(i)
             tmp.next = None
             cur.next = tmp
             cur = tmp
             i += 1
        cur = self.next
        while cur != None:
            ordered_list.append(cur.data)
            cur = cur.next
        return ordered_list,self
    
#%%
class LNode:
    def __init__(self,data):
        self.data = data
        self.next = None
        

#%%
def create_unordered_list(list1): #输出值为list1各元素的无序表的表头
    tmp = None
    head = LHead()
    cur = head
     #构造单链表
    for i in range(len(list1)):
         tmp = LNode(list1[i])
         cur.next = tmp
         cur = tmp
    return head
#%%
if __name__ == '__main__':
    print('good ending')

相关文章

网友评论

      本文标题:LNode文件

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