简体   繁体   English

循环双链表

[英]Circular Doubly Linked List

I need help with a Circular Doubly Linked List in Java. 我需要Java中的双向双重链接列表的帮助。

This is my code (originally coded by "sanfoundry"; it uses interfaces): 这是我的代码(最初由“ sanfoundry”编码;它使用接口):

LinkedList.java: LinkedList.java:

public class LinkedList<T extends Comparable<T>> implements
        ILinkedList<T> {

    private ILinkedListNode<T> head;
    private ILinkedListNode<T> end;
    private int size;

    public LinkedList() {
        head = null;
        end = null;
        head = null;
        size = 0;
    }


    @Override
    public void append(T element) { 
        ILinkedListNode<T> tempNode = new LinkedListNode(element, null, null);

        if (head == null) {
            head = tempNode;
            end = head;


        } else {    
            tempNode.setPrev(end);
            tempNode.setNext(tempNode);
            end = tempNode;
        }
        size++;

    }
// should return element at position "index"
    @Override
    public T get(int index) {
        return null;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public ILinkedListNode<T> getHead() {
        return head;
    }


}

Now I need help to get it working. 现在,我需要帮助才能使其正常运行。 Did I do something wrong and what do I have to code in method "public T get (int index)"? 我做错了什么吗?我必须在方法“ public T get(int index)”中编写什么代码? Sorry, but I'm a Java noob :( 抱歉,但我是Java新手:(

EDIT: Is this a possible solution? 编辑:这是可能的解决方案吗?

public T get(int index) { 公共T get(int索引){

T element = null;

if (index == 0) {
    element = head.getElement();

} else if (index == size()-1) {
    element = head.getPrev().getElement(); // end.getElement() also possible

} else {
    ILinkedListNode<T> temp = head;
    for (int i = 0; i < index; i++) {
        temp = temp.getNext();
    }

    element = temp.getElement();

}
return element;

} }

You should traverse the LinkedList, keeping track of your current position as you go. 您应该遍历LinkedList,并在移动时跟踪当前位置。 When your current position is equal to the index passed in, then you can return the T from that node. 当您的当前位置等于传入的索引时,则可以从该节点返回T。

Read about traversing a linked list here . 在此处阅读有关遍历链接列表的信息

Try making some test cases. 尝试做一些测试用例。 Ideally you'll want to use a real test framework but using a normal main method could work. 理想情况下,您将要使用真实的测试框架,但使用常规的main方法可能会起作用。 For example: 例如:

public static void main(String[] args) {
    ILinkedList<String> a = new LinkedList<String>();

    System.out.println(a.size()); // 0
    System.out.println(a.getHead()); // null

    a.append("foo");
    System.out.println(a.size()); // 1
    System.out.println(a.get(0)); // "foo"
    System.out.println(a.get(1)); // decide yourself what this should result in

    a.append("bar");
    System.out.println(a.size()); // 2
    System.out.println(a.get(0)); // "foo"
    System.out.println(a.get(1)); // "bar"

    a.append("baz");
    System.out.println(a.size()); // 3
    System.out.println(a.get(0)); // "foo"
    System.out.println(a.get(1)); // "bar"
    System.out.println(a.get(2)); // "baz"

}

Expand the test as necessary. 根据需要扩展测试。 See if the code returns what you expect it to, or if the code never returns, or throws an exception, etc.... The easiest way to check whether your code is running properly is, after all, to actually run it. 看看代码是否返回了您期望的结果,或者代码是否永不返回,或引发异常等。...毕竟,检查代码是否正确运行的最简单方法是实际运行它。

Hint: the code, as of this writing, has some errors. 提示:撰写本文时,该代码存在一些错误。

Also, if the code can run as expected, consider: 另外,如果代码可以按预期运行,请考虑:

  • Traversing the nodes backward if it's faster than forward. 向后遍历节点的速度比向前快。
  • Using a recursion instead of iteration. 使用递归而不是迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM