【发布时间】:2010-11-16 18:08:35
【问题描述】:
是的,这是我的家庭作业项目之一 - 实现基于单链表的循环链表。这很简单,代码很容易阅读。我所有的属性都是公开的,以避免 getters 和 setters 和私人业务。就本项目而言,public 就足够了。
我在属性字段中初始化了我的 nItems 计数器(列表中的项目数)和链接头,但稍后我将通过在构造函数内部对其进行初始化来更改它。
我的 step() 方法似乎根本不起作用。我的编译器只是冻结了一会儿,然后什么都没有出现。如果我调用它 4 次,这就是 step() 方法的工作方式:
List: 40 80 30 20 10 70 50
List: 80 30 20 10 70 50 40
List: 30 20 10 70 50 40 80
List: 20 10 70 50 40 80 30
Find() 方法工作正常,只要我们要搜索的值在链接列表内。如果不是,它将永远持续下去。如果这是我的列表,如果您搜索不存在的值(我逐步调试它),find() 方法会发生这种情况:
70 60 50 40 30 20 10 10 10 10 10 10 10 10...and 10 forever
原因:如果我们要搜索的键值不存在,它将永远不会从 while 循环中退出,因此会永远重复 current = current.next .
while(current.dData != key) {
current = current.next;
我的删除方法说它删除了值 60,就像我想要的那样,但这是我得到的:
Deleted link with key 60
70 60 40 30 20 10 // lie! it deletes 50 instead of 60
还请查看我的 display() 和 insert() 方法。它们对我来说看起来不错,但我可能错了,这可能是我在使用 find() 和 delete() 方法时遇到的所有问题的根源。
提前谢谢你!!!
class Link {
public int dData;
public Link next;
public Link(int dd) {
dData = dd;
}
public void displayLink() {
System.out.print(dData + " ");
}
}
class CircList {
public Link head = null;
int nItems = 0;
public boolean isEmpty() {
return (nItems == 0);
}
// INSERT
public void insert(int key) {
Link current = new Link(key);
if(isEmpty())
head = current;
current.next = head;
head = current;
nItems++;
}
// STEP
public void step() {
Link current = head;
do {
current = current.next;
} while(current != head);
}
// FIND
public Link find (int key) {
Link current = head;
while(current.dData != key) {
current = current.next;
}
return current;
}
// DELETE
public Link delete(int key) {
Link current = head;
while(current.dData != key) {
current = current.next;
}
if(current == head)
head = head.next;
else {
current.next = current.next.next;
nItems--;
}
return current;
}
// DISPLAY
public void displayList() {
Link current = head; // start at beginning
int counter = nItems;
while(true) {
if(counter != 0) {
current.displayLink();
current = current.next; // move to next link
counter--;
}
else
break;
}
}
}
class CircListApp {
public static void main(String[] args) {
Link f, d;
CircList theList = new CircList();
theList.insert(10); // insert items
theList.insert(20);
theList.insert(30);
theList.insert(40);
theList.insert(50);
theList.insert(60);
theList.insert(70);
//System.out.println(theList.nItems + " ");
theList.displayList(); // display list
System.out.println("");
f = theList.find(30); // find item
if( f != null)
System.out.println("Found link with key " + f.dData);
else
System.out.println("Can't find link with key 30");
// theList.step();
//
// System.out.println("Inserting link with key 80");
// theList.insert(80);
// theList.displayList(); // display list
//
d = theList.delete(60); // delete item
if( d != null )
System.out.println("Deleted link with key " + d.dData);
else
System.out.println("Can't delete link with key 60");
theList.displayList(); // display list
//
// f = theList.find(99); // find item
// if( f != null)
// System.out.println("Found link with key " + f.dData);
// else
// System.out.println("Can't find link with key 99" );
// theList.displayList(); // display list
//
// d = theList.delete(13); // delete item
// if( d != null )
// System.out.println("Deleted link with key " + d.dData);
// else
// System.out.println("Can't delete link with key 13");
// theList.displayList(); // display list
//
// System.out.println("Stepping through list");
// for(int j=0; j<theList.getSize; j++)
// {
// theList.step();
// theList.displayList();
// }
//
// System.out.println("Will delete and step one by one");
// while(theList.isEmpty() == false)
// {
// theList.delete();
// theList.step();
// theList.displayList();
// }
} // end main()
}
【问题讨论】:
-
要修复 Find(),请查看 Step() 以了解如何停止。
-
我得到了我需要使用 do-while 循环的提示?但这仍然不会改变任何事情。我觉得我在 while 语句中缺少另一个条件,或者我需要引入某种计数器,例如while(nItems !0) nItems--;
-
其实你不需要柜台!您可以使用“current == head”来检查您是否遍历了整个列表。
-
目前不鼓励使用作业标签。 meta.stackexchange.com/questions/10811/…
-
从该线程中并不清楚标签是否有很多共识。我觉得它很有用...
标签: java data-structures linked-list circular-list