1

I'm trying to implement a linked list using objects. I get this error msg when I compile the code:

Person.java:49: error: constructor Node in class Node cannot be applied to given types;
      Node newNode = new Node(last, first, age);

Can anyone kindly give me a hand? why is this happening? Thank you. Here is the code:

    class Person{

private String lastName;
private String firstName;
private int age;

   public Person(String last, String first, int a){
      lastName=last;
      firstName=first;
      age=a;
   } 

   public void displayPerson(){
      System.out.println("Last Name: "+lastName);
      System.out.println("First name"+firstName);
      System.out.println("Age: "+age);
   }

   public String getLast(){
      return lastName;                                                                                                             
   }
}

class Node
{
   public Person data; 
   public Node next; 

   public Node(Person d)
   {
      data = d; 
   }

}
class LinkList
{
   private Node first;

   public  LinkList()
   {
      first = null;
   }
   public boolean isEmpty()
   {
      return (first==null);
   }
   public void insertFirst(String last, String first, int age)
   {
      Node newNode = new Node(last, first, age);
      newNode.next = first;
      first = newNode;
   }
   public Node deleteFirst(String last, String first, int age)
   {
      Node temp = first;
      first = first.next;
      return temp;
   }
   public void displayList()
   {
      System.out.print("Linked List (first -->last): ");
      Node current = first;
      while(current != null)
      {
         current.displayPerson();
         current = current.next;
      }
      System.out.println(" ");
   }
}
1
  • Where do you have a node constructor which takes string, string, int? Also first = newNode; is ambiguous, I think you meant to write something like this.first = newNode; Commented Oct 6, 2015 at 18:35

3 Answers 3

1

The line

Node newNode = new Node(last, first, age);

does not compile since the Node class does not have a constructor with three parameters of these types. It seems that you want

Node newNode = new Node(new Person(last, first, age));
Sign up to request clarification or add additional context in comments.

Comments

0

Node newNode = new Node(last, first, age);

Node doesn't have a constructor that takes 3 arguments.

I'm assuming you meant to create a Person object first and then pass that to the Node constructor, such as new Node(new Person(last, first, age))

Comments

0

I see some other errors in your code, for example:

  • on the insertFirst method, your String first parameter is hiding your Node first attribute. You should rename your parameter to avoid this
  • On your displayList method yo have current.displayPerson (), I think you want to current.data.displayPerson ()

Hope this helps :) Alberto

Comments

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.