亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 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 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號