简体   繁体   English

我无法使用递归以相反的顺序打印链表元素

[英]i can't print the linkedlist elements in reverse order using recursion

I am a beginner in java .I am implementing recursion in linked lists to print the elements in reverse order but i think that there is a semantic error in my code , check my code(especially that reverse method) thanks in an advance. 我是Java的初学者。我正在实现链表中的递归以按相反的顺序打印元素,但我认为我的代码中存在语义错误,请提前检查我的代码(尤其是反向方法)。 output:78 30 52 send after which item count from head u need to insert package practice; 输出:78 30 52发送后,从头开始您需要插入包装练习的计数;

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;
        }

    }

    public static void main(String args[]) {
        Linkedlist obj = new Linkedlist();
        obj.insert(78);
        obj.insert(30);
        obj.insert(52);
        obj.reverse();
        System.out.println("send after which item count from head u need to insert");
        obj.insertAt(2, 5);
    }
}

Looking at your code, I don't think there is anything wrong with your Reverse method. 查看您的代码,我认为您的Reverse方法没有任何问题。 It is actually printing in reverse order. 它实际上是按相反的顺序打印。 What's throwing you off is probably the way you are inserting the elements. 让您烦恼的可能是插入元素的方式。 Your insert() method is actually a stack. 您的insert()方法实际上是一个堆栈。 ( It inserts at top ). (它插入顶部)。 So after all insertions, head points to 52 and not 78. So when you print, the reverse list is printed as : 因此,在所有插入之后,head指向52而不是78。因此,在打印时,反向列表显示为:

78 30 52

Also, your code needs a bit of formatting and should follow java conventions. 另外,您的代码需要一些格式设置,并且应遵循Java约定。 Method names start with lower case and class names with uppercase. 方法名称以小写字母开头,类名称以大写字母开头。 Good luck :) 祝好运 :)

在您的LinkedList中,而不是使用insert方法,该方法在头部添加元素,请使用方法append将方法添加到LinkedList的末尾,然后调用反向方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM