Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258087
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-05-03 11:19:29



点击(此处)折叠或打开

  1. /*
  2.  * 链结点,相当于是车厢
  3.  */
  4. public class Node {
  5.     //数据域
  6.     public long data;
  7.     //指针域
  8.     public Node next;
  9.     public Node previous;
  10.     
  11.     public Node(long value) {
  12.         this.data = value;
  13.     }
  14.     
  15.     /**
  16.      * 显示方法
  17.      */
  18.     public void display() {
  19.         System.out.print(data + " ");
  20.     }
  21. }


点击(此处)折叠或打开

  1. *
  2.  * 双向链表
  3.  */
  4. public class DoubleLinkList {
  5.     //头结点
  6.     private Node first;
  7.     //尾结点
  8.     private Node last;
  9.     
  10.     public DoubleLinkList() {
  11.         first = null;
  12.         last = null;
  13.     }
  14.     
  15.     /**
  16.      * 插入一个结点,在头结点后进行插入
  17.      */
  18.     public void insertFirst(long value) {
  19.         Node node = new Node(value);
  20.         if(isEmpty()) {
  21.             last = node;
  22.         } else {
  23.             first.previous = node;
  24.         }
  25.         node.next = first;
  26.         first = node;
  27.     }
  28.     
  29.     /**
  30.      * 插入一个结点,从尾结点进行插入
  31.      */
  32.     public void insertLast(long value) {
  33.         Node node = new Node(value);
  34.         if(isEmpty()) {
  35.             first = node;
  36.         } else {
  37.             last.next = node;
  38.             node.previous = last;
  39.         }
  40.         last = node;
  41.     }
  42.     
  43.     /**
  44.      * 删除一个结点,在头结点后进行删除
  45.      */
  46.     public Node deleteFirst() {
  47.         Node tmp = first;
  48.         if(first.next == null) {
  49.             last = null;
  50.         } else {
  51.             first.next.previous = null;
  52.         }
  53.         first = tmp.next;
  54.         return tmp;
  55.     }
  56.     
  57.     /**
  58.      * 删除结点,从尾部进行删除
  59.      */
  60.     public Node deleteLast() {
  61.         Node tmp = last;
  62.         if(first.next == null) {
  63.             first = null;
  64.         } else {
  65.             last.previous.next = null;
  66.         }
  67.         last = last.previous;
  68.         return last;
  69.     }
  70.     
  71.     /**
  72.      * 显示方法
  73.      */
  74.     public void display() {
  75.         Node current = first;
  76.         while(current != null) {
  77.             current.display();
  78.             current = current.next;
  79.         }
  80.         System.out.println();
  81.     }
  82.     
  83.     /**
  84.      * 查找方法
  85.      */
  86.     public Node find(long value) {
  87.         Node current = first;
  88.         while(current.data != value) {
  89.             if(current.next == null) {
  90.                 return null;
  91.             }
  92.             current = current.next;
  93.         }
  94.         return current;
  95.     }
  96.     
  97.     /**
  98.      * 删除方法,根据数据域来进行删除
  99.      */
  100.     public Node delete(long value) {
  101.         Node current = first;
  102.         while(current.data != value) {
  103.             if(current.next == null) {
  104.                 return null;
  105.             }
  106.             current = current.next;
  107.         }
  108.         
  109.         if(current == first) {
  110.             first = first.next;
  111.         } else {
  112.             current.previous.next = current.next;
  113.         }
  114.         return current;
  115.         
  116.     }
  117.     
  118.     /**
  119.      * 判断是否为空
  120.      */
  121.     public boolean isEmpty() {
  122.         return (first == null);
  123.     }
  124. }



点击(此处)折叠或打开

  1. public class TestDoubleLinkList {
  2.     public static void main(String[] args) {
  3.         DoubleLinkList dl = new DoubleLinkList();
  4.         dl.insertLast(45);
  5.         dl.insertLast(56);
  6.         dl.insertLast(90);
  7.         dl.display();
  8.         
  9.         
  10.         while(!dl.isEmpty()) {
  11.             dl.deleteFirst();
  12.             dl.display();
  13.         }
  14.     }
  15. }



阅读(601) | 评论(0) | 转发(0) |
0

上一篇:单链表

下一篇:

给主人留下些什么吧!~~