在java实现自定义链表(单向链表)

来源:互联网 发布:2017淘宝入驻条件费用 编辑:程序博客网 时间:2024/05/01 01:34
  1. import java.io.*;
  2. import com.toplx.game.utils.Node;
  3. public class List {
  4.     /* 用变量来实现表头 */
  5.     private Node Head = null;
  6.     private Node Tail = null;
  7.     private Node Pointer = null;
  8.     private int Length = 0;
  9.     /* 清空整个链表 */
  10.     public void deleteAll() {
  11.         Head = null;
  12.         Tail = null;
  13.         Pointer = null;
  14.         Length = 0;
  15.     }
  16.     /* 链表复位,使第一个结点成为当前结点 */
  17.     public void reset() {
  18.         Pointer = null;
  19.     }
  20.     /* 判断链表是否为空 */
  21.     public boolean isEmpty() {
  22.         return (Length == 0);
  23.     }
  24.     /* 判断当前结点是否为最后一个结点 */
  25.     public boolean isEnd() {
  26.         if (Length == 0)
  27.             throw new java.lang.NullPointerException();
  28.         else if (Length == 1)
  29.             return true;
  30.         else
  31.             return (cursor() == Tail);
  32.     }
  33.     /* 返回当前结点的下一个结点的值,并使其成为当前结点 */
  34.     public Object nextNode() {
  35.         if (Length == 1)
  36.             throw new java.util.NoSuchElementException();
  37.         else if (Length == 0)
  38.             throw new java.lang.NullPointerException();
  39.         else {
  40.             Node temp = cursor();
  41.             Pointer = temp;
  42.             if (temp != Tail)
  43.                 return (temp.next.data );
  44.             else
  45.                 throw new java.util.NoSuchElementException();
  46.         }
  47.     }
  48.     /* 返回当前结点的值 */
  49.     public Object currentNode() {
  50.         Node temp = cursor();
  51.         return temp.data;
  52.     }
  53.     /* 在当前结点前插入一个结点,并使其成为当前结点 */
  54.     public void insert(Object d) {
  55.         Node e = new Node(d);
  56.         if (Length == 0) {
  57.             Tail = e;
  58.             Head = e;
  59.         } else {
  60.             Node temp = cursor();
  61.             e.next = temp;
  62.             if (Pointer == null)
  63.                 Head = e;
  64.             else
  65.                 Pointer.next = e;
  66.         }
  67.         Length++;
  68.     }
  69.     /* 返回链表的大小 */
  70.     public int size() {
  71.         return (Length);
  72.     }
  73.     /**
  74.      * 将当前结点移出链表,下一个结点
  75.      * 成为当前结点,如果移出的结点是
  76.      * 最后一个结点,则第一个结点 成
  77.      * 为当前结点
  78.      */
  79.     public Object remove() {
  80.         Object temp;
  81.         if (Length == 0)
  82.             throw new java.util.NoSuchElementException();
  83.         else if (Length == 1) {
  84.             temp = Head.data;
  85.             deleteAll();
  86.         } else {
  87.             Node cur = cursor();
  88.             temp = cur.data;
  89.             if (cur == Head)
  90.                 Head = cur.next;
  91.             else if (cur == Tail) {
  92.                 Pointer.next = null;
  93.                 Tail = Pointer;
  94.                 reset();
  95.             } else
  96.                 Pointer.next = cur.next;
  97.             Length--;
  98.         }
  99.         return temp;
  100.     }
  101.     /* 返回当前结点的指针 */
  102.     private Node cursor() {
  103.         if (Head == null)
  104.             throw new java.lang.NullPointerException();
  105.         else if (Pointer == null)
  106.             return Head;
  107.         else
  108.             return Pointer.next;
  109.     }
  110.     
  111.     public class Node 
  112.     {
  113.         public Object data;
  114.         public Node next;
  115.         public Node(Object d)
  116.         {
  117.             data = d;
  118.             next = null;
  119.         }
  120.     }
  121.     /* 链表的简单应用举例 */
  122.     public static void main(String[] args) {
  123.         List a = new List();
  124.         for (int i = 1; i <= 10; i++)
  125.             a.insert(new Integer(i));
  126.         System.out.println(a.currentNode());
  127.         while (!a.isEnd())
  128.             System.out.println(a.nextNode());
  129.         a.reset();
  130.         while (!a.isEnd()) {
  131.             a.remove();
  132.         }
  133.         a.remove();
  134.         a.reset();
  135.         if (a.isEmpty())
  136.             System.out.println("There is no Node in List /n");
  137.         System.out.println(" You can press return to quit/n");
  138.         try {
  139.             System.out.println("I am waitting for you :) >>>>>>");
  140.             System.in.read(); // 确保用户看清程序运行结果
  141.             System.out.println("exit success!");
  142.         } catch (IOException e) {
  143.       }
  144.     }
  145. }
原创粉丝点击