如何从链表中删除节点
嘿伙计们,我写了这个deleteNode()方法,如果我使用数字(int),它可以工作,但当我尝试传递一个字符串时,它就不行。我正在打印一个 String[] 名称列表,并且我正在尝试删除名单上的某个名字。当我输入名称时,它会打印“未找到节点”。就像我说的,如果我打印出一个数字列表,它效果很好,但如果我改变并打印一个字符串,它就不行了。任何帮助表示赞赏。
public class BigNode {
public String dataitems;
public BigNode next;
BigNode front ;
public void initList(){
front = null;
}
public BigNode makeNode(String number){
BigNode newNode;
newNode = new BigNode();
newNode.dataitems = number;
newNode.next = null;
return newNode;
}
public boolean isListEmpty(BigNode front){
boolean balance;
if (front == null){
balance = true;
}
else {
balance = false;
}
return balance;
}
public BigNode findTail(BigNode front) {
BigNode current;
current = front;
while(current.next != null){
//System.out.print(current.dataitems);
current = current.next;
} //System.out.println(current.dataitems);
return current;
}
public void addNode(BigNode front ,String number){
BigNode tail;
if(isListEmpty(front)){
this.front = makeNode(number);
}
else {
tail = findTail(front);
tail.next = makeNode(number);
}
}
public void deleteNode(BigNode front, String value) {
BigNode curr, previous = null; boolean found;
if (!isListEmpty(front)){
curr = front;
found = false;
while ((curr.next != null) && (!found)) {
if(curr.dataitems.equals(value)) {
found = true;
}
else {
previous = curr;
curr = curr.next;
}
}
if (!found) {
if(curr.dataitems.equals(value)) {
found = true;
}
}
if (found) {
if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems
front = curr.next;
} else {
previous.next = curr.next;
}
} else {
System.out.println("Node not found!");
//curr.next = null; // Not sure If this is needed
}
}
showList(front);
}
public void printNodes(String[] len){
int j;
for (j = 0; j < len.length; j++){
addNode(front, len[j]);
} showList(front);
}
public void showList(BigNode front){
BigNode current;
current = front;
while ( current.next != null){
System.out.print(current.dataitems + ", ");
current = current.next;
}
System.out.println(current.dataitems);
}
public static void main(String[] args) {
String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"};
BigNode x = new BigNode();
x.printNodes(names);
Scanner in = new Scanner(System.in);
String delete = in.next();
x.deleteNode(x.front, delete);
}
String[] names = {name1, name2, name3, name4}
- 首先打印列表,然后询问要删除哪个名称。
Hey guys I wrote this deleteNode() method that works if I used numbers(int) but doesn't when I try to pass a string in. I'm printing out a String[] list of names and I'm trying to delete a certain name off the list. When I enter a name, it prints "Node not found". Like I said, if I print out a list of numbers it works great but if I change up and print a string it doesnt. Any help is appreciated.
public class BigNode {
public String dataitems;
public BigNode next;
BigNode front ;
public void initList(){
front = null;
}
public BigNode makeNode(String number){
BigNode newNode;
newNode = new BigNode();
newNode.dataitems = number;
newNode.next = null;
return newNode;
}
public boolean isListEmpty(BigNode front){
boolean balance;
if (front == null){
balance = true;
}
else {
balance = false;
}
return balance;
}
public BigNode findTail(BigNode front) {
BigNode current;
current = front;
while(current.next != null){
//System.out.print(current.dataitems);
current = current.next;
} //System.out.println(current.dataitems);
return current;
}
public void addNode(BigNode front ,String number){
BigNode tail;
if(isListEmpty(front)){
this.front = makeNode(number);
}
else {
tail = findTail(front);
tail.next = makeNode(number);
}
}
public void deleteNode(BigNode front, String value) {
BigNode curr, previous = null; boolean found;
if (!isListEmpty(front)){
curr = front;
found = false;
while ((curr.next != null) && (!found)) {
if(curr.dataitems.equals(value)) {
found = true;
}
else {
previous = curr;
curr = curr.next;
}
}
if (!found) {
if(curr.dataitems.equals(value)) {
found = true;
}
}
if (found) {
if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems
front = curr.next;
} else {
previous.next = curr.next;
}
} else {
System.out.println("Node not found!");
//curr.next = null; // Not sure If this is needed
}
}
showList(front);
}
public void printNodes(String[] len){
int j;
for (j = 0; j < len.length; j++){
addNode(front, len[j]);
} showList(front);
}
public void showList(BigNode front){
BigNode current;
current = front;
while ( current.next != null){
System.out.print(current.dataitems + ", ");
current = current.next;
}
System.out.println(current.dataitems);
}
public static void main(String[] args) {
String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"};
BigNode x = new BigNode();
x.printNodes(names);
Scanner in = new Scanner(System.in);
String delete = in.next();
x.deleteNode(x.front, delete);
}
String[] names = {name1, name2, name3, name4}
-First it prints the list, then ask what name to delete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:好的,我已经发现您发布的示例代码有什么问题。
您正在调用
Scanner.next()
来读取单个单词。所有节点值都是两个单词。因此,如果我输入“Sally Mae”,它实际上只是在寻找“Sally”。这与 BigNode 中的大部分代码无关(尽管这当然可以变得更优雅)。 这样的:
基本上是
现在我强烈建议您不要只是更改代码,而是考虑一下您可以自己诊断此问题的方法:
如果您尝试一些或最好所有这些方法,您将从这个问题中学到的东西不仅仅是如何使用
扫描仪
...在不同的地方,您使用 == 运算符比较字符串引用。只有当您传入对列表中存在的实际字符串对象之一的引用时,才会找到您的节点 - 而不是对 equal 字符串对象的引用。
您想要类似的东西:(
但要仔细检查
null
)。EDIT: Okay, I've found out what's wrong with the sample code you've posted.
You're calling
Scanner.next()
which reads a single word. All of your node values are two words. So if I type in "Sally Mae" it's actually just looking for "Sally".This has nothing to do with the majority of the code in
BigNode
(although that could certainly be made more elegant). Basically this:should be
Now I would strongly suggest that you don't just change the code, but instead think about the ways you could have diagnosed this for yourself:
If you try some or preferrably all of those approaches, you'll have learned a lot more from this question than just how to use
Scanner
...In various places, you're comparing string references by using the == operator. That will only find your node if you pass in a reference to one of the actual string objects which exists in the list - not a reference to an equal string object.
You want something like:
(but with careful
null
checking).您应该使用 String.equals 比较而不是 == 比较。
应该是:
You should be using String.equals comparison instead of == comparison,.
should be:
您可以与
==
进行比较。对于int
,它比较值,但对于String
,仅比较引用。因此,您想删除具有相同值但在不同对象中的字符串,这将失败。请改用 String.equals() 。
You do comparisons with
==
. Forint
this compares the values, but forString
only the references. So is you want to remove a String with the same value but in different objects this will fail.Use
String.equals()
instead.您应该始终使用 equals() 来比较对象值,而不是 == 。例如,在您的代码中,这一行:
应该写为:
You should always use equals() for comparing object values, not == . For instance, in your code this line:
should have been written as: