java双向链表查找_JAVA带表头的双向链表插入,删除,查找操作

public classLinkedlist {publicNode head;//指向表头地址的引用public intlength;publicLinkedlist(){//建立头结点

length=0;

head=newNode();

head.data=0;

head.left=null;

head.right=null;

}public boolean Insert(intpos,Node node) {//在下标为pos-1和pos的结点之间插入node结点int count=0;

Node preNode=head;while(preNode!=null&&count

preNode=preNode.right;

}if(preNode==null||count!=pos-1){return false;

}if(preNode.right==null) {

preNode.right=node;

node.left=preNode;

node.right=null;++length;return true;

}

Node nextNode=preNode.right;

node.right=nextNode;

node.left=preNode;

preNode.right=node;

nextNode.left=node;++length;return true;

}public boolean Delete(intpos) {//删除指定位置的结点if(pos<0||pos>=length) {return false;

}int count=0;

Node curNode=head;while(count!=pos&&curNode!=null) {++count;

curNode=curNode.right;

}if(count!=pos||curNode==null) {return false;

}if(curNode.right==null) {

curNode.left.right=null;

curNode=null;

}if(curNode.right!=null) {//前

curNode.left.right=curNode.right;

curNode.right.left=curNode.left;

}--length;return true;

}public intSize() {//返回链表元素个数returnlength;

}public booleanisEmpty() {if(length==0) {return true;

}return false;

}public booleanTraverse() {//输出链表

Node preNode=head;while(preNode.right!=null) {

preNode=preNode.right;

System.out.print(preNode.data+" ");

}return true;

}public int getData(intpos) {//取得指定位置结点元素

Node preNode=head;int count=0;while(count!=pos) {

preNode=preNode.right;++count;

}returnpreNode.data;

}public booleanDelete(Node node) {//删除node结点

Node curNode=head;while(curNode!=node&&curNode!=null) {

curNode=curNode.right;

}if(curNode!=node) {return false;

}if(curNode.right!=null) {

curNode.left.right=curNode.right;

curNode.right.left=curNode.left;--length;return true;

}if(curNode.right==null) {

curNode.left.right=null;--length;return true;

}return false;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值