【发布时间】:2018-03-26 02:48:37
【问题描述】:
我正在尝试根据以下内容创建循环链接列表: 对列表的唯一访问是单个引用,当前,它可以指向列表上的任何链接,并且可以根据需要在列表中移动。 列表应该处理插入、搜索和删除:这些操作发生在 current 指向的链接的下游一个链接。 能够显示列表。
1) 类循环链表的数据成员应包括引用电流和一个用于跟踪循环链表中链接大小的变量。
2) 类循环中需要定义的方法包括:
• insert:在当前链接之后插入 • 删除:删除一个超出当前的 • find:使用给定键查找链接 • deleteKey:使用给定键删除链接 • displayList:显示列表(列表中的所有链接) • step:将当前链接移至下一个链接 • peek:返回当前存储的数据 • isEmpty:检查列表是否为空
这是我当前的代码(现在它是线性的 - 还没有做成圆形):
public class CircularList<T> {
private Link current; // ref to the current link in the list
private int nLinks; //Reference to number of links in the list
//--------------------------------------------------------------
public CircularList() // constructor
{
current = null;
nLinks = 0;
} //End constructor method - CircularList()
// -------------------------------------------------------------
public void insert(long dd)
{
Link newLink = new Link(dd); // make new link
if (nLinks == 0)
{
current = newLink;
nLinks++;
}
if(nLinks != 0)
{
current.next = newLink; // current --> newLink
newLink = current;
nLinks++;
}
} //End insert()
// --------------------------------------------------------------
public Link delete() // delete link after current
{
Link temp = null;
if(nLinks == 0) //first check whether the list is empty
return null;
if(nLinks != 0)
{
temp = current.next; // save reference to link
current = current.next; // delete it: current-->one after current
nLinks--;
}
return temp; // return deleted link
} //End delete()
// -------------------------------------------------------------
public void displayList()
{
System.out.print("List: ");
int tempLinks = nLinks;
while(tempLinks > 0) // until end of list
{
current.displayLink(); // print data
tempLinks--; // decrement nLinks
current = current.next; // move to next link
}
System.out.println("");
} //End displayList()
// -------------------------------------------------------------
public boolean isEmpty() // true if list is empty
{
return (current==null);
} //End isEmpty()
// -------------------------------------------------------------
public void step() //Move current to next Link
{
current = current.next;
} //End step()
} //End CircularList Class
当它运行时,我得到一个空指针异常。 (当我尝试调用 display 方法时,我认为我的 insert 方法有问题会导致此问题)我很确定它没有正确插入所有 4 个链接。当我尝试显示它时,它只显示数据 2 和 8,它们是插入的第一个和最后一个项目。
我的链接类:
public class Link {
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{
dData = d;
next = null;
}
// -------------------------------------------------------------
public void displayLink() // display this link
{
System.out.print(dData + " ");
}
// -------------------------------------------------------------
} //End Link Class
我的应用类:
public class CircularLinkListApp {
public static void main(String[] args) {
CircularList theList = new CircularList(); // make new list
theList.insert(2); // insert four items
theList.insert(4);
theList.insert(6);
theList.insert(8);
theList.displayList(); // display list
while( !theList.isEmpty() ) // until it's empty,
{
Link aLink = theList.delete(); // delete link
System.out.print("Deleted "); // display it
aLink.displayLink();
System.out.println("");
}
theList.displayList(); // display list
} // end of main()
} //End CircularLinkListApp class
【问题讨论】:
-
你需要先写一些代码。获取您刚刚提供给我们的信息并尝试制作一些东西,如果您需要帮助,请返回该代码。
-
欢迎来到 Stack Overflow!请阅读how to ask a good question。你的问题应该是具体的,你的相关代码应该是minimal, complete, and verifiable。
-
我当前的代码继续给我一个空指针异常。
-
还是贴吧,不然帮不上忙
-
经典循环链表:你可以创建一个代表链表每个节点的类,然后创建一个对节点有单一引用的类,从而处理插入、删除等...
标签: java list hyperlink circular-list