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

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

雙重鏈表實(shí)現(xiàn)不起作用

雙重鏈表實(shí)現(xiàn)不起作用

慕的地6264312 2022-09-01 16:47:36
我有一個(gè)雙重鏈表實(shí)現(xiàn)如下:public class DoublyLinkedList<T> {    DoublyLinkedListNode<T> head;    public DoublyLinkedListNode<T> getHead() {        return head;    }    public void setHead(DoublyLinkedListNode<T> head) {        this.head = head;    }    public void addNode(DoublyLinkedListNode<T> node) {        if(null == head) {            head = new DoublyLinkedListNode<>(node.getData());        }else{            traverseAndAdd(node);        }    }    private boolean traverseAndAdd(DoublyLinkedListNode<T> node) {         boolean isAdded = false;    DoublyLinkedListNode<T> tempHead = head;  do{        if(tempHead.getNext() == null) {            head.setNext(node);            node.setPrev(head);            isAdded = true;            break;        }         tempHead = tempHead.getNext();    }while(null != tempHead);    return isAdded;    }    @Override    public String toString() {        StringBuffer sb = new StringBuffer();        while(null != head) {            sb.append(head.getData());            head = head.getNext();        }        return sb.toString();    }}以下是我的DoubleLinkedListNode類:public class DoublyLinkedListNode<T> {    T data;    DoublyLinkedListNode<T> prev;    DoublyLinkedListNode<T> next;    public DoublyLinkedListNode(T data) {        this.data = data;    }    public T getData() {        return data;    }    public void setData(T data) {        this.data = data;    }    public DoublyLinkedListNode<T> getPrev() {        return prev;    }    public void setPrev(DoublyLinkedListNode<T> prev) {        this.prev = prev;    }    public DoublyLinkedListNode<T> getNext() {        return next;    }    public void setNext(DoublyLinkedListNode<T> next) {        this.next = next;    }}現(xiàn)在,當(dāng)我創(chuàng)建DoublyLinkedList的實(shí)例并嘗試以某種方式向其添加節(jié)點(diǎn)時(shí),我的頭部實(shí)例變量始終為空。即使我在addNode方法中初始化它,當(dāng)我嘗試添加下一個(gè)節(jié)點(diǎn)時(shí),它總是空的。如果有人知道這個(gè)實(shí)現(xiàn)有任何問(wèn)題,有人可以告訴我。
查看完整描述

2 回答

?
慕沐林林

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

這有效


class DoublyLinkedList <T> {


    DoublyLinkedListNode<T> head;


    public DoublyLinkedListNode<T> getHead() {

        return head;

    }


    public void setHead(DoublyLinkedListNode<T> head) {

        this.head = head;

    }


    public void addNode(DoublyLinkedListNode<T> node) {

        if (null == head) {

            head = new DoublyLinkedListNode<>(node.getData());

        } else {

            traverseAndAdd(node);

        }

    }


    private boolean traverseAndAdd(DoublyLinkedListNode<T> node) {


        boolean isAdded = false;


        for (DoublyLinkedListNode<T> it = head; it != null; it = it.getNext()) {

            if(null == it.getNext()) {

                it.setNext(node);

                node.setPrev(it);

                isAdded = true;

                break;

            }

        }


        return isAdded;

    }


    @Override

    public String toString() {


        StringBuffer sb = new StringBuffer();


        for (DoublyLinkedListNode<T> it = head; it != null; it = it.getNext()) {

            sb.append(it.getData() + "\n");

        }


        return sb.toString();


    }

}


class DoublyLinkedListNode <T> {


    T data;

    DoublyLinkedListNode<T> prev;

    DoublyLinkedListNode<T> next;


    public DoublyLinkedListNode(T data) {

        this.data = data;

    }


    public T getData() {

        return data;

    }


    public void setData(T data) {

        this.data = data;

    }


    public DoublyLinkedListNode<T> getPrev() {

        return prev;

    }


    public void setPrev(DoublyLinkedListNode<T> prev) {

        this.prev = prev;

    }


    public DoublyLinkedListNode<T> getNext() {

        return next;

    }


    public void setNext(DoublyLinkedListNode<T> next) {

        this.next = next;

    }

}


public class Main {


    public static void main(String []args) {

        DoublyLinkedList<Integer> mylist = new DoublyLinkedList<>();


        DoublyLinkedListNode<Integer> node1 = new DoublyLinkedListNode<>(10);

        DoublyLinkedListNode<Integer> node2 = new DoublyLinkedListNode<>(20);

        DoublyLinkedListNode<Integer> node3 = new DoublyLinkedListNode<>(30);

        DoublyLinkedListNode<Integer> node4 = new DoublyLinkedListNode<>(40);

        DoublyLinkedListNode<Integer> node5 = new DoublyLinkedListNode<>(50);

        DoublyLinkedListNode<Integer> node6 = new DoublyLinkedListNode<>(60);

        mylist.addNode(node1);

        mylist.addNode(node2);

        mylist.addNode(node3);

        mylist.addNode(node4);

        mylist.addNode(node5);

        mylist.addNode(node6);


        System.out.println(mylist.toString());

        System.out.println(mylist.toString());

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-09-01
?
波斯汪

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

你的 head 引用并不總是空的,它確實(shí)初始化了添加第一個(gè)節(jié)點(diǎn),這是 traverseAndAdd() 方法中代碼中的問(wèn)題。


private boolean traverseAndAdd(DoublyLinkedListNode<T> node) {

    boolean isAdded = false;

    while(null != head.getNext()) {

        if(head.getNext() == null) {

            //this code is unreachable

        }

        //why are u changing the head value

        head = head.getNext();

    }

    return isAdded;

}

因此,將函數(shù)替換為


private void traverseAndAdd(DoublyLinkedListNode<T> node) {

    DoublyLinkedListNode<T>temp=head;

    while(null != temp.getNext()) {

        temp=temp.getNext();

    }

    temp.setNext(node);

    node.setPrev(temp);

}

另一個(gè)錯(cuò)誤,在 toString() 方法中,您將 null 值分配給 head。head 值首先被初始化,但每次調(diào)用 String() 時(shí)都會(huì)變?yōu)?null。將方法替換為


public String toString() {

    StringBuffer sb = new StringBuffer();

    DoublyLinkedListNode<T>temp=head;

    while(null != temp) {

        sb.append(temp.getData());

        temp = temp.getNext();

    }

    return sb.toString();

}


查看完整回答
反對(duì) 回復(fù) 2022-09-01
  • 2 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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