0
     private Node<T> recremoveFirst (Node<T> list, T element)
     {

 if (list == null)
     return null;
 else if(list.next == element)
     return list.next;
 else{
     list.next = recremoveFirst(list.next, element);
     return list;
 }

 }//calling recursive method
     public void removeFirst(T element) {

    recremoveFirst(head, element);

 }
 int choice;
Element elem;//constructor public Element (String name, int no)

LinkedList<Element> list = new LinkedList<Element>();

String name;
int number;
case 1 :  // addFirst
      System.out.print("Type name and number: ");
      name = Cin.readString();
      number = Cin.readInt();
      list.addFirst(new Element(name,number));
      break;
    case 2 :  // addLast
      System.out.println("Enter name and number to add last element: ");
      name = Cin.readString();
      number = Cin.readInt();
      list.addLast(new Element(name, number));
      break;

 case 3 :  // removeFirst

        list.removeFirst(elem);

When I'm trying to test this recursive method it shows me an error near list.removeFirst(elem); and gives only suggestion initialize it even though it is initialized(if press initialize sets it to null). So I wonder what's is that I'm doing wrong. Error mssage: Multiple markers at this line - The local variable elem may not have been initialized - The constructor Element(Element) is undefined

7
  • You have initialized the list but its empty and you are trying to remove particular element Commented Mar 8, 2014 at 17:04
  • if(list.elem == element) <-- are you sure about this one? Commented Mar 8, 2014 at 17:06
  • supposed to be list.next Commented Mar 8, 2014 at 17:09
  • What you're doing wrong: you're not reading, nor posting the error message that the compiler produces and which indicates what the error is. And you're trying to work, and make us work with code that is very hard to read because it's not formatted correctly. Your IDE can format it using a single keyboard shortcut. Commented Mar 8, 2014 at 17:12
  • error message: Multiple markers at this line - The local variable elem may not have been initialized - The constructor Element(Element) is undefined Commented Mar 8, 2014 at 17:13

1 Answer 1

1

Because

Element elem;

could be null when

list.removeFirst(elem);

is executed.

So it will be

Element elem = null;

(You need to initialize it to use it.)

Anyway, i'm pretty sure you want something like this:

list.addFirst(elem = new Element(name,number));

So it

list.removeFirst(elem);

will remove the item added recently.

Anyway, are you sure you don't want to use removeFirstOccurrence ? Because removeFirst does a total different thing.

removeFirstOccurrence:

Removes the first occurrence of the specified element in this list (when traversing the list from head to tail). If the list does not contain the element, it is unchanged.

Anyway the reason you get this error, is not related to the recursion

Edit:

Well, you don't need any edit to addFirst since removeFirst will remove the first item in the list. You just need to change

removeFirst(elem);

to

removeFirst();

In this case, if you don't use it in other places, you don't need anymore elem.

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

3 Comments

I might, just don't really know what that is
You need to replace list.removeFirst with list.removeFirstOccurrence(elem); If you want to remove elem from the list.
Edited answer, you don't need to edit anything since addFirst/removeFirst will do it for you. But anyway, you need to remove the first arg from removeFirst and, if you don't need it anymore, remove elem variable.

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.