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

有人可以帮我验证我的伪代码是否正常工作,以便显示的函数将 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 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) 


    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

该课程是关于算法和离散数学的

海绵宝宝撒
浏览 161回答 1
1回答

喵喵时光机

// 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)     //tail keeps the last node    tail = head    for i = 2 to n        tail.next = makeNode(i)        tail.next.prev = tail        tail = tail.next
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python