当前位置:编程学习 > JAVA >>

求解我写的循环双链表 帮忙看哪里错了 谢谢

package doublessss;



public class DoubleLinkedList<T> implements LList<T>
{
public DLinkNode<T> head;  //头指针

public DoubleLinkedList()
{
this.head = new DLinkNode<T>();
this.head.prev = head;
this.head.next = head;
}

public DoubleLinkedList(T[] element) {
this();
DLinkNode<T> rear=this.head;
for(int i=0;i<element.length;i++)
{
rear.next = new DLinkNode<T>(element[i],rear.prev,this.head);
rear = rear.next;
}
}

public boolean isEmpty()
{
return head.next==head;
}

public int length()
{
int i = 0;
DLinkNode<T> p = this.head.next;
while(p!=head)
{
i++;
p = p.next;
}
return i;
}

public String toString()
{
String str = "(";
DLinkNode<T> p = this.head.next;
while(p!= head)
{
str = str + p.data.toString();
if(p.next!=head)
str = str +",";
p = p.next;
}
return str + ")";
}

public T get(int i)
{
if(i>=0)
{
DLinkNode<T> p = this.head.next;
for(int j=0;p!=head&&j<i;j++)
p = p.next;
if(p!=head)
return p.data;
}
return null;
}

public void set(int i,T x)
{
if(x==null)
return;
if(i>=0)
{
DLinkNode<T> p=this.head.next;
for(int j=0;p!=head&&j<i;j++)
p=p.next;
if(p!=head)
p.data = x;
}
else throw new IndexOutOfBoundsException(i+"");
}

public void insert(int i,T x)
{
if(x==null)
return;
DLinkNode<T> p=this.head;
for(int j=0;p.next!=this.head&&j<i;j++)
p = p.next;
DLinkNode<T> q=new DLinkNode<T>(x,p,p.next);
p.next.prev = q;
p.next = q;
}

public void append(T x)
{
if(x==null)
return;
DLinkNode<T> q=new DLinkNode<T>(x,head.prev,head);
head.prev.next = q;
head.prev = q;
}

public T remove(int i)
{
if(i>=0)
{
DLinkNode<T> p=this.head.next;
for(int j=0;p!=head&&j<i;j++)
p = p.next;
if(p!=head)  
{
T old=p.data;
p.prev.next = p.next;
p.next.prev = p.prev;
return old;
}
}
return null;
}

public void removeAll()
{
this.head.prev = head;
}

public static void main(String[] args)
{
String values[] = {"A","B","C","D","E"};
DoubleLinkedList<String> list = new DoubleLinkedList<String>(values);
System.out.println("list:"+list.toString());
//list.remove(2);
//list.append("F");
//list.remove(1);
System.out.println(list.remove(1));
System.out.println("list:"+list.toString());
//System.out.println("list:"+list1.toString());
System.out.println("长度是:"+ list.length());
}
}
--------------------编程问答-------------------- remove()之后,前面的几个都删掉了啊 这是怎么回事 求指教 谢谢
补充:Java ,  Java相关
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,