java创建链表伪代码 java构建链表( 五 )


list is
empty!");
//输出提示信息
return;
//返回到调用的地方
}
if(head==tail){
//当链表中只有一个节点时
System.out.println(head.info);
//输出这个节点的信息,就是头结点的信息
return;
}
IntNode
temp;
//定义一个中间变量
for(temp=head;temp!=null;temp=temp.next){
//遍历整个链表
System.out.print(temp.info+"
");
//输出每个节点的信息
}
System.out.println();
//输出一个换行 , 可以没有这一行
}
public boolean isInList(int
el){
//判断el是否存在于链表中
IntNode
temp;
//定义一个中间变量
for(temp=head;temp!=null
temp.info!=el;temp=temp.next);
//将el找出来,注意最后的分
return
temp!=null;
// 如果存在返回true,否则返回flase,这两行代码很有思想
}
public void delete(int
el){
//删除链表中值为el的节点
if(head.info==el
head==tail){
//如果只有一个节点,并且节点的值为el
head=tail=null;
//删除这个节点
}else
if(head.info==el){
// 不止一个节点,而头结点的值就是el
head=head.next;
//删除头结点
}else{
IntNode
pred,temp;
//定义两个中间变量
for(pred=head,temp=head.next;temp.info!=el
temp.next!=null;pred=pred.next,temp=temp.next);
//跟上面的类似,自己琢磨吧 , 也是要注意最后的分号
pred.next=temp.next;
//将temp指向的节点删除,最好画一个链表的图,有助于理解
if(temp==tail){
//如果temp指向的节点是尾结点
tail=pred;
//将pred指向的节点设为尾结点 , 
}
}
}
//下面这个方法是在链表中值为el1的节点前面插入一个值为el2的节点,
//用类似的思想可以再写一个在链表中值为el1的节点后面插入一个值为el2的节点
public boolean insertToList(int el1,int
el2){
//定义一个插入节点的方法,插入成功返回true,否则返回false
IntNode
pred,temp;//定义两个中间变量
if(isEmpty()){
//判断链表是否为空
return
false;
//如果链表为空就直接返回false
}
if(head.info==el1
head==tail){
//如果链表中只有一个节点,并且这个节点的值是el1
head=new
IntNode(el2,head);
//新建立一个节点
return
true;
}else if(head.info==el1){
IntNode t=new
IntNode(el2);
t.next=head;
head=t;
return
true;
}else{
for(pred=head,temp=head.next;temp!=null
temp.info!=el1;pred=pred.next,temp=temp.next);
if(temp!=null){
IntNode
a=new IntNode(el2);
pred.next=a;
a.next=temp;
return
true;
}else{
System.out.println(el1+"
NOT EXEISTS!");
return
false;
}
}
}
3.下面是测试代码
public static void main(String[] args){
IntSLList test=new
IntSLList();
//test.addToHead(7);
test.addToTail(7);
System.out.println(test.insertToList(7,5));
test.printAll();
System.out.println(test.isInList(123));
}
}
关于java创建链表伪代码和java构建链表的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读