java中LinkedList的问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 13:40:51
public class Test2{
public static void main(String[] args){
LinkedList1 ll=new LinkedList1();
for(int i=0;i<25;i++){
ll.add(new Cat(i));
}
while(ll.hasNext()){
System.out.println(ll.next());
}
while(ll.hasNext()){
System.out.println(ll.next());
}
}
}

class LinkedList1{
private Node n;
private Node nn;
private Node head;
private Node tell;
private int index;
private int currentIndex=0;
private Object o;
public void add(Object o){
n=new Node(o,null);
if(head==null){
head=n;
tell=n;
}
tell.setNode(n);
tell=n;
index++;
}
public int size(){
return index;
}
public boolean hasNext(){
if(currentIndex>=index){
currentIndex=0;
return false;

}
else{
return true;
}
}
public Object next(){<

//单链表类

package dataStructure.linearList;
import dataStructure.linearList.Node; //导入单链表结点类
import java.util.Iterator; //导入迭代器接口

public class SinglyLinkedList<E> extends AbstractList<E> implements LList<E> //单链表类,实现线性表接口
{
protected Node<E> head; //头指针,指向单链表第1个结点

public SinglyLinkedList() //构造空单链表
{
this.head = null;
}

public SinglyLinkedList(Node<E> head) //构造指定头指针的单链表
{
this.head = head;
}

public boolean isEmpty() //判断单链表是否为空,O(1)
{
return this.head==null;
}

public int length() //返回单链表长度
{ //单链表遍历算法,O(n)
int i=0;
Node<E> p=this.head;
whil