如何在链表中找到第一个值? [英] How to find the first value in a linked list?
问题描述
我有一个链表,我需要通过getFirst方法在链表中找到第一个值,如果该值为空,我需要显示一条错误消息并退出程序.链接列表已提供给我链接,所以:
I have a linked list I'm given and I need to find the first value in the list via a getFirst method.I need to display an error message and quit the program if the value is null. The linked list is already given to me link so:
class MyLinkedList
{
private class Node // inner class
{
private Node link;
private int x;
}
//----------------------------------
private Node first = null; // initial value is null
//----------------------------------
public void addFirst(int d)
{
Node newNode = new Node(); // create new node
newNode.x = d; // init data field in new node
newNode.link = first; // new node points to first node
first = newNode; // first now points to new node
}
//----------------------------------
public void traverse()
{
Node p = first;
while (p != null) // do loop until p goes null
{
System.out.println(p.x); // display data
p = p.link; // move p to next node
}
}
}
//==============================================
class TestMyLinkedList
{
public static void main(String[] args)
{
MyLinkedList list = new MyLinkedList();
list.addFirst(1);
list.addFirst(2);
list.addFirst(3);
System.out.println("Numbers on list");
list.traverse();
}
}
这是我尝试使用的方法:
Here's what I tried out for the method:
public static Node getFirst(Node list)
{
if (list == null)
{
System.out.println("Error!");
System.exit(1);
}
return MyLinkedList.first;
}
我知道这是不对的,我们只是在我的课堂上开始了,所以我很难理解它是怎么回事.谢谢!
I know this isn't exactly right, we just started this in my class so I'm having trouble understanding what's going on with it. Thank you!
推荐答案
问题中的类MyLinkedList
遵循堆栈数据结构的模式(在我编写此答案时).也就是说:每次添加新元素时,新元素都会替换先前添加的元素作为第一个元素.
The class MyLinkedList
in your question follows the pattern of a stack data structure(At the time when i am writing this answer). That is: ever time you add a new element, the new element replaces the previously added element as the first element.
我想如果要按顺序添加元素1
,2
,3
,则希望将1
作为第一个元素.如果我错了请纠正我.
I guess you want to get 1
as your first element, if you have added elements 1
,2
,3
in that order. Correct me if i am wrong.
在这种情况下,您的链表及其检索应如下所示:
In that case your linked list and it's retrieval should be like this:
(注意:我避免使用private
vars,public
getter,settter 等;以使代码易于阅读.但是读者应该添加它们.)
(Note: i have avoided private
vars , public
getter , settter , etc; to make code easily readable. But readers should add them.)
class Node{ int x; Node next; }
class LinkedList
{ Node head,tail;
void add(int y)
{ Node node = new Node();
node.x=y;
if(head==null)
head = tail = node;
else
tail = tail.next = node;
}
int getFirst()
{ if(head!=null)
return head.x;
else
throw new java.util.NoSuchElementException("List is empty");
}
}
如果您查看java.util.LinkedList
,您会发现链表中通常使用的方法.如果这不是家庭作业的问题,那么我建议您不要重新发明轮子.只需使用现有的库即可.
If you look at java.util.LinkedList
, you will find methods that are conventionally used in linked lists. If this is not a homework question, then i suggest you do not reinvent the wheel. Just use the existing libraries.
如果必须使用堆栈数据结构,并且无法更改它,那么我建议您必须像这样更改getFirst()
:
If you have to use the stack data structure, and you cannot change it, then i suggest you have to change your getFirst()
like this:
int getFirst()
{ if(tail!=null)
return tail.x;
else
throw new java.util.NoSuchElementException("List is empty");
}
如果您不允许在代码中添加Node tail
,则您的getFirst()
将如下所示:
If you not allowed to add Node tail
in your code, then your getFirst()
will look like this:
int getFirst()
{ if(head==null)
throw new java.util.NoSuchElementException("List is empty");
Node node = head;
while(node.next!=null)
node=node.next;
return node.x;
}
这篇关于如何在链表中找到第一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!