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

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-05-02 20:10:35



点击(此处)折叠或打开

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


点击(此处)折叠或打开

  1. /*
  2.  * 链表,相当于火车
  3.  */
  4. public class LinkList {
  5.     //头结点
  6.     private Node first;
  7.     
  8.     public LinkList() {
  9.         first = null;
  10.     }
  11.     
  12.     /**
  13.      * 插入一个结点,在头结点后进行插入
  14.      */
  15.     public void insertFirst(long value) {
  16.         Node node = new Node(value);
  17.         node.next = first;
  18.         first = node;
  19.     }
  20.     
  21.     /**
  22.      * 删除一个结点,在头结点后进行删除
  23.      */
  24.     public Node deleteFirst() {
  25.         Node tmp = first;
  26.         first = tmp.next;
  27.         return tmp;
  28.     }
  29.     
  30.     /**
  31.      * 显示方法
  32.      */
  33.     public void display() {
  34.         Node current = first;
  35.         while(current != null) {
  36.             current.display();
  37.             current = current.next;
  38.         }
  39.         System.out.println();
  40.     }
  41.     
  42.     /**
  43.      * 查找方法
  44.      */
  45.     public Node find(long value) {
  46.         Node current = first;
  47.         while(current.data != value) {
  48.             if(current.next == null) {
  49.                 return null;
  50.             }
  51.             current = current.next;
  52.         }
  53.         return current;
  54.     }
  55.     
  56.     /**
  57.      * 删除方法,根据数据域来进行删除
  58.      */
  59.     public Node delete(long value) {
  60.         Node current = first;
  61.         Node previous = first;
  62.         while(current.data != value) {
  63.             if(current.next == null) {
  64.                 return null;
  65.             }
  66.             previous = current;
  67.             current = current.next;
  68.         }
  69.         
  70.         if(current == first) {
  71.             first = first.next;
  72.         } else {
  73.             previous.next = current.next;
  74.         }
  75.         return current;
  76.         
  77.     }
  78. }


点击(此处)折叠或打开

  1. public class TestLinkList {
  2.     public static void main(String[] args) {
  3.         LinkList linkList = new LinkList();
  4.         linkList.insertFirst(34);
  5.         linkList.insertFirst(23);
  6.         linkList.insertFirst(12);
  7.         linkList.insertFirst(0);
  8.         linkList.insertFirst(-1);
  9.         
  10. //        linkList.display();
  11. //        
  12. //        linkList.deleteFirst();
  13. //        linkList.display();
  14. //        
  15. //        Node node = linkList.find(23);
  16. //        node.display();
  17.         
  18.         Node node1 = linkList.delete(0);
  19.         node1.display();
  20.         System.out.println();
  21.         linkList.display();
  22.     }
  23. }


阅读(540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~