简体   繁体   English

java递归链表

[英]java recursive linked list

     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); 当我尝试测试此递归方法时,它在list.removeFirst(elem);附近显示一个错误。 and gives only suggestion initialize it even though it is initialized(if press initialize sets it to null). 并且仅给出建议,即使已将其初始化(如果按initialize将其设置为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 错误消息:此行有多个标记-本地变量elem可能尚未初始化-构造函数Element(Element)未定义

Because 因为

Element elem;

could be null when 在以下情况下可以为null

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 ? 无论如何,您确定不想使用removeFirstOccurrence吗? Because removeFirst does a total different thing. 因为removeFirst完全不同。

removeFirstOccurrence : 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. 好吧,您不需要对addFirst进行任何编辑,因为removeFirst会删除列表中的第一项。 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 . 在这种情况下,如果您不在其他地方使用它,则不再需要elem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM