帮忙用Java制作一个单向链表
这是家庭作业,但请知道我已经在网上寻求帮助(例如 http://www.sethi.org/classes/class_stuff/cis435/others/notes-java/data/collections/lists/simple-linked-list.html )和我的教科书,但我仍然遇到一些问题。
任何帮助将不胜感激...
现在我正在尝试插入值,但没有任何效果。无论它是第一项,无论是作为最后一项添加,还是介于两者之间。
Node header = null; // First element of list.
Node back = null; // Last element of list.
public void insert(int i, double value){ //insert value before i-th element
Node e = new Node();
e.num = value;
Node curr = header;
for(int x=0;x<i;x++) {
if (i == 1) { //we want to insert as first thing
if (size == 0) { //its the FIRST time we add something
header.next = e;
e.next = back;
break;
} else if (size == 1){
e.next = header.next; //i.e. the second thing in the list
header.next = e;
break;
} else {
e.next = header.next.next; //i.e. the second thing in the list
header.next = e;
break;
}
}
else if (x == (i-1)) {
e.next = curr.next;
curr.next = e;
break;
}
curr = curr.next;
}
size = size+1;
}
不太确定为什么它不起作用。
谢谢!
This is for homework but please know that I have looked online for help (such as http://www.sethi.org/classes/class_stuff/cis435/others/notes-java/data/collections/lists/simple-linked-list.html) and my textbook but I am still having some issues.
Any help would be appreciated...
Right now I'm trying to just insert values in but nothing is working. Whether it's the first item, whether it's being added as the last one, or somewhere in between.
Node header = null; // First element of list.
Node back = null; // Last element of list.
public void insert(int i, double value){ //insert value before i-th element
Node e = new Node();
e.num = value;
Node curr = header;
for(int x=0;x<i;x++) {
if (i == 1) { //we want to insert as first thing
if (size == 0) { //its the FIRST time we add something
header.next = e;
e.next = back;
break;
} else if (size == 1){
e.next = header.next; //i.e. the second thing in the list
header.next = e;
break;
} else {
e.next = header.next.next; //i.e. the second thing in the list
header.next = e;
break;
}
}
else if (x == (i-1)) {
e.next = curr.next;
curr.next = e;
break;
}
curr = curr.next;
}
size = size+1;
}
Not really sure why it isn't working.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于某种原因,仍在学习编程的人使事情变得比他们需要的复杂得多。我在学习 java 时就这样做了,当我刚刚进入一门新语言时我仍然这样做,而且我标记的学生找到了新的、令人惊奇的方法来做到这一点。您的插入中需要进行更多操作,例如,在特定索引处插入值的方法不应该检查它是否是要插入的第一个项目(并不是说它不应该检查边界)。这是我要做的伪代码。
给你几个方便的提示,你应该有一个函数从链接列表中获取一个元素,返回一个节点?例如公共节点 elementAt(int index) ?用它来遍历链表。如果你想追加到链接列表,试试这个
,如果你想在开头插入?同样的想法
For some reason, people who are still learning to program make things far more complicated then they need to be. I did it when I was learning java, I still do it when I am just getting into a new language, and students that I have marked find new and amazing ways to do it. You have more going on in your insert then there needs to be, for example, a method that inserts a value at a specific index should not check if it's the first item to be inserted (not saying it shouldn't check bounds). Here is the pseudo code of what I would do.
Couple of handy hints for you, you should have a function to get an element from the link list, something that returns a node? public node elementAt(int index) for example? use that to traverse the linked list. If you want to append to the Linked list, try this
and if you want to insert at the beginning? same idea
e.next = header.next.next
行中,如果header.next
指向“null”会发生什么?可以到达吗?e.next = header.next.next
what would happen ifheader.next
points to a 'null'? Is it possible to get there?一些建议:
在考虑“在 i 处插入”之前,先从“在末尾插入”开始。
A few suggestions:
Start with "insert at the end" before you think about "insert at i".
我尝试了一个简单的程序,这对你们很有用,我也在学习Java,如果有任何错误,请原谅我,但这个程序运行良好。
我正在发布一个非常简单的 Java 单链表程序,我今天尝试了它。
我希望它能帮助大家。
LinkList.java
Node.java
输出:
下面是我们的输出
I have tried a simple program, which will be useful for you guys, I am also learning Java, please bear with me for any mistakes, but this program works fine.
I am posting a very simple singly linked list program in Java, which I tried out today.
I hope it will help all.
LinkList.java
Node.java
OUTPUT :
The below is our output