A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© My_Android 中级黑马   /  2016-5-26 09:29  /  476 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



  1. /**
  2. * 定义一个,表示一个节点
  3. * @author hasee
  4. *
  5. */
  6. public class Node {
  7.        
  8.         private Node previous; // 表示上个节点
  9.         private Object obj;   //  当前节点的值
  10.         private Node next;  // 表示下个节点
  11.        
  12.         public Node() {
  13.         }
  14.        
  15.         public Node(Node previous, Object obj, Node next) {
  16.                 this.previous = previous;
  17.                 this.obj = obj;
  18.                 this.next = next;
  19.         }
  20.        
  21.         public Node getPrevious() {
  22.                 return previous;
  23.         }
  24.         public void setPrevious(Node previous) {
  25.                 this.previous = previous;
  26.         }
  27.         public Object getObj() {
  28.                 return obj;
  29.         }
  30.         public void setObj(Object obj) {
  31.                 this.obj = obj;
  32.         }
  33.         public Node getNext() {
  34.                 return next;
  35.         }
  36.         public void setNext(Node next) {
  37.                 this.next = next;
  38.         }
  39. }
复制代码
  1. /**
  2. * 双向链表简单模拟的实现
  3. * @author hasee
  4. *
  5. */
  6. public class SxtLinkedList {
  7.         private Node first; //表示容器头节点
  8.         private Node last; //表示容器尾节点
  9.         private int size; //容器存储的元素大小
  10.         /**
  11.          * 添加一个元素对象
  12.          * @param obj
  13.          */
  14.         public void add(Object obj){
  15.                 /**
  16.                  * 如果first为null,代表没有第一个节点。
  17.                  * 不为空,代表有第一个节点,直接往last节点后增加一个节点
  18.                  */
  19.                 //创建一个节点
  20.                 Node n = new Node();
  21.                 if(first == null){
  22.                         n.setPrevious(null); //容器的头部节点没有上个节点,所以为null
  23.                         n.setObj(obj);   
  24.                         n.setNext(null);  //容器的尾部节点也有下个节点,所以也为null
  25.                        
  26.                         first = n; // 节点n代表容器的头部
  27.                         last = n; // 节点n 也代表容器的尾部
  28.                 }else{
  29.                         n.setPrevious(last);
  30.                         n.setObj(obj);
  31.                         n.setNext(null);
  32.                        
  33.                         last.setNext(n); //尾部节点的下一个节点为n节点
  34.                         last = n; // 当前容器的尾部的节点替换为n
  35.                 }
  36.                 size++;
  37.         }
  38.         /**
  39.          * 按照索引位置,取出一个对象
  40.          * @param index
  41.          * @return
  42.          */
  43.         public Object get(int index){
  44.                 rangeCheck(index); //
  45.                 Node temp = null;
  46.                 if(first != null){
  47.                         temp = first;
  48.                         for(int i=0;i<index;i++){
  49.                                 temp = temp.getNext();
  50.                         }
  51.                 }
  52.                 return temp.getObj();
  53.         }
  54.         /**
  55.          * 索引检测
  56.          * @param index
  57.          */
  58.         private void rangeCheck(int index) {
  59.                 if(index < 0 || index >= size){
  60.                         try {
  61.                                 throw new Exception();
  62.                         } catch (Exception e) {
  63.                                 e.printStackTrace();
  64.                         }
  65.                 }
  66.         }
  67.         public int size(){
  68.                 return size;
  69.         }
  70. }
复制代码




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马