第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

我正在嘗試可視化鏈接列表,我想在頭部插入一個(gè)節(jié)點(diǎn)并將另一個(gè)節(jié)點(diǎn)推到下一個(gè)

我正在嘗試可視化鏈接列表,我想在頭部插入一個(gè)節(jié)點(diǎn)并將另一個(gè)節(jié)點(diǎn)推到下一個(gè)

瀟瀟雨雨 2023-04-13 14:13:28
我想要做的是在頭部之后插入一個(gè)節(jié)點(diǎn)。在任意位置插入,當(dāng)我在head插入時(shí):我想讓之前的head移動到head.next。class Node{Node next;Node previous;int data;public Node(int data){    this.data = data;}}public class LinkedList {Node head;public Node push(int data){Node newNode = new Node(data);if(head == null){newNode.next = head;head = newNode;}else{newNode.next = head.next;head.next = new Node(data);            }        return head;}public Node insertAtEnd(int data){    Node temp = this.head;    while(temp!=null){    temp = temp.next;}    return temp = new Node(data);}主要的LinkedList ll = new LinkedList();         ll.push(15);         ll.push(4);         ll.push(78);         ll.push(55);         ll.insertAtEnd(80);         ll.printList();         int s = ll.getSize();         System.out.println(s);代碼只輸出某些節(jié)點(diǎn)而不是列表中的所有節(jié)點(diǎn)。
查看完整描述

3 回答

?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

在 push 方法的 else 語句中有一個(gè)不必要的 while 循環(huán)。


而(溫度!=空)


public Node push(int data){

    Node newNode = new Node(data);

    if(head == null){

    newNode.next = head;

    head = newNode;

    }

    else{

    newNode.next = head.next;

    head.next = new Node(data);            

    }        

    return head;

    }


查看完整回答
反對 回復(fù) 2023-04-13
?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

public final class LinkedList {


    private Node head;


    // method name should be clear

    public void addHead(int data) {

        Node node = new Node(data);


        if (!isEmpty())

            updateLinksBeforeInsert(node, head);


        head = node;

    }


    public void addTail(int data) {

        Node node = new Node(data);


        if (isEmpty())

            head = node;

        else

            updateLinksBeforeInsert(findLastNode(), node);

    }


    public boolean isEmpty() {

        return head == null;

    }


    // The last node is the node with 'next == null'

    private Node findLastNode() {

        Node node = head;


        while (node.next != null)

            node = node.next;


        return node;

    }


    // Before insert both 'prev' and 'next' links should be correctly updated

    private static void updateLinksBeforeInsert(Node prev, Node next) {

        prev.next = next;

        next.prev = prev;

    }


    // Accept a stream is more flexible than simple System.out

    public void print(PrintStream out) {

        Node node = head;


        while (node != null) {

            // print '-->' only after the first element

            if (node != head)

                out.print("-->");

            out.print(node.data);

            node = node.next;

        }

    }


    // Node should not be visible outside LinkedList

    private static final class Node {


        final int data;

        Node next;

        Node prev;


        private Node(int data) {

            this.data = data;

        }

    }


查看完整回答
反對 回復(fù) 2023-04-13
?
拉丁的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊

while 循環(huán)是導(dǎo)致無限循環(huán)的原因。tmp!=null由于 tmp 保持原樣,因此條件不會變?yōu)榧?。它不是穿越。第二個(gè)函數(shù)的情況也是如此,其中 head 永遠(yuǎn)不會向前遍歷,因此如果它不為空,它將保持不為空并且循環(huán)不會結(jié)束。


這將工作 -


在你的 push() 函數(shù)中 -


else{

   Node node = new Node(data);

   node.next = head;

   head.prev = node;   // taking into account that it is a dll

   this.head = node;

   return node;

}

還有你的 insertAtEnd(int data) -


public Node insertAtEnd(int data){

  Node tmp = this.head;

  while(tmp.next!=null){

    tmp = tmp.next;

  }

  tmp.next = new Node(data);

  tmp.next.prev = tmp;    // taking into account that it is a dll

  return tmp.next;

}

PS 在插入函數(shù)中通常沒有返回值,因?yàn)槲覀冎皇菍⒁恍?shù)據(jù)插入到數(shù)據(jù)結(jié)構(gòu)中。我們最多可能需要插入是否成功。


查看完整回答
反對 回復(fù) 2023-04-13
  • 3 回答
  • 0 關(guān)注
  • 135 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號