0

I am trying to write a simple linked list class that uses generic data type. I am new to Java so I can't figure out how to debug the error message in the main method that arises when I try to insert data into an instance of my class. The class code and the main method are as follows:

import java.io.*; 
  
// Java program to implement 
// a Singly Linked List 
public class MyLinkedList<T> {
    

     Node head; // head of the list
     class Node<T> {
    
     T data;
     Node next;
     // Constructor
         Node(T d)
         {
             data = d;
             next = null;
         }
         }
      
        // Method to insert a new node 
    MyLinkedList insert(MyLinkedList list, T data)
    { 
        // Create a new node with given data 
            Node new_node = new Node(data); 
            new_node.next = null; 
      
            // If the Linked List is empty, 
        // then make the new node as head 
        if (list.head == null) { 
            list.head = new_node; 
        } 
        else { 
            // Else traverse till the last node 
            // and insert the new_node there 
                Node last = list.head; 
                while (last.next != null) { 
                    last = last.next; 
                } 
      
                // Insert the new_node at last node 
                last.next = new_node; 
            } 
      
            // Return the list by head 
        return list; 
    }    
    // Driver code 
    public static void main(String[] args) 
    { 
        /* Start with the empty list. */
        MyLinkedList list = new MyLinkedList();
        
        // Insert the values 
        list = insert(list, 1);
    } 
}
7
  • 3
    Add the error message to the question. Commented Jul 30, 2020 at 8:50
  • I think there is no problem with your code, could you show us the error message? Commented Jul 30, 2020 at 8:53
  • 2
    This is possibly not the code you intended to provide, as there's no generic data type usage to be seen; just integers as data. Commented Jul 30, 2020 at 8:55
  • I've run your code and it didn't give me any errors so there's nothing wrong with the code itself. Commented Jul 30, 2020 at 8:56
  • 1
    Furthermore, you should strongly consider, implementing it in an object-oriented fashion. Using your static method would not be considered clean or elegant in java.. Commented Jul 30, 2020 at 9:00

1 Answer 1

3
  1. You need to change int to T in class declaration and method signature.

     class MyLinkedList<T> {
     MyLinkedList() {
     head=null;
     }
     Node head; // head of list
     class Node<T> {
    
     T data;
     Node next;
    
     // Constructor
     Node(T d) {
         data = d;
         next = null;
     }
    

    }

    // Method to insert a new node

    public void insert(T data) {
     // Create a new node with given data
     Node new_node = new Node(data);
     new_node.next = null;
    
     // If the Linked List is empty,
     // then make the new node as head
     if (this.head == null) {
         this.head = new_node;
     } else {
         Node last = this.head;
         while (last.next != null) {
             last = last.next;
         }
         // Insert the new_node at last node
         last.next = new_node;
     }
    

    }

     protected void display() {
     Node myNode=head;
     System.out.println();
     while (myNode != null) {
         System.out.println(myNode.data);
         myNode=myNode.next;
     }
    

    }

  2. Change the insert method signature to the below:

    public static void main(String[] args) {
     /* Start with the empty list. */
     MyLinkedList<Integer> list = new MyLinkedList<Integer>();
    
     // Insert the values
     list.insert(1);
     list.insert(3);
     list.insert(12);
     list.insert(11);
     list.insert(21);
     list.insert(22);
     list.insert(45);
     list.display();
     }
    
  3. For clear coding and understanding I have changed class name as MyLinkedList

Sign up to request clarification or add additional context in comments.

5 Comments

I did as you suggested and I am still getting an error. Try running my code that's above.
I would say, either copy-paste the error message here or screenshot.
I can't send an error message because the code won't compile so I can't run it. I am using Eclipse and it highlights the error, it's this line in the code that won't compile list = insert(list, 1);
@JunsuiBantsu: plz check my answer. I have added the complete code. It's working for me and it should work for you as well. All the best. (y) Also, you can not call a non-static method as static i.e. instance method must be called using class object only.
I assume you're trying to call "insert" from the main method. You cannot do this, since "main" is a static (global) method and "insert" is a method of "MyLinkedList" which you call as follows: list.insert(...) as @garima-garg points out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.