【问题标题】:Implementing a Circular Link List in Java在 Java 中实现循环链​​表
【发布时间】: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


【解决方案1】:

您所面临的所有问题似乎都是因为您没有花时间思考您的代码必须在纸上做什么。我向你保证,如果你开始在纸上绘制你的列表,用链接和箭头表示.next 属性的情况,你需要编写什么行为会突然变得更加明显

1。插入问题

您将面临的第一个也是最明显的问题是您的插入方法。

已经没有链接了

这个很好,即使你可以再添加一行使其成为圆形,它会更好。

列表中已有链接的情况

这是问题开始的地方。该练习要求您在当前链接之后添加链接。它以current.next = newLink; 开头,但下一行newLink = current; 对您没有任何作用,因为它只是将current 的值分配给newLink

要考虑的第二个问题是,您永远不会检查当前是否还没有下一个链接。如果是这样,那么您的代码将有效地分离链接到current 链接的所有链接。找到一个解决方案来验证 next 是否已经设置,如果是,您需要找到一种方法在两个现有链接之间插入新链接。

循环性

我觉得它是圆形的事实让你害怕。真的不应该。即使只有一件物品,你也可以是圆形的。在这种情况下,current.next 将与 current 相同。你已经有了一个变量来知道你有多少元素,所以只需要依靠它来知道该怎么做。事实上,一旦正确编码,您将永远不会有一个 .next 等于 null。唯一的例外是当列表为空时 current 将等于 null (即使您也可以避免这种情况,但没有必要)

2。 displayList问题

这里是您需要循环列表的地方。您有正确的想法,但是由于您没有循环列表,因此最终 current.next 之一将指向 null,因此您的原始问题。如果你修复你的圆化并插入显示将至少在插入后的第一个工作

3。删除问题

分配是删除列表中的下一项。您首先将要删除的元素存储在一个临时变量中,但您缺少一些要点:

  1. current = current.next; 表示您正在失去实际电流。这不是你想要做的。首先你的“光标”不应该移动。其次,此时current.next 是您要从列表中删除的项目。
  2. 您需要多考虑一下您需要做的所有事情才能切断链接。您需要将current.next 对您要移除的项目的引用分离,并将其附加到您要移除的项目之后出现的项目。

我希望我已经清楚了。有很多我不会提及的小怪癖主要是因为它们不会影响功能,它们只是一段可以以更好、更清晰的方式重写的代码。

【讨论】:

  • 感谢您的时间和帮助。不胜感激!
猜你喜欢
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多