gpt4 book ai didi

java - 链表的迭代器

转载 作者:行者123 更新时间:2023-11-30 03:13:51 24 4
gpt4 key购买 nike

我的项目应该实现两个类。基本链表和排序链表。一切似乎都工作正常,除了由于某种原因我无法迭代排序的链表。类结构如下:

public class BasicLinkedList<T> implements Iterable<T> {
public int size;

private class Node {
private T data;
private Node next;

private Node(T data) {
this.data = data;
next = null;
}
}

private Node head;
private Node tail;

public BasicLinkedList() {
head = tail = null;
}
//Add, remove method

public Iterator<T> iterator() {
return new Iterator<T>() {

Node current = head;

@Override
public boolean hasNext() {
return current != null;
}

@Override
public T next() {
if(hasNext()){
T data = current.data;
current = current.next;
return data;
}
return null;
}

@Override
public void remove(){
throw new UnsupportedOperationException("Remove not implemented.");
}

};

现在,当我测试这个类时,它工作得很好。迭代器可以工作,我可以测试一切。问题出在扩展了这个类的排序链表类中。这是它的实现和我在构造函数中使用的比较器类:

public class SortedLinkedList<T> extends BasicLinkedList<T>{
private class Node{
private T data;
private Node next;

private Node(T data){
this.data = data;
next = null;
}
}

private Node head;
private Node tail;
private Comparator<T> comp;

public SortedLinkedList(Comparator<T> comparator){
super();
this.comp = comparator;
}

这是比较器类和我在单独的类中运行的测试:

public class intComparator implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
}

public static void main(String[] args) {
System.out.println("---------------SortedLinkedList--------------");
SortedLinkedList<Integer> sortedList = new SortedLinkedList<Integer>(new intComparator());
sortedList.add(3);
sortedList.add(5);
sortedList.add(2);
for(int i: sortedList){
System.out.println(i);
}
}

什么也没有打印出来。我假设继承的迭代器会帮助我遍历这个,没有问题,并且显然它是合法的,因为 for-each 循环会编译。只是什么也没有打印出来。我调试了它,所有添加、删除的内容都按预期工作。只是迭代器没有做它应该做的事情。我应该为这个类创建一个单独的新迭代器吗?但这不是多余的代码吗,因为我已经继承了它?感谢帮助!

编辑:这是排序列表的添加方法

public SortedLinkedList<T> add(T element){
Node n = new Node(element);
Node prev = null, curr = head;
if(head == null){
head = n;
tail = n;
}
//See if the element goes at the very front
else if(comp.compare(n.data, curr.data) <= 0){
n.next = head;
head = n;
}
//See if the element is to be inserted at the very end
else if(comp.compare(n.data, tail.data)>=0){
tail.next = n;
tail = n;
}
//If element is to be inserted in the middle
else{
while(comp.compare(n.data, curr.data) > 0){
prev = curr;
curr = curr.next;
}
prev.next = n;
n.next = curr;
}

size++;
return this;
}

最佳答案

1) SortedLinkedList 扩展了 BasicLinkedList 但两者都有

private Node head; 
private Node tail

这是错误的。如果你想在子类中继承这些字段,你应该在父类(super class)中将这些变量标记为 protected ,然后将它们从子类中删除。

2) 私有(private)类 Node 也是如此。您在 SortedLinkedListBasicLinkedList 中声明 Node 类。您应该做的是声明一次(也许在父类(super class)中?)并在两个地方使用相同的类。如果这样做,两个类都应该可以访问构造函数和字段。因此,您必须更改访问修饰符(private 是您现在拥有的)。

我将发布下面有效的代码,但我没有花任何时间进行设计。只是发布它来演示如何更改代码以使其工作。您必须决定使用哪些访问修饰符以及将类放在哪里。

import java.util.Comparator;
import java.util.Iterator;

public class Test {
public static void main(String[] args) {
System.out.println("---------------SortedLinkedList--------------");
SortedLinkedList<Integer> sortedList = new SortedLinkedList<Integer>(new intComparator());
sortedList.add(3);
sortedList.add(5);
sortedList.add(2);
for (int i : sortedList) {
System.out.println(i);
}
}
}

class BasicLinkedList<T> implements Iterable<T> {
public int size;

class Node {
T data;
Node next;

Node(T data) {
this.data = data;
next = null;
}
}

protected Node head;
protected Node tail;

public BasicLinkedList() {
head = tail = null;
}

// Add, remove method

public Iterator<T> iterator() {
return new Iterator<T>() {

Node current = head;

@Override
public boolean hasNext() {
return current != null;
}

@Override
public T next() {
if (hasNext()) {
T data = current.data;
current = current.next;
return data;
}
return null;
}

@Override
public void remove() {
throw new UnsupportedOperationException("Remove not implemented.");
}

};

}
}

class SortedLinkedList<T> extends BasicLinkedList<T> {


private Comparator<T> comp;

public SortedLinkedList(Comparator<T> comparator) {
super();
this.comp = comparator;
}

public SortedLinkedList<T> add(T element) {
Node n = new Node(element);
Node prev = null, curr = head;
if (head == null) {
head = n;
tail = n;
}
// See if the element goes at the very front
else if (comp.compare(n.data, curr.data) <= 0) {
n.next = head;
head = n;
}
// See if the element is to be inserted at the very end
else if (comp.compare(n.data, tail.data) >= 0) {
tail.next = n;
tail = n;
}
// If element is to be inserted in the middle
else {
while (comp.compare(n.data, curr.data) > 0) {
prev = curr;
curr = curr.next;
}
prev.next = n;
n.next = curr;
}

size++;
return this;
}
}

class intComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
}

关于java - 链表的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com