简体   繁体   中英

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. output:78 30 52 send after which item count from head u need to insert package practice;

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. 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. ( 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 :

78 30 52

Also, your code needs a bit of formatting and should follow java conventions. Method names start with lower case and class names with uppercase. Good luck :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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