【问题标题】:DoublyLinkedList Project: Example inputDoubleLinkedList 项目:示例输入
【发布时间】:2011-12-02 20:35:25
【问题描述】:

我目前正在我的大学 Java 课程中从事双向链表项目。我了解双向链表、链表和列表的概念。但是,我在编写程序时遇到了很多麻烦,因为我不确定如何创建需要在我的方法中修改的数据。我们的教授通常会向我们提供他将使用的输入,但这次没有,而且我似乎无法在我的研究中弄清楚。

我想我的主要问题是任何人都可以为我编写一些代码来开始工作并开始了解我的方法需要做得更好吗?

这是我目前所拥有的。 (基本上只是覆盖骨架..)

非常感谢您的帮助。

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class DoublyLinkedList<E> implements List<E>{

DoublyLinkedListNode header;

public static void main(String[] args) {

}
public boolean add(E e) {
    return false;
}
public void add(int index, E element) {

}
public boolean addAll(Collection<? extends E> c) {
    return false;
}
public void clear() {
    header=null;
}
public boolean contains(Object o) {
    return false;
}
public E get(int index) {
    return null;
}
public int indexOf(Object o) {
    return 0;
}
public boolean isEmpty() {
    return header == null;
}
public int lastIndexOf(Object o) {
    return 0;
}
public ListIterator<E> listIterator() {
    return null;
}
public boolean remove(Object o) {
    return false;
}
public E remove(int index) {
    return null;
}
public int size() {
    return 0;
}
public Object[] toArray() {
    return null;
}
private class DoublyLinkedListNode{
    DoublyLinkedListNode next;
    DoublyLinkedListNode last;
    E contents;
}

//extra credit
private class DoublyLinkedListItr  implements java.util.ListIterator{

    public void add(Object arg0) {

    }
    public boolean hasNext() {

        return false;
    }
    public boolean hasPrevious() {

        return false;
    }
    public Object next() {

        return null;
    }
    public int nextIndex() {

        return 0;
    }
    public Object previous() {

        return null;
    }
    public int previousIndex() {

        return 0;
    }
    public void remove() {

    }
    public void set(Object arg0) {

    }

}
public ListIterator<E> listIterator(int index) {
    throw new UnsupportedOperationException("not implemented");
}
public <T> T[] toArray(T[] a) {
    throw new UnsupportedOperationException("not implemented");
}
public List<E> subList(int fromIndex, int toIndex) {
    throw new UnsupportedOperationException("not implemented");
}
public boolean retainAll(Collection<?> c) {
    throw new UnsupportedOperationException("not implemented");
}
public E set(int index, E element) {
    throw new UnsupportedOperationException("not implemented");
}
public boolean removeAll(Collection<?> c) {
    throw new UnsupportedOperationException("not implemented");
}
public boolean addAll(int index, Collection<? extends E> c) {
    throw new UnsupportedOperationException("not implemented");
}
public Iterator<E> iterator() {
    throw new UnsupportedOperationException("not implemented");
}
public boolean containsAll(Collection<?> c) {
    throw new UnsupportedOperationException("not implemented");
}

}

【问题讨论】:

  • 真的要实现List接口才能入手吗?

标签: list linked-list doubly-linked-list


【解决方案1】:

在这里创建数据是sn-p:

public static void main(String[] args) {
  DoublyLinkedList<String> doublyLinkedList = new DoublyLinkedList<String>();
  doublyLinkedList.add("Hello");
  doublyLinkedList.add("World");
  // If you want to store int
  DoublyLinkedList<Integer> dlli = new DoublyLinkedList<Integer>();
  dlli.add(new Integer(10));
  dlli.add(new Integer(5));
}

我希望这就是你要找的。​​p>

【讨论】:

    【解决方案2】:

    创建一个节点并将值存储在其中。

    如果 header 为 null,则让 header 引用您的新节点。

    如果header不为null,取指向的节点,只要next不为null,取next引用的节点。如果 next 为 null 您在列表的末尾,请获取下一个引用并让它引用您新创建的节点。然后获取新节点的最后一个(我将其称为上一个)引用,并让它引用您找到的节点(列表末尾)。

    这应该让你开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 2012-10-19
      • 2013-05-13
      相关资源
      最近更新 更多