我删除了我的上一个帖子,然后再次重写了该问题(在我了解如何做以及有什么要求之后。
1。我需要将Nodedouble定义为通用内部类-private。在此类中,我需要编写使用数据和字符串方法的构造函数。
2。我需要在没有元素的需要构造器的此类中定义Linkedlistdouble。添加元素的方法和显示列表的方法。
3。在LinkedListdouble类中,我需要定义方法listiterator,该方法从listiteartor类型返回对象。在functiob listiterator中,我需要返回3方法: -T next-返回下一个元素 -如果列表iteartor有更多元素,则布尔值具有next-return true -T previous-如果没有元素,则返回列表中的前一个元素-返回异常。
这是我的代码:
在此处输入代码
public class DoublyLinkedList<T> {
/**
*
* @author USER
*
* @param <T>
*/
class Node <T>{
private T data;
private Node previous;
private Node next;
Node(T data) {
this.data = data;
}
public Node previous() {
return this.previous;
}
public Node next() {
return this.next;
}
public T getContent() {
return this.data;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public void setNext(Node next) {
this.next = next;
}
public String toString() {
return data.toString();
}
}
private Node head;
private Node tail;
private int size = 0;
public Node head() {
return this.head;
}
public Node tail() {
return this.tail;
}
public void add(T element) {
Node newNode = new Node(element);
if (this.size == 0) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.setNext(newNode);
newNode.setPrevious(this.tail);
this.tail = newNode;
}
this.size++;
}
/*
public boolean isEmpty(){
return size==0;
}
public void push(int data){
Node n= new Node(data);
if(isEmpty()) {
tail=n;
head=n;
}
else {
head.previous = n;
n.next=head;
head=n;
}
size++;
}*/
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
Node currentNode = this.head;
while (currentNode != null) {
stringBuilder.append(currentNode.getContent()).append(" ");
currentNode = currentNode.next();
}
System.out.println(stringBuilder.toString().trim());
return stringBuilder.toString().trim();
}
public ListIterator listIterator() {
return new ListIterator() {
Node curr;
public boolean hasNext() {
return curr.next() != null;
}
public T next() {
T element = (T) curr.getContent();
curr = curr.next();
return element;
}
public T previous() {
T element = (T) curr.getContent();
if(curr.previous()!=null) {
curr = curr.previous();
return element;
}
throw new NoSuchElementException();
}
@Override
public boolean hasPrevious() {
if(curr.previous() != null) {
return true;
}
return false;
}
@Override
public void add(Object e) {
throw new UnsupportedOperationException();
}
@Override
public int nextIndex() {
throw new UnsupportedOperationException();
}
@Override
public int previousIndex() {
throw new UnsupportedOperationException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(Object e) {
throw new UnsupportedOperationException();
}
};
}
public static void main (String[]args) {
DoublyLinkedList<Integer> a = new DoublyLinkedList();
a.add(7);
a.add(8);
a.add(9);
a.add(10);
a.toString();
ListIterator <Integer> iter = a.listIterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
我的问题: 1。 我尝试在主体中执行此代码时遇到错误: 我该如何解决? (主要在上面)
谢谢! 祝你有美好的一天
答案 0 :(得分:0)
对listIterator所做的更改:
节点curr = head //否则为空
将curr.next()更改为hasNext()方法,否则您不会得到最后一个元素。
public boolean hasNext() {
return curr != null;
}
希望有帮助
RR
答案 1 :(得分:0)
最直接的方法是在listIterator的头部或尾部添加一个参数:
ListIterator<Integer> iter = a.listIterator(a.getHead());
while (iter.hasNext()) {
System.out.println(iter.next());
}
iter = a.listIterator(a.getTail());
while (iter.hasPrevious()) {
System.out.println(iter.previous());
}
我这样修改了ListIterator:
public ListIterator listIterator(Node start) {
return new ListIterator() {
DoublyLinkedList.Node curr = start;
public boolean hasNext() {
//System.out.println(curr);
return curr != null;
}
public T next() {
T element = (T) curr.getContent();
curr = curr.next();
return element;
}
public T previous() {
T element = (T) curr.getContent();
curr = curr.previous();
return element;
}
@Override
public boolean hasPrevious() {
return curr != null;
}
也许有更好的方法可以做到这一点,例如创建自己的迭代器。 RR