简体   繁体   中英

Printing a circular singly linked List

Below is my code to print a circular singly linked list from the SECOND NODE(ie the node next to my starting node from where I have inserted my values). But it seems that my code is unable to link the last node to my starting node. As a result of which I am not able to print my Circular linked list.

Can somebody correct my mistake?

INPUT : 1 2 3 4

EXPECTED O/P : 2 3 4 1

O/P GETTING: 2 3 4

import java.util.Scanner;
public class CircularLinkedList {
    CircularLinkedList ptr,head,next;
    int v;
    void headcre()
    {
       head=new CircularLinkedList();
       ptr=head;
    }
    void linkcre(int n)
    {
        Scanner sc=new Scanner(System.in);
        ptr=head;
        System.out.println("Enter elements of list");
        for(int i=0;i<n;i++)
        {
            ptr.v=sc.nextInt();
            ptr.next=new CircularLinkedList();
            ptr=ptr.next;
        }
        ptr.next=head; //TO LINK LAST NODE TO STARTING NODE
    }
    void printcre()
    {
        ptr=head;
        ptr=ptr.next; //printing the list from the second node
        while(ptr.next!=head)
        {
            System.out.print(ptr.v+" ");
            ptr=ptr.next;
        }
    }
    public static void main(String[] args) {
        CircularLinkedList obj=new CircularLinkedList();
        System.out.println("Enter number of elements to be present in the list");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        obj.headcre(); //To create starting node
        obj.linkcre(n); //To enter elements
        obj.printcre(); //To print the list
    }
}

Some issues:

  • Your code is trying to fit two different concepts into one class: a node of a list, and the list itself. It is weird that a first instance of the CircularLinkedList class serves as the container for head , while other instances (which also have a head reference which remains useless) serve as the actual data-nodes of the list. You should dedicate a separate class for each.

  • The code creates a node before it has a value to store in that node. This means your list will always have a node that is unused for data. So when all input has been stored in nodes, there is one more node that was created, which has no data ( ptr.v remains uninitialised).

  • In printcre , when the head node is visited, the loop exits, and so that node's value is never printed.

  • It is not good practice to perform I/O in methods of such a class. Keep I/O in your main code, and provided methods that do the pure list stuff, or maybe produce a string representation of the list. But don't do I/O in these methods. Mixing concerns like that is not a good habit.

  • For a circular list it is actually more interesting to keep a reference to the tail than to the head, because the head can be easily be found from the tail (it is its successor), while getting the tail from the head requires to traverse all nodes.

Here is code I would suggest:

ListNode class

public class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
        this.next = this; // Make circular by default
    }
    
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

CircularLinkedList class

public class CircularLinkedList {
    ListNode tail = null;

    public void append(int data) {
        if (tail == null) {
            tail = new ListNode(data);
        } else {
            tail = tail.next = new ListNode(data, tail.next);
        }
    }

    public String toString() {
        if (tail == null) return "";
        String s = "";
        ListNode node = tail.next.next;
        while (node != tail.next) {
            s += node.val + " ";
            node = node.next;
        }
        return s + node.val; // Also include the head's value in the string
    } 
}

Driver code

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter number of elements to be present in the list");
        int n = sc.nextInt();
        
        CircularLinkedList list = new CircularLinkedList();
        
        System.out.println("Enter elements of list");
        for (int i = 0; i < n; i++) {
            list.append(sc.nextInt());
        }
        sc.close();

        System.out.println(list.toString());
    }

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