Skip to content
Advertisement

Why this linked list rear operation snippet code throws error?

in the below snippet code contains 3 files, named main which is the main operation code, node file contains LinkedList creation and methods contains the linked list operation. In the add rear section throws error.. while execution. I have added the snippet error screshoot

Main.java

import java.util.*; 
    public class main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (true) {
                System.out.println("1-> add front 2->add rear 3->display");
                System.out.println("enter your choice");
                int ch = in.nextInt();
                methods list = new methods();
                switch (ch) {
                    case 1: {
                        System.out.println("Enter the element to add");
                        int newAdd = in.nextInt();
                        list.addfront(newAdd);
                    }
                    break;
                    case 2: {
                        System.out.println("Enter the element to add");
                        int newAdd = in.nextInt();
                        list.addrear(newAdd);
                    }
                    case 3:
                        list.display();
                        break;
                    default:
                        System.exit(0);
    
                }
    
    
            }
        }
    }

node.java

public class node {
    private int data;
    private node next;

    public node(int data) {
        this.data = data;
        this.next = null;
    }

    public int getData() {
        return data;
    }

    public node getNext() {
        return next;
    }

    public void setNext(node next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "node{" +
                "data=" + data +
                '}';
    }
}

Methods.java

public class methods 
{

    node head;

    public void addfront(int data)
    {
        node n= new node(data);
        if(head!=null)
           n.setNext(head);
        head=n;
    }
    public void addrear(int data)
    {
        node n = new node(data);
        node temp= head;
        while(temp.getNext()!=null)
        {
            temp=temp.getNext();
        }
        temp.setNext(n);
    }

    public void display()
    {
        node current = head;
        while(current!=null)
        {
            System.out.println(current);
            current=current.getNext();
        }
    }
}

Execution Output Image

Advertisement

Answer

few mistakes here :

need to initialise ‘methods’ outside while loop. As at every input, it gets initialised.

Scanner in = new Scanner(System.in);
methods list = new methods();
while (true) { ----}

Also in addrear(int data) method , need to handle head == null condition, i.e., how to add element when there are no elements in the list.

if (head == null) { 
 head = n; return; 
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement