Java双链表

一、概述

  

  

二、英雄类 

 1 class HeroNode {
 2     //值域
 3     public int id;
 4     public String name;
 5     public String nickName;
 6     //指针域
 7     public HeroNode next;
 8     public HeroNode prev;
 9 
10     HeroNode(int id, String name, String nickName) {
11         this.id = id;
12         this.name = name;
13         this.nickName = nickName;
14     }
15 }

三、主方法

 1     private  HeroNode head = null;
 2     private  HeroNode last = null;
 3     @Test
 4     public void test() {
 5        /* insertNodeBefore(new HeroNode(1, "松江", "及时雨"));
 6         insertNodeBefore(new HeroNode(2, "武松", "行者"));
 7         insertNodeBefore(new HeroNode(3, "林冲", "豹子头"));*/
 8         insertNodeAfter(new HeroNode(1, "松江", "及时雨"));
 9         insertNodeAfter(new HeroNode(2, "武松", "行者"));
10         insertNodeAfter(new HeroNode(3, "林冲", "豹子头"));
11 
12         deleteNode(2);
13         deleteNode(1);
14         deleteNode(3);
15         printLinkedLast();
16     }

 

四、操作

   双链表操作跟单链表相差无几,所以只写了插入和打印操作

  1、最前插入节点

 1 //插入新节点
 2     public void insertNode(HeroNode head, HeroNode newNode) {
 3         if (head == null) {
 4             System.out.println("头结点不能为空");
 5             return;
 6         }
 7         HeroNode hn = head;
 8         //循环到最后一个节点,它的next=null,即hn.next==null
 9         while (hn.next != null) {
10             hn = hn.next;
11         }
12         //将hn.next指向新节点
13         hn.next = newNode;
14         //新节点的prev指向hn,即新节点的前一个节点
15         newNode.prev = hn;
16     }

  2、最后插入节点

 1     //插入新节点(插入最前面)
 2     public void insertNodeBefore(HeroNode newNode) {
 3         //如果插入的是第一个元素
 4         if(head==null){
 5             head = newNode;
 6             head.prev=null;
 7             head.next=null;
 8             last = head;
 9         }else {
10             newNode.prev = null;
11             newNode.next = head;
12             head.prev = newNode;
13             head = newNode;
14         }
15     }

 

  3、打印双向链表(根据head向后遍历打印)

 1     //打印双向链表(根据head打印)
 2     public void printLinkedHead() {
 3         if (head == null) {
 4             System.out.println("链表为空");
 5             return;
 6         }
 7         HeroNode tmp = head;
 8         while (tmp!= null) {
 9             System.out.println(show(tmp));
10             tmp = tmp.next;
11         }
12     }

 

  4、打印双向链表(根据last向前遍历打印)

 

 1 //打印双向链表(根据head打印)
 2     public void printLinkedLast() {
 3         if (last == null) {
 4             System.out.println("链表为空");
 5             return;
 6         }
 7         HeroNode tmp = last;
 8         while (tmp!= null) {
 9             System.out.println(show(tmp));
10             tmp = tmp.prev;
11         }
12     }

 

  5、删除节点

 

 1     public  void deleteNode(int id){
 2         if (last == null) {
 3             System.out.println("链表为空");
 4             return;
 5         }
 6         HeroNode tmp = last;
 7         while (tmp!= null) {
 8             if(tmp.id==id){
 9                 //只有一个节点,
10                 if(last.id==id && head.id==id){
11                     head = null;
12                     last = null;
13                 }
14                 //有两个节点,删除后一个
15                 else if(last.id == id && head.id!=id){
16                     head.next=null;
17                     last = head;
18                 }
19                 //有两个节点,删除前一个
20                 else if(head.id==id && last.id!=id){
21                     last.prev = null;
22                     head = last;
23                 }
24                 //有两个以上节点
25                 else{
26                     //当前节点的前一个节点的下一个节点,指向当前节点的下一个节点
27                     tmp.prev.next=tmp.next;
28                     //当前节点的下一个节点的前一个节点,指向当前节点的前一个节点
29                     tmp.next.prev=tmp.prev;
30                 }
31                 return;
32             }
33             tmp = tmp.prev;
34         }
35     }

 

  5、打印的辅助方法 

 1 public String show(HeroNode h) {
 2         return "{" +
 3                 "本节点id=" + convertNode(h) + '\'' +
 4                 "id=" + h.id +
 5                 ", name='" + h.name + '\'' +
 6                 ", nickName='" + h.nickName + '\'' +
 7                 ", prev=" + convertNode(h.prev) + '\'' +
 8                 ", next=" + convertNode(h.next) + '\'' +
 9                 '}' + "\n";
10     }
11 
12     //格式化HeroNode,直接输出地址值
13     public String convertNode(HeroNode h) {
14         return h == null ? null : h.toString().substring(h.toString().lastIndexOf('@'));
15     }

 

五、结果展示

  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 基于双向链表实现双端队列结构 */ package dsa; public class Deque_DLNode implements Deque { protected DLNode header;//指向头节点(哨兵) protected DLNode trailer;//指向尾节点(哨兵) protected int size;//队列中元素的数目 //构造函数 public Deque_DLNode() { header = new DLNode(); trailer = new DLNode(); header.setNext(trailer); trailer.setPrev(header); size = 0; } //返回队列中元素数目 public int getSize() { return size; } //判断队列是否为空 public boolean isEmpty() { return (0 == size) ? true : false; } //取首元素(但不删除) public Object first() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return header.getNext().getElem(); } //取末元素(但不删除) public Object last() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return trailer.getPrev().getElem(); } //在队列前端插入新节点 public void insertFirst(Object obj) { DLNode second = header.getNext(); DLNode first = new DLNode(obj, header, second); second.setPrev(first); header.setNext(first); size++; } //在队列后端插入新节点 public void insertLast(Object obj) { DLNode second = trailer.getPrev(); DLNode first = new DLNode(obj, second, trailer); second.setNext(first); trailer.setPrev(first); size++; } //删除首节点 public Object removeFirst() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = header.getNext(); DLNode second = first.getNext(); Object obj = first.getElem(); header.setNext(second); second.setPrev(header); size--; return(obj); } //删除末节点 public Object removeLast() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = trailer.getPrev(); DLNode second = first.getPrev(); Object obj = first.getElem(); trailer.setPrev(second); second.setNext(trailer); size--; return(obj); } //遍历 public void Traversal() { DLNode p = header.getNext(); while (p != trailer) { System.out.print(p.getElem()+" "); p = p.getNex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值