为了账号安全,请及时绑定邮箱和手机立即绑定

将 n 个值插入空的双向链表(伪代码)

将 n 个值插入空的双向链表(伪代码)

海绵宝宝撒 2021-06-01 15:55:10
有人可以帮我验证我的伪代码是否正常工作,以便显示的函数将 n 个数字附加到列表中吗?我第一年的数据科学作业需要帮助。我们正在学习数据结构和双向链表。我理解我认为的双向链表背后的逻辑,但是我很难写出伪代码中发生的事情。” 作业 1:让列表 S 为空作为开始。现在输入 n(自然数,例如:1, 2, 3 ...)一个。当 n 的最后一个数字放入列表时, list 将是一个包含 n 个数字的排序列表。描述为什么我们会在 O(n^2) 时间内得到一个排序的 n 个数字。”我对作业的回答写在下面,我真的不确定它是否正确。// Our nodes will consist of 3 cells in each object.// key  = a number (int)// prev = address pointer to previous node // next = address pointer til next node// This function creates an empty listfunction emptyList()   L = new List{head = nil, size = 0}   return L// This function creates a node.function makeNode(val)  node = new Node{prev = NIL, key = val, next = NIL}  return node// This function inserts n amount of nodes to an empty listfunction InsertNodes(n)    // Create an empty list, S.    emptyList()    // Initiate the first node    S.head  = makeNode(1)     for i = 2 to n-1        prevNode = makeNode(i-1)        newNode  = makeNode(i)        while newNode.prev == NIL do            // connect addresses of nodes            prevNode.next = newNode.prev            newNode.prev  = prevNode.next该课程是关于算法和离散数学的
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

// Our nodes will consist of 3 cells in each object.

// key  = a number (int)

// prev = address pointer to previous node 

// next = address pointer til next node


// This function creates an empty list

function emptyList()

   L = new List{head = nil, size = 0}

   return L


// This function creates a node.

function makeNode(val)

  node = new Node{prev = NIL, key = val, next = NIL}

  return node


// This function inserts n amount of nodes to an empty list

function InsertNodes(n)


    // Create an empty list, S.

    emptyList()


    // Initiate the first node

    S.head  = makeNode(1) 


    //tail keeps the last node

    tail = head

    for i = 2 to n


        tail.next = makeNode(i)

        tail.next.prev = tail

        tail = tail.next


查看完整回答
反对 回复 2021-06-29
  • 1 回答
  • 0 关注
  • 160 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号