4

我一直在做一个项目,我必须实现一个 java 类来实现双向链表的使用。我用我所有的方法完成了 LinkedList 类。我只是不确定如何将节点对象实际添加到列表中。到目前为止,这是我的代码,底部有测试。任何帮助,将不胜感激。谢谢

public class LinkedList {

    private Node first;
    private Node current;
    private Node last;
    private int currentIndex;
    private int numElements;

    public LinkedList() {
        this.first = null;
        this.last = null;
        this.numElements = 0;
        this.current = null;
        this.currentIndex = -1;
    }

    private class Node {

        Node next;
        Node previous;
        Object data;
    }

    public boolean hasNext() {
        return (current != null && current.next != null);
    }

    public Object next() {
        if (!this.hasNext()) {
            throw new IllegalStateException("No next");
        }

        current = current.next;
        return current.data;

    }

    public boolean hasPrevious() {
        return (current != null && current.previous != null);

    }

    public Object previous() {
        if (!this.hasPrevious()) {
            throw new IllegalStateException("No previous");
        }
        current = current.previous;
        return current.data;

    }

   int nextIndex() {
        int index = numElements;
        if (hasNext()) {
            index = this.currentIndex + 1;
        }
        System.out.println(index + "The current index is " + current);
        return index;
    }

    int previousIndex() {
        int index = -1;
        if (hasPrevious()) {
            index = this.currentIndex - 1;
        }
        System.out.println(index + "The current index is " + current);
        return index;
    }

    public void set(Object o) {
        if (this.current == null) {
            throw new IllegalStateException("No node found, cannot set.");
        }
        current.data = o;
    }

    public int size() {
        return numElements;
    }

    public void add(Object o) {       
        Node newNode = new Node();
        newNode.data = o;
        if (first == null) {
            first = newNode;
            last = newNode;
            newNode.next = null;

        } else if (first != null) {
            if (current == null) {
                newNode.previous = null;
                newNode.next = first;
                first.previous = newNode;
                first = newNode;
            } else if (current == last) {
                newNode.previous = current;
                newNode.next = null;
                current.next = newNode;
                last = newNode;
            } else {
                newNode.previous = current;
                newNode.next = current.next;
                current.next.previous = newNode;
                current.next = newNode;
            }
        }
        current = newNode;
        numElements++;
        currentIndex++;

    }

    public void remove() {
        if (current != null) {
            if (current == first && current == last) {
                first = null;
                last = null;
            } else if (current == last) {
                current.previous = null;
                last = current.previous;
            } else if (current == last) {
                current.previous.next = null;
                last = current.previous;
            } else {
                current.previous.next = current.next;
                current.next.previous = current.previous;
            }
            current = current.next;
            numElements--;
        }
    }
}



import java.util.Scanner;


public class LinkedListTest {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String name;
        int index;

        LinkedList<Object> listOne = new LinkedList<Object>();

        listOne.add(object o);

    }
}
4

7 回答 7

4

发布的类 LinkedList 对我来说看起来很实用。

确保您的测试代码不会混淆java.util.LinkedListJava 为您提供的此类和 (它是现有 Collections 框架的一部分)。

为清楚起见,我建议将您的课程重命名为MyLinkedList

以下代码有效,输出为“0”,“2”:

public class MyLinkedListTest {

    public static final void main(String[] args) {

        MyLinkedList list = new MyLinkedList();
        System.out.println("Number of items in the list: " + list.size());

        String item1 = "foo";
        String item2 = "bar";

        list.add(item1);
        list.add(item2);

        System.out.println("Number of items in the list: " + list.size());      

        // and so on...
    }

}
于 2013-03-07T22:55:21.867 回答
2

如果您的代码已编译,我会感到惊讶,因为您的类实际上不是通用的。只需将其初始化为LinkedList listOne = new LinkedList();(无尖括号)。

至于实际添加元素,您只需要添加一些的实例Object即可;任何事情都会做(假设您的内部代码正常工作)。在最后尝试一下:

Object objectToAdd = "Strings are Objects";
listOne.add(objectToAdd);
objectToAdd = new File("C:\\foo.bar"); // Or use any other Objects!
listOne.add(objectToAdd);
于 2013-03-07T22:34:37.953 回答
1

LinkedList没有泛型类型,因此您不能将其声明为

LinkedList<Object> listOne = new LinkedList<Object>();

而是作为

LinkedList listOne = new LinkedList();

现在添加元素只需使用您的add方法

listOne.add("something");
listOne.add(1);//int will be autoboxed to Integer objects

此外,如果您想从键盘添加数据,您可以使用类似

String line="";
do{
    System.out.println("type what you want to add to list:");
    line = keyboard.nextLine();
    listOne.add(line);
}while(!line.equals("exit"));
于 2013-03-07T22:37:51.960 回答
1

考虑编号列表并查看元素之间的关系

说我有清单:

  1. 一个
  2. C

我必须对关系做什么才能得到列表:

  1. 一个
  2. 新节点
  3. C

B的新下一个节点是NewNode C的新的前一个节点是NewNode。因此,插入函数会想知道紧邻的前一个节点或紧邻的下一个节点,然后调整关系。

于 2013-03-07T22:34:21.480 回答
0

线

LinkedList<Object> listOne = new LinkedList<Object>();

除非您将类声明更改为

class LinkedList<T>

或者你也可以写

LinkedList listOne = new LinkedLis();

之后,您应该能够将对象添加到列表中。但是,您需要创建一个对象来添加到它,listOne.add(object o);不会这样做 - 至少您必须编写listOne.add(new Object()). (您的代码没有实例化 Object,没有您已经调用过的 Object,o此外,object o在 Java 中没有任何意义,也不会编译。

于 2013-03-07T22:41:12.963 回答
0

正如人们所提到的,您的列表并不通用。但是,由于他们建议您摆脱该参数,您也可以将<Object>或添加<E>到您的链表实现中,并保持列表的初始化不变。

因此,在您的链表类中,您应该执行以下操作:

public class LinkedList<E>

这将确保您在使用时LinkedList<Object> listOne = new LinkedList<Object>();E将被确认为Object

于 2013-03-07T22:42:00.900 回答
0

让我们稍微改进一下您的测试,以便您的问题出在哪里(如果有的话)我注释掉了对 current() 方法的调用,因为您没有包含一个。(我会不理会它,因为它可能会让您感到困惑。)一般的想法是将项目添加到链接列表中,并在它中前后移动,检查每一步的项目。

public class LinkedListTest {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String name;
        int index;

        LinkedList listOne = new LinkedList();
        //Initially we should be empty so we are positioned
        // at both the beginning and end of the list
        assert listOne.size() == 0 :"List should be empty";
        assert listOne.hasPrevious()==false: "Should be at the beginning of the list";
        assert listOne.hasNext()==false : "Should be at the end of the list";

        Object firstNode = "I am the first node";
        listOne.add(firstNode); //we've added something
//I left this commented out since you don't have a current() method.
//        assert firstNode == listOne.current() : "Our current item should be what we just added";
        assert listOne.hasPrevious()==false : "Should not have moved forward in our list yet";
        assert listOne.hasNext()==true : "should have an item after our current";
        assert listOne.size() == 1 : "Should only have one item in the list";
        Object secondNode = "I am the second node";
        listOne.add(secondNode);
        assert listOne.size() == 2 : "Should only have two items in the list";

        assert firstNode == listOne.next() : "1st call to next should return the 1st node";
        assert listOne.hasPrevious()==true : "We should be positioned after the 1st node";
        assert listOne.hasNext()==true : "We should be positioned before the 2nd node";
    }
}
于 2013-03-07T22:47:24.973 回答