【问题标题】:How to implement circular linked list in java?如何在java中实现循环链​​表?
【发布时间】:2015-01-04 16:38:14
【问题描述】:

我读了一本关于“数据结构和算法”的书,其中有一项要求我实现循环链​​表的作业。这是一个学习练习,我的代码可能不是很高的标准。

我实现循环链​​表背后的主要思想是有一个指向最后一个元素的指针,每次我添加新项目时,最后一个项目的“下一个”字段将被刷新以指向新的添加项目。

插入方法工作正常,我可以毫无问题地添加项目,但由于某种原因我无法从列表中删除项目。

这里是“链接”或“节点”的代码:

public class Link {
  public long data;
  public Link next;

  public Link(long val) {
    data = val;
    next = null;
  }

  public void displayLink() {
    System.out.print(data + " ");
  }

}  // end class

这是执行工作的类的代码,错误显然在这里:

public class CircularList {
Link first;
Link last;

public CircularList() {
     first = null;
     last = null;
}

public Link find(long key) {
    Link current = first;
    while(current.data != key) {
        current = current.next;
    }
    return current;
} // end find

public Link delete() {
    if(first.next == null) 
        last = null;
    Link temp = first;
    first = first.next;
    return temp;
}  // end delete

public boolean isEmpty() { return (first == null); }

public void insert(long val) {
    Link newLink = new Link(val);

    if(isEmpty())
        last = newLink;

    newLink.next = first;
    first = newLink;
    last.next = first;
} // end insert

public void displayAmount(int n) {
    Link current = first;
    while(n>0) {
        current.displayLink();
        current = current.next;
        n--;
    }
    System.out.println("");
} // end displayAmount

}  // end class

以及主应用代码:

public class App {
public static void main(String[] args) {
    CircularList cl = new CircularList();

    cl.insert(10);
    cl.insert(20);
    cl.insert(30);
    cl.insert(40);

    cl.displayAmount(6);

    cl.delete();

    cl.displayAmount(6);
}
}  // end class

显示量看起来有点傻,我只是试图避免无限循环并做了一些简单的工作。

【问题讨论】:

  • 你的问题是什么?
  • 您的链表节点缺少对前一个节点的引用,因此无法删除。您希望最后一个元素引用第一个以及第一个到最后一个,这意味着两者都需要下一个和上一个。有了这些,您可以获取任何元素,获取当前元素的上一个和下一个,并将上一个和下一个连接起来,有效地切割出要删除的元素。
  • @G_V delete() 方法总是(试图)删除第一个元素,这没关系,因为它的前身是last。如果您想删除任意元素,则需要将其进行双重链接,但如果您只删除 first,则不需要。
  • 这个链接可能对你有帮助sanfoundry.com/…

标签: java data-structures


【解决方案1】:

在您的 delete() 函数中,在 return temp 之前添加 last.next = first

public Link delete() {
    if(first.next == null) 
        last = null;
    Link temp = first;
    first = first.next;
    if(last != null)
        last.next = first
    return temp;
} 

更新:

我找不到满足first.next == null 的场景,我们应该考虑在空列表上调用 delete()。

public Link delete() {
    Link temp = first;
    if(first == null){
        ;  // or you can throw some exception as a warning
    }
    else if(first==last){  // only one element
        first = null; // reset to initial state
        last = null;
    }
    else{
        first = first.next;
        last.next = first;
    }
    return temp;
} 

【讨论】:

  • 你不需要你的null 检查:如果first 不为空,那么last 也不能为空(因为如果你只有一个元素,那么@987654329 @)。
  • @chiastic-security 只要 first.next == null 可能为真,我认为我们需要进行 null 检查,因为我们将 last 设置为 null。但是,我注意到这可能永远不会发生,因此我更新了我的答案并为另一个我们在空列表中删除()的案例添加了防御性代码。
【解决方案2】:

您的delete() 方法无法处理列表的循环性。最后一个元素指向第一个元素;删除第一个元素时也需要更新。

换句话说,您需要将last.next 设置为指向新的first,而不是旧的first

您遇到的另一个问题是,如果您删除最后一个元素(使其现在为空),那么您还需要将last 设置为null

public Link delete() {
    if (first.next == null) {
        first = null;
        last = null;
    }
    Link temp = first;
    first = first.next;
    last.next = first;
    return temp;
} 

【讨论】:

  • 你的变种也可以工作。至少对我来说,它很清楚地传达了这个想法。
猜你喜欢
  • 1970-01-01
  • 2018-03-26
  • 2012-09-02
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多