我一直致力于一个项目,我必须实现一个实现双链表使用的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);
}
}
答案 0 :(得分: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...
}
}
答案 1 :(得分: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);
答案 2 :(得分:1)
考虑编号列表并查看元素之间的关系
说我有清单:
我需要对关系做什么才能获得列表:
B的新下一个节点是NewNode C的新上一个节点是NewNode。因此,插入函数需要知道前一个节点或下一个节点,然后调整关系。
答案 3 :(得分: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"));
答案 4 :(得分:0)
该行
LinkedList<Object> listOne = new LinkedList<Object>();
将无法编译
class LinkedList<T>
或者你也可以写
LinkedList listOne = new LinkedLis();
之后,您应该能够将对象添加到列表中。但是,您需要创建一个要添加的对象,listOne.add(object o);
不会这样做 - 至少您必须编写listOne.add(new Object())
。 (您的代码没有实例化Object,没有您已经调用的对象o
,此外,object o
在Java中没有任何意义,也不会编译。
答案 5 :(得分:0)
正如人们提到的,你的清单不是通用的。但是,当他们建议您删除参数时,您也可以将<Object>
或<E>
添加到链接列表实现中,并保持列表的初始化。
因此,在链表类中,您应该执行以下操作:
public class LinkedList<E>
这将确保您在使用LinkedList<Object> listOne = new LinkedList<Object>();
时,将E
与Object
联系起来
答案 6 :(得分: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";
}
}