[英]Data structures and algorithms in Java
好的,我正在尝试在这本书上做这个编程项目。 这就是它所说的:
循环列表是一个链表,其中最后一个链接指向第一个链接。 有很多方法可以设计循环列表。 有时会有一个指向列表“开始”的指针。 然而,这使得列表不太像一个真正的圆圈,而更像是一个普通的列表,它的结尾与开头相连。 为一个没有结束也没有开始的单向循环列表创建一个类。 对列表的唯一访问是单个引用,当前,可以指向列表上的任何链接。 此引用可以根据需要在列表中移动。 (有关这种循环列表最适合的情况,请参阅编程项目 5.5。)您的列表应该处理插入、搜索和删除。 如果这些操作发生在 current 指向的链接下游的一个链接上,您可能会发现它很方便。 (因为上游链接是单链接的,所以不绕圈子就无法找到它。)您还应该能够显示列表(尽管您需要在任意点断开圆圈以打印在屏幕上)。 将当前移动到下一个链接的 step() 方法也可能会派上用场。
这是我到目前为止所拥有的,我拥有的主要方法无法更改。
class circularL
{
int data;
circularL next;
circularL prev;
public circularL(int d)
{
data = d;
next = null;
prev = null;
}
public int getData()
{
return data;
}
}
class circularLL
{
circularL currPt;
//constructor
public circularLL()
{
currPt = null;
}
/******************************************
* insertLink() function performs *
* inserting a new link with the data item*
* to the existing list. *
*****************************************/
public void insertLink(int dd)
{
circularL theLink = new circularL(dd);
if(currPt==null)
{
theLink.next = theLink;
theLink.prev = theLink;
}
else if(currPt.next==null&&currPt.prev==null&& currPt!=null)
{
theLink.next = currPt;
theLink.prev = currPt;
currPt.prev = theLink;
currPt.next = theLink;
}
else if(currPt != null)
{
theLink.next = currPt.next;
theLink.prev = currPt;
currPt.next.prev = theLink;
currPt.next = theLink;
}
currPt = theLink;
}
/**************************************************
* The search() function search for the particular*
* key by traversing through the linked list. *
*************************************************/
public int find(int key)
{
circularL tempCurr= currPt;
if(tempCurr.data == key)
{
return key;
}
else
{
tempCurr = tempCurr.next;
}
while(tempCurr.data!=currPt.data)
{
if(tempCurr.data==key)
{
return key;
}
else
{
tempCurr = tempCurr.next;
}
}
System.out.println("Key not found!");
return -99;
}
/******************************************
* The delete() function delete the linked*
* list at the current pointer and return *
* the value of the data item. *
*****************************************/
public int delete()
{
circularL tempCurr = currPt;
currPt.next.prev = currPt.prev;
currPt.prev.next = currPt.next;
currPt = tempCurr.prev;
return tempCurr.data;
}
/********************************************
* The printRing() function prints out the *
* linked list by traversing the linked list*
* in either direction. *
*******************************************/
public void displayList(boolean direction)
{
circularL tempLink = currPt;
do
{
System.out.print(tempLink.getData() +" ");
tempLink=direction?tempLink.next:tempLink.prev;
}
while(tempLink.data!=currPt.data);
System.out.println("");
}
}
class CircApp
{
public static void main(String[] args)
{
Link f, d;
circularLL theList = new circularLL(); // make list
theList.insertLink(10); // insert items
theList.insertLink(20);
theList.insertLink(30);
theList.insertLink(40);
theList.insertLink(50);
theList.insertLink(60);
theList.insertLink(70);
theList.displayList(); // display list
f = theList.find(30); // find item
if( f != null)
System.out.println("Found link with key " + f.iData);
else
System.out.println("Can't find link with key 30");
theList.displayList(); // display list
System.out.println("Inserting link with key 80");
theList.insertLink(80);
theList.displayList(); // display list
d = theList.delete(60); // delete item
if( d != null )
System.out.println("Deleted link with key " + d.iData);
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.iData);
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.iData);
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()
}
以下是错误:
1.
CircApp.java:134: error: cannot find symbol
Link f, d;
原因:没有您要引用的Link
类。 解决:创建一个Link
类,或具有类似行为的东西。
在第displayList
:您使用的是not defined
方法displayList
在第 152、172 行:您使用的是not defined
方法delete
在第 182、190 行:您正在使用not defined
方法step
在第 180 行:您正在使用not defined
方法getSize
在第 187 行:您正在使用not defined
方法isEmpty
解决:定义这些方法
如果您想在定义这些方法之前运行代码,至少为它们创建一个空定义或注释这些行。
PS:永远不要在 SO 上发布代码来解决编译错误。 这显然意味着你不想学习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.