定义
1.定义:双向链表(双链表)是链表的一种。和单链表一样,双链表也是由节点组成,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。
图解
2.图解·:
单链表的缺点:
- 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
- 单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除,所以前面我们单链表删除
时节点,总是找到 temp,temp 是待删除节点的前一个节点。
遍历
1.单链表一样,只是可以向前,也可以向后查找
添加 (默认添加到双向链表的最后)
(1) 先找到双向链表的最后这个节点
(2) temp.next = newHeroNode
(3) newHeroNode.pre = temp;
修改
思路和 原来的单向链表一样.
删除
(1) 因为是双向链表,因此,我们可以实现自我删除某个节点
(2) 直接找到要删除的这个节点,比如 temp
(3) temp.pre.next = temp.next
(4) temp.next.pre = temp.pr
指定位置插入
(1).找到指定位置的节点
newNode.nextNode=temp.nextNode;
temp.nextNode.preNode = newNode;
newNode.preNode=temp;
temp.nextNode=newNode;
Node类
public class HeroNode {
public int no;
public String name;
public HeroNode preNode;
public HeroNode nextNode;
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name +
'}';
}
}
DoubleLinkedList类
public class DoubleLinkedList {
//遍历
public void printList(HeroNode head) {
HeroNode temp=null;
if (!isEmpty(head)) {
temp=head.nextNode;
while(temp!=null){
System.out.println(temp.toString());
temp=temp.nextNode;
}
}else{
throw new myException("链表为空!");
}
}
//添加一个节点
public void addNode(HeroNode head,HeroNode newNode){
if(isEmpty(head)){
head.nextNode=newNode;
newNode.preNode=head;
}else {
HeroNode temp = head.nextNode;
while (temp.nextNode != null) {
temp = temp.nextNode;
}
temp.nextNode=newNode;
newNode.preNode=temp;
}
}
//修改一个节点
public void modifyNode(HeroNode head,HeroNode newNOde){
HeroNode temp=null;
boolean flag=true;
if (!isEmpty(head)) {
temp=head.nextNode;
while (flag){
if(temp.no==newNOde.no){
flag=false;
}else if(temp.nextNode==null){
throw new myException("未找到");
}
else{
temp=temp.nextNode;
}
}
newNOde.preNode=temp.preNode;
newNOde.nextNode=temp.nextNode;
temp.preNode.nextNode=newNOde;
if(temp.nextNode!=null) {
temp.nextNode.preNode = newNOde;
}
System.out.println("修改成功");
}else{
throw new myException("链表为空!");
}
}
//删除一个节点
public void removeNode(HeroNode head,int no){
HeroNode temp=null;
boolean flag=true;
if (!isEmpty(head)) {
temp=head.nextNode;
while (flag){
if(temp.no==no){
flag=false;
}else if(temp.nextNode==null){
throw new myException("未找到");
}
else{
temp=temp.nextNode;
}
}
temp.preNode.nextNode=temp.nextNode;
//判断是否为最后一个节点
if(temp.nextNode!=null){
temp.nextNode.preNode=temp.preNode;
}
System.out.println("修改成功");
}else{
throw new myException("链表为空!");
}
}
public void Insert(HeroNode head, int index,HeroNode newNode)
{
if(index<=0||index>getLenght(head)){
throw new myException("索引不符合规格");
}
HeroNode temp=head;
for(int i=0;i<index;i++){
temp=temp.nextNode;
}
newNode.nextNode=temp.nextNode;
//判断是否最后一个节点
if(temp.nextNode!=null) {
temp.nextNode.preNode = newNode;
}
newNode.preNode=temp;
temp.nextNode=newNode;
}
//是否为空
public boolean isEmpty(HeroNode head){
boolean flag=true;
if (head.nextNode!=null) {
flag=false;
}
return flag;
}
//长度
public int getLenght(HeroNode head){
HeroNode temp=null;
int lenght=0;
if(!isEmpty(head)){
temp=head;
while (temp.nextNode!=null){
temp=temp.nextNode;
lenght++;
}
}
return lenght;
}
}
class myException extends RuntimeException{
public myException(String e){
super(e);
}
}
测试类
public class DoubleLinkedListTest {
@Test
public void testLinkedList(){
DoubleLinkedList test=new DoubleLinkedList();
HeroNode head=new HeroNode(0, "");
HeroNode t1=new HeroNode(1, "美国队长");
HeroNode t2=new HeroNode(2, "雷神");
HeroNode t3=new HeroNode(3, "浩克");
//判断空
System.out.println(test.isEmpty(head));
//添加
test.addNode(head,t1);
test.addNode(head,t2);
test.addNode(head,t3);
//输出
test.printList(head);
//长度
System.out.println(test.getLenght(head));
//修改
test.modifyNode(head,new HeroNode(2,"蚁人"));
test.printList(head);
//test.modifyNode(head,new HeroNode(4,"蚁人"));//抛出异常
//删除
test.removeNode(head,3);
test.printList(head);
//test.removeNode(head,5);//抛出异常
//插入
test.Insert(head,1,new HeroNode(4,"金刚狼"));
test.printList(head);
}
}
结果
true
HeroNode{no=1, name='美国队长}
HeroNode{no=2, name='雷神}
HeroNode{no=3, name='浩克}
3
修改成功
HeroNode{no=1, name='美国队长}
HeroNode{no=2, name='蚁人}
HeroNode{no=3, name='浩克}
修改成功
HeroNode{no=1, name='美国队长}
HeroNode{no=2, name='蚁人}
HeroNode{no=1, name='美国队长}
HeroNode{no=4, name='金刚狼}
HeroNode{no=2, name='蚁人}