标签:name else false err next get return break 存在
首先来分析一个上篇文章中单向链表的缺点:
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。
代码示例:
public class HeroNode2 {
public int no;
public String name;
public String nickName;
public HeroNode2 next;//指向下一个节点
public HeroNode2 pre;//指向前一个节点
public HeroNode2(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
class DoubleLinkedList{
private HeroNode2 head = new HeroNode2(0,"","");
public HeroNode2 getHead(){
return head;
}
/**
* 遍历双向链表
*/
public void list(){
if (head.next==null){
throw new RuntimeException("链表为空!");
}
HeroNode2 temp = head.next;
while (temp!=null){
System.out.println(temp);
temp = temp.next;
}
}
/**
* 添加节点
*/
public void add(HeroNode2 heroNode){
HeroNode2 temp = head;
while (true){
if (temp.next==null){
break;
}
temp = temp.next;
}
temp.next = heroNode;
heroNode.pre = temp;
}
/**
* 按照编号进行添加
*/
public void addByOrder(HeroNode2 heroNode){
HeroNode2 temp = head;
boolean flag = false;
while (true){
if (temp.next==null){
break;
}
if (temp.next.no>heroNode.no){
break;
}else if (temp.next.no == heroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
throw new RuntimeException("要添加的节点编号已存在!");
}else{
if (temp.next!=null){
temp.next.pre = heroNode;
}
heroNode.next = temp.next;
temp.next = heroNode;
heroNode.pre = temp;
}
}
/**
* 删除节点
* 1、对于双向链表,我们可以直接找到要删除的这个节点
* 2、找到后删除即可
*/
public void delete(int no){
if (head.next==null){
throw new RuntimeException("链表为空,不能删除!");
}
HeroNode2 temp = head.next;
boolean flag = false;
while (true){
if (temp==null){
break;
}
if (temp.no == no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.pre.next = temp.next;
if (temp.next!=null){
temp.next.pre = temp.pre;
}
}else {
throw new RuntimeException("要删除的节点不存在!");
}
}
}
标签:name else false err next get return break 存在
原文地址:https://www.cnblogs.com/lee0527/p/12013365.html