双向循环链表的add方法

Lifehopeness 2012-06-05 10:02:59
package linkList.kcsj;

public class BorthWay<AnyType> {
Node<AnyType> firstNode;
int theSize;
//建立空表
/* public BorthWay(){
firstNode.next=firstNode.pre=firstNode;
theSize=0;
}*/

//返回表的大小
int size(){
return theSize;
}
//得到第id个节点
public Node<AnyType> get(int id){
Node<AnyType> p;
p=firstNode;
int j;
if(theSize==0){
return null;
}
else if(id<=theSize){
for(j=0;j<id-1;j++){
p=p.next;
}
}
else
System.out.println("越界");
return p;
}
//插入第i个节点
public void add(int id,AnyType insert){
if(theSize==0){
firstNode=new Node<AnyType>(insert,null,null);
theSize++;
}
else{

Node<AnyType> getNode=get(id);
System.out.print(getNode);
Node<AnyType> newNode=new Node<AnyType>(insert,getNode.pre,getNode);
getNode.pre=newNode;
getNode.next=null;
theSize++;


}


}
public void addFirst(AnyType insert ){
add(0,insert);
}

//输出链表
void print(){
Node p=firstNode;
while(p!=null){
System.out.print(p.data+" ");
p=p.next;
}
}
public static void main(String[] args){
BorthWay<Integer> btw=new BorthWay<Integer>();
int[] a={1,2,3,4,5,6,};
for(int i=0;i<6;i++){
btw.add(i,a[i]);
}
btw.addFirst(3);
btw.print();

}
}


哪位大侠帮忙看看,哪里的错误额
...全文
191 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lifehopeness 2012-06-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
不是很理解你对双向循环链表的理解!!!
[/Quote]
是我理解错了么??
xiaoliner2050 2012-06-05
  • 打赏
  • 举报
回复
不是很理解你对双向循环链表的理解!!!
Lifehopeness 2012-06-05
  • 打赏
  • 举报
回复
package linkList.kcsj;



public class Node<AnyType> {
public AnyType data;
Node<AnyType> pre,next;
public Node(AnyType d,Node<AnyType> p,Node<AnyType> n){
data=d;
pre = p;
next=n;
}
}
这是node类
无名之辈 2012-06-05
  • 打赏
  • 举报
回复
看不懂你这写的什么 - - !
Node 类也没给阿。
AnyType 这个泛型也没说明阿。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧