java循环单链表比较相等_java的循环单链表

packageclink;//循环单链表

public classTestClink {public static voidmain(String[] args) {//TODO Auto-generated method stub

Clink t1 = newClink();for(int i = 0;i<9;i++){

t1.insertHead(i);

}

t1.show();int len=t1.getlength();

System.out.println(len);

System.out.println("---");

t1.insertTail(5);

t1.insertTail(5);

t1.show();

System.out.println("---");

t1.delete(5);

t1.show();

}

}classClink{classEntry{intdata;

Entry next;publicEntry(){this.data=-1;this.next=null;

}public Entry(intdata){this.data=data;this.next=null;

}

}private Entry head=null;publicClink(){this.head=newEntry();this.head.next=this.head;

}

//头插法public void insertHead(intval){

Entry entry=newEntry(val);

entry.next=this.head.next;this.head.next=entry;

}

//尾插法public void insertTail(intval){

Entry cur=this.head;//定义一个节点用来寻找链表的尾结点

while(cur.next!=this.head){

cur=cur.next;

}

Entry entry=new Entry(val);//定义要插入的节点

entry.next=cur.next;

cur.next=entry;

}public booleanisEmpty(){

Entry cur=this.head;if(cur.next!=this.head){return false;

}return true;

}

删除值为5的所有节点public boolean delete(intval){//此时删除5

Entry prev=this.head;

Entry cur=this.head.next;if(isEmpty()){return false;

}while(cur!=this.head){if(cur.data==val){

prev.next=cur.next;

cur=prev.next;

}else{

prev=cur;

cur=cur.next;

}

}return true;

}/*public void delete(int val){

Entry prev=this.head;

Entry cur=this.head.next;

while(cur!=this.head){

if(cur.data==val){

prev.next=cur.next;

cur=prev.next;

}else{

prev=cur;

cur=cur.next;

}

}

}*/

public intgetlength(){

Entry cur=this.head;int len=0;while(cur.next!=head){

len++;

cur=cur.next;

}returnlen;

}public voidshow(){

Entry cur=this.head.next;while(cur!=this.head){

System.out.println(cur.data+"data");

cur=cur.next;

}

System.out.println();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值