java递归链表(java recursive linked list)

编程入门 行业动态 更新时间:2024-10-26 01:16:44
java递归链表(java recursive linked list) java 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);

当我试图测试这个递归方法时,它会向我显示list.removeFirst(elem)附近的错误; 并且只给出建议初始化它,即使它被初始化(如果按初始化将其设置为null)。 所以我想知道我做错了什么。 错误消息:此行有多个标记 - 局部变量elem可能尚未初始化 - 构造函数Element(Element)未定义

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

最满意答案

因为

Element elem;

可能是null时

list.removeFirst(elem);

被执行。

所以它会

Element elem = null;

(您需要初始化它才能使用它。)

无论如何,我很确定你想要这样的东西:

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

所以

list.removeFirst(elem);

将删除最近添加的项目。

无论如何,你确定你不想使用removeFirstOccurrence吗? 因为removeFirst完全不同。

removeFirstOccurrence

删除此列表中第一次出现的指定元素(从头到尾遍历列表时)。 如果列表不包含该元素,则不会更改。

无论如何,你得到这个错误的原因与递归无关

编辑

好吧,你不需要任何编辑addFirst,因为removeFirst将删除列表中的第一个项目。 你只需要改变

removeFirst(elem);

removeFirst();

在这种情况下,如果您不在其他地方使用它,则不再需要elem 。

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.

更多推荐

本文发布于:2023-08-07 01:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457935.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   链表   java   list   linked

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!