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が提供するこのクラスとを混同しないようjava.util.LinkedListにしてください(これは既存の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

番号付きリストを考えて、要素間の関係を見てください

私がリストを持っているとしましょう:

  1. A
  2. B
  3. C

リストを取得する関係に何をする必要がありますか:

  1. A
  2. B
  3. NewNode
  4. C

Bの新しい次のノードはNewNodeです。Cの新しい前のノードはNewNodeです。したがって、挿入関数は、直前のノードまたは直前のノードを認識してから、関係を調整する必要があります。

于 2013-03-07T22:34:21.480 に答える
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 に答える
0

人々が言っ​​ているように、あなたのリストは一般的ではありません。ただし、パラメータを削除するようにアドバイスされているので、リンクリストの実装に<Object>または<E>を追加して、リストの初期化をそのままにしておくこともできます。

したがって、リンクリストクラスでは、次のようなことを行う必要があります。

public class LinkedList<E>

これによりLinkedList<Object> listOne = new LinkedList<Object>();Eを使用しているときに、Object

于 2013-03-07T22:42:00.900 に答える
0

この線

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

クラス宣言を次のように変更しない限り、コンパイルされません

class LinkedList<T>

または代わりにあなたはただ書くことができます

LinkedList listOne = new LinkedLis();

その後、リストにオブジェクトを追加できるようになります。ただし、追加するオブジェクトを作成する必要がありますが、作成する必要はありません。listOne.add(object o);少なくとも、を作成する必要がありますlistOne.add(new Object())。(コードはオブジェクトをインスタンス化しません。すでに呼び出したオブジェクトはありません。さらに、Javaで何も意味せず、コンパイルされませんoobject o

于 2013-03-07T22:41:12.963 に答える
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 に答える