Java实现单链表

 java中有基本数据类型和引用数据类型(其实就是指针)。如果对引用不够了解请访问:点击打开链接,写的非常详细且易于理解。

实现链表的思路

1)链表类,结点类(链表类的内部类),在main()方法创建一条链表类对象,通过方法逐步创建结点类,通过引用链接起来成为链表。

2)结点类包含数据和对下个结点的引用,以及可以对数据赋值的构造函数。

3)链表类的构造方法,只构造出不含数据的头结点。(外部类可以直接对内部类的私有成员进行访问,这样就可以直接修改引用)

单链表结点的数据结构:

class Node{
      Object data;         //保存数据
      Node next;           //对下个结点的引用
}  

单链表一些必须掌握的方法:

package myLinkedList;
   
   public class MyLinkedList<E> {          //使用泛型是为了使用时候可以规范链表的数据类型
       // 结点内部类
       private class Node {
           private Object data;                  //数据
           private Node next = null;          //指向下个结点的引用
   
           public Node() {                         //无参数构造函数为了创建头结点服务
              data = null;
          }
  
          public Node(E data) {                //带数据的构造函数
              this.data = data;
          }
  
      }
  
      private Node head;               // 头引用(指针)
      private Node rear;                // 尾引用(指针)
      private Node point;              //临时引用(指针)
      private int length;                 // 链表长度
  
      public MyLink() {                  //链表构造函数,创建无数据的头结点
          head = new Node();     
          rear = head;
          length = ;
      }
  
      /**
       * 从尾部插入链表
       * 
       */
      public void add(E elem) {
          point = new Node(elem);
          rear.next = point;
          rear = point;
          length++;
  
      }
  
      /**
       * 遍历输出链表
       */
      public void traverse() {
          point = head;            //移动临时引用到头结点
          if (head != null)
              System.out.print("[" + head.data + "]");
          while (point.next != null) {
              System.out.print("-➜[" + point.next.data + "]");
              point = point.next;
          }
          System.out.println();
      }
  
      /**
       * 获得链表长度
       * 
       */
      public int length() {
          return length;
      }
  
      /**
       * 清除链表内容
           * (java垃圾回收,当对象没有引用指向,则视为垃圾,清理时机由系统决定)
       */
      public void clear() {               
          while (head.next != null) {
              head.next = head.next.next;
          }
          rear = head;      // 回到初始状态
          point = null;
          length = ;
          System.gc();      //请求系统清理垃圾,未必有用
      }
  
      /**
       * 在指定个位置插入元素成为第p个元素
       */
      public void insert(int position, E elem) {
                  if(position>= && position<=length){
              point = movePoint(position);
              Node tmp = new Node(elem);
              tmp.next = point.next;
              point.next = tmp;
              length++;
                 }else{
                      System.out.println("没有指定位置,插入失败")
                 }
  
      }
  
      /**
       * 删除指定位置的元素
       */
      public void remove(int position) {
          if (position >=  && position < length) {
              point = movePoint(position);
             Node tmp = point.next;
             point.next = tmp.next;
             length--;
         } else {
             System.out.println("删除失败,没有指定位置元素");
         }
     }
 
     /**
      * 更改指定位置的元素
      */
     public void set(int position, E elem) {
         if (position >=  && position < length) {
             point = movePoint(position);
             point.next.data = elem;
         } else {
             System.out.println("修改失败,没有指定位置元素");
         }
     }
 
     /**
      * 移动指针到指定位置
          * 私有方法,供其它方法使用
      */
     private Node movePoint(int position) {
         if (position < )                    //如果参数小于零,则移动到头部,本来是为了规避参数错误问题,但在使用此方法的其他方法皆有做其他处理,可删除
             return head;
         if (position > length)            //如果参数大于长度,则移动到尾部,同上,可删除
             return rear;
 
         if (position >=  && position <= length) {
             point = head;
             while (point != null) {
                 if (position == )
                     break;
                 position--;
                 point = point.next;
             }
         }
 
         return point;
 
     }
 
     /**
      * 连接两条链表
          * 已知问题,两链表的数据类型不一定相同,凭我目前实力无法理解解决(如何获取泛型实际类型)
      */
     public void connect(MyLink b) {
         this.rear.next = b.head.next;
         this.length += b.length;
         b.head = null;
     }
 
     /**
      * 按下标查找
      */
     public E find(int position) {
         if (position >=  && position < length) {
             Node tmp = movePoint(position);
             return (E) tmp.next.data;
         }
         return null;
     }
 
     /**
      * 查找元素的值,返回下标
      */
     public int search(E elem) {
         point = head.next;
         int idex = -;
         while (point != null) {
             idex++;
             if (point.data == elem)
                 break;
             point = point.next;
         }
         return idex;
 
     }
 }
参考博客:http://www.cnblogs.com/xy-hong/p/7247374.html


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值