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

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

我無法使用遞歸以相反的順序打印鏈表元素

我無法使用遞歸以相反的順序打印鏈表元素

冉冉說 2021-05-15 19:14:39
我是Java的初學(xué)者。我正在鏈表中實(shí)現(xiàn)遞歸以按相反的順序打印元素,但我認(rèn)為我的代碼中存在語義錯誤,請?zhí)崆皺z查我的代碼(尤其是反向方法)。輸出:78 30 52發(fā)送后,從頭開始您需要插入包裝練習(xí)的物品計(jì)數(shù);public class Linkedlist {    Node head;    public Linkedlist() {        head = null;    }    public void insert(int data) {        Node obj1 = new Node(data);        obj1.next = head;        head = obj1;    }    public void append(int data) {        Node newn = new Node(data);        if (head == null) {            newn.next = null;            head = newn;            return;        }        Node n = head;        while (n.next != null) {            n = n.next;        }        newn.next = null;        n.next = newn;    }    void delete(int data) {        if (head == null) {            return;        } else if (head.data == data) {            head = head.next;            return;        }        Node curr = head;        while (curr.next != null) {            if (curr.next.data == data) {                curr.next = curr.next.next;            }            curr = curr.next;        }    }    void insertAt(int count, int data) {        Node h = head;        if (count == 0) {            this.insert(data);            return;        }        while (h.next != null) {            if (count == 0) {                Node f = new Node(data);                f = h.next;                h = f;                return;            }            count--;            h = h.next;        }    }    public void reverse() {        if (head == null) {            System.out.println("null");        } else {            this.reverseRecursive(head);        }    }    private void reverseRecursive(Node nod) {        if (nod == null) {            return;        }        reverseRecursive(nod.next);        System.out.print(nod.data + " ");    }    class Node {        Node next;        int data;        public Node(int data) {            this.data = data;        }    }
查看完整描述

2 回答

?
慕尼黑8549860

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

在您的LinkedList中,而不是使用insert方法(該方法在頭部添加一個(gè)元素),請使用append方法,該方法在LinkedList的末尾添加該元素,然后調(diào)用反向方法。


查看完整回答
反對 回復(fù) 2021-05-26
  • 2 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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