线性表

  1. import java.util.Arrays;  
  2.   
  3. public class SequenceList<T>  
  4. {  
  5.     private int DEFAULT_SIZE = 16;  
  6.     //保存数组的长度。  
  7.     private int capacity;  
  8.     //定义一个数组用于保存顺序线性表的元素  
  9.     private Object[] elementData;  
  10.     //保存顺序表中元素的当前个数  
  11.     private int size = 0;  
  12.     //以默认数组长度创建空顺序线性表  
  13.     public SequenceList()  
  14.     {  
  15.         capacity = DEFAULT_SIZE;  
  16.         elementData = new Object[capacity];  
  17.     }  
  18.     //以一个初始化元素来创建顺序线性表  
  19.     public SequenceList(T element)  
  20.     {  
  21.         this();  
  22.         elementData[0] = element;  
  23.         size++;  
  24.     }  
  25.     /** 
  26.      * 以指定长度的数组来创建顺序线性表 
  27.      * @param element 指定顺序线性表中第一个元素 
  28.      * @param initSize 指定顺序线性表底层数组的长度 
  29.      */  
  30.     public SequenceList(T element , int initSize)  
  31.     {  
  32.         capacity = 1;  
  33.         //把capacity设为大于initSize的最小的2的n次方  
  34.         while (capacity < initSize)  
  35.         {  
  36.             capacity <<= 1;  
  37.         }  
  38.         elementData = new Object[capacity];  
  39.         elementData[0] = element;  
  40.         size++;  
  41.     }  
  42.     //获取顺序线性表的大小  
  43.     public int length()  
  44.     {  
  45.         return size;  
  46.     }  
  47.     //获取顺序线性表中索引为i处的元素  
  48.     public T get(int i)  
  49.     {  
  50.         if (i < 0 || i > size - 1)  
  51.         {  
  52.             throw new IndexOutOfBoundsException("线性表索引越界");  
  53.         }  
  54.         return (T)elementData[i];  
  55.     }  
  56.     //查找顺序线性表中指定元素的索引  
  57.     public int locate(T element)  
  58.     {  
  59.         for (int i = 0 ; i < size ; i++)  
  60.         {  
  61.             if (elementData[i].equals(element))  
  62.             {  
  63.                 return i;  
  64.             }  
  65.         }  
  66.         return -1;  
  67.     }  
  68.     //向顺序线性表的指定位置插入一个元素。  
  69.     public void insert(T element , int index)  
  70.     {  
  71.         if (index < 0 || index > size)  
  72.         {  
  73.             throw new IndexOutOfBoundsException("线性表索引越界");  
  74.         }  
  75.         ensureCapacity(size + 1);  
  76.         //将index处以后所有元素向后移动一格。  
  77.         System.arraycopy(elementData , index , elementData  
  78.              , index + 1 , size - index);  
  79.         elementData[index] = element;  
  80.         size++;  
  81.     }  
  82.     //在线性顺序表的开始处添加一个元素。  
  83.     public void add(T element)  
  84.     {  
  85.         insert(element , size);  
  86.     }  
  87.     //很麻烦,而且性能很差  
  88.     private void ensureCapacity(int minCapacity)  
  89.     {  
  90.         //如果数组的原有长度小于目前所需的长度  
  91.         if (minCapacity > capacity)  
  92.         {  
  93.             //不断地将capacity * 2,直到capacity大于minCapacity为止  
  94.             while (capacity < minCapacity)  
  95.             {  
  96.                 capacity <<= 1;  
  97.             }  
  98.             elementData = Arrays.copyOf(elementData , capacity);//此方法jdk1.6开始提供  
  99.         }  
  100.     }  
  101.     //删除顺序线性表中指定索引处的元素  
  102.     public T delete(int index)  
  103.     {  
  104.         if (index < 0 || index > size - 1)  
  105.         {  
  106.             throw new IndexOutOfBoundsException("线性表索引越界");  
  107.         }  
  108.         T oldValue = (T)elementData[index];  
  109.         int numMoved = size - index - 1;  
  110.         if (numMoved > 0)  
  111.         {  
  112.             System.arraycopy(elementData , index+1  
  113.                 , elementData, index ,  numMoved);  
  114.         }  
  115.         //清空最后一个元素  
  116.         elementData[--size] = null;   
  117.         return oldValue;  
  118.     }  
  119.     //删除顺序线性表中最后一个元素  
  120.     public T remove()  
  121.     {  
  122.         return delete(size - 1);  
  123.     }  
  124.     //判断顺序线性表是否为空表  
  125.     public boolean empty()  
  126.     {  
  127.         return size == 0;  
  128.     }  
  129.     //清空线性表  
  130.     public void clear()  
  131.     {  
  132.         //将底层数组所有元素赋为null  
  133.         Arrays.fill(elementData , null);  
  134.         size = 0;  
  135.     }  
  136.     public String toString()  
  137.     {  
  138.         if (size == 0)  
  139.         {  
  140.             return "[]";  
  141.         }  
  142.         else  
  143.         {  
  144.             StringBuilder sb = new StringBuilder("[");  
  145.             for (int i = 0 ; i < size ; i++ )  
  146.             {  
  147.                 sb.append(elementData[i].toString() + ", ");  
  148.             }  
  149.             int len = sb.length();  
  150.             return sb.delete(len - 2 , len).append("]").toString();  
  151.         }  
  152.     }  
  153.       
  154.     public static void main(String[] args)   
  155.     {  
  156.         SequenceList<String> list = new SequenceList<String>();  
  157.         list.add("aaaa");  
  158.         list.add("bbbb");  
  159.         list.add("cccc");  
  160.         //在索引为1处插入一个新元素  
  161.         list.insert("dddd" , 1);  
  162.         //输出顺序线性表的元素  
  163.         System.out.println(list);  
  164.         //删除索引为2处的元素  
  165.         list.delete(2);  
  166.         System.out.println(list);  
  167.         //获取cccc字符串在顺序线性表中的位置  
  168.         System.out.println("cccc在顺序线性表中的位置:"   
  169.             + list.locate("cccc"));  
  170.     }  
  171. }  
 

链式线性表的实现:单链表

 

Java代码   收藏代码
  1. public class LinkList<T>  
  2. {  
  3.     //定义一个内部类Node,Node实例代表链表的节点。  
  4.     private class Node  
  5.     {  
  6.         //保存节点的数据  
  7.         private T data;  
  8.         //指向下个节点的引用  
  9.         private Node next;  
  10.         //无参数的构造器  
  11.         public Node()  
  12.         {  
  13.         }  
  14.         //初始化全部属性的构造器  
  15.         public Node(T data , Node next)  
  16.         {  
  17.             this.data = data;  
  18.             this.next = next;  
  19.         }  
  20.     }  
  21.     //保存该链表的头节点  
  22.     private Node header;  
  23.     //保存该链表的尾节点  
  24.     private Node tail;  
  25.     //保存该链表中已包含的节点数  
  26.     private int size;  
  27.     //创建空链表  
  28.     public LinkList()  
  29.     {  
  30.         //空链表,header和tail都是null  
  31.         header = null;  
  32.         tail = null;  
  33.     }  
  34.     //以指定数据元素来创建链表,该链表只有一个元素  
  35.     public LinkList(T element)  
  36.     {  
  37.         header = new Node(element , null);  
  38.         //只有一个节点,header、tail都指向该节点  
  39.         tail = header;  
  40.         size++;  
  41.     }  
  42.     //返回链表的长度     
  43.     public int length()  
  44.     {  
  45.         return size;  
  46.     }  
  47.     //获取链式线性表中索引为index处的元素  
  48.     public T get(int index)  
  49.     {  
  50.         return getNodeByIndex(index).data;  
  51.     }  
  52.     //根据索引index获取指定位置的节点  
  53.     private Node getNodeByIndex(int index)  
  54.     {  
  55.         if (index < 0 || index > size - 1)  
  56.         {  
  57.             throw new IndexOutOfBoundsException("线性表索引越界");  
  58.         }  
  59.         //从header节点开始  
  60.         Node current = header;  
  61.         for (int i = 0 ; i < size && current != null  
  62.             ; i++ , current = current.next)  
  63.         {  
  64.             if (i == index)  
  65.             {  
  66.                 return current;  
  67.             }  
  68.         }  
  69.         return null;  
  70.     }  
  71.     //查找链式线性表中指定元素的索引  
  72.     public int locate(T element)  
  73.     {  
  74.         //从头节点开始搜索  
  75.         Node current = header;  
  76.         for (int i = 0 ; i < size && current != null  
  77.             ; i++ , current = current.next)  
  78.         {  
  79.             if (current.data.equals(element))  
  80.             {  
  81.                 return i;  
  82.             }  
  83.         }  
  84.         return -1;  
  85.     }  
  86.     //向线性链式表的指定位置插入一个元素。  
  87.     public void insert(T element , int index)  
  88.     {  
  89.         if (index < 0 || index > size)  
  90.         {  
  91.             throw new IndexOutOfBoundsException("线性表索引越界");  
  92.         }  
  93.         //如果还是空链表  
  94.         if (header == null)  
  95.         {  
  96.             add(element);  
  97.         }  
  98.         else  
  99.         {  
  100.             //当index为0时,也就是在链表头处插入  
  101.             if (index == 0)  
  102.             {  
  103.                 addAtHeader(element);  
  104.             }  
  105.             else  
  106.             {  
  107.                 //获取插入点的前一个节点  
  108.                 Node prev = getNodeByIndex(index - 1);  
  109.                 //让prev的next指向新节点,让新节点的next引用指向原来prev的下一个节点。  
  110.                 prev.next = new Node(element , prev.next);  
  111.                 size++;  
  112.             }  
  113.         }  
  114.     }  
  115.     //采用尾插法为链表添加新节点。  
  116.     public void add(T element)  
  117.     {  
  118.         //如果该链表还是空链表  
  119.         if (header == null)  
  120.         {  
  121.             header = new Node(element , null);  
  122.             //只有一个节点,header、tail都指向该节点  
  123.             tail = header;  
  124.         }  
  125.         else  
  126.         {  
  127.             //创建新节点  
  128.             Node newNode = new Node(element , null);  
  129.             //让尾节点的next指向新增的节点  
  130.             tail.next = newNode;  
  131.             //以新节点作为新的尾节点  
  132.             tail = newNode;  
  133.         }  
  134.         size++;  
  135.     }  
  136.     //采用头插法为链表添加新节点。  
  137.     public void addAtHeader(T element)  
  138.     {  
  139.         //创建新节点,让新节点的next指向原来的header  
  140.         //并以新节点作为新的header  
  141.         header = new Node(element , header);  
  142.         //如果插入之前是空链表  
  143.         if (tail == null)  
  144.         {  
  145.             tail = header;  
  146.         }  
  147.         size++;  
  148.     }  
  149.     //删除链式线性表中指定索引处的元素  
  150.     public T delete(int index)  
  151.     {  
  152.         if (index < 0 || index > size - 1)  
  153.         {  
  154.             throw new IndexOutOfBoundsException("线性表索引越界");  
  155.         }  
  156.         Node del = null;  
  157.         //如果被删除的是header节点  
  158.         if (index == 0)  
  159.         {  
  160.             del = header;  
  161.             header = header.next;  
  162.         }  
  163.         else  
  164.         {  
  165.             //获取删除点的前一个节点  
  166.             Node prev = getNodeByIndex(index - 1);  
  167.             //获取将要被删除的节点  
  168.             del = prev.next;  
  169.             //让被删除节点的next指向被删除节点的下一个节点。  
  170.             prev.next = del.next;  
  171.             //将被删除节点的next引用赋为null.  
  172.             del.next = null;  
  173.         }  
  174.         size--;  
  175.         return del.data;  
  176.     }  
  177.     //删除链式线性表中最后一个元素  
  178.     public T remove()  
  179.     {  
  180.         return delete(size - 1);  
  181.     }  
  182.     //判断链式线性表是否为空表  
  183.     public boolean empty()  
  184.     {  
  185.         return size == 0;  
  186.     }  
  187.     //清空线性表  
  188.     public void clear()  
  189.     {  
  190.         //header、tail赋为null  
  191.         header = null;  
  192.         tail = null;  
  193.         size = 0;  
  194.     }  
  195.     public String toString()  
  196.     {  
  197.         //链表为空链表时  
  198.         if (empty())  
  199.         {  
  200.             return "[]";  
  201.         }  
  202.         else  
  203.         {  
  204.             StringBuilder sb = new StringBuilder("[");  
  205.             for (Node current = header ; current != null  
  206.                 ; current = current.next )  
  207.             {  
  208.                 sb.append(current.data.toString() + ", ");  
  209.             }  
  210.             int len = sb.length();  
  211.             return sb.delete(len - 2 , len).append("]").toString();  
  212.         }  
  213.     }  
  214.       
  215.     public static void main(String[] args)   
  216.     {  
  217.         LinkList<String> list = new LinkList<String>();  
  218.         list.insert("aaaa" , 0);  
  219.         list.add("bbbb");  
  220.         list.add("cccc");  
  221.         //在索引为1处插入一个新元素  
  222.         list.insert("dddd" , 1);  
  223.         //输出顺序线性表的元素  
  224.         System.out.println(list);  
  225.         //删除索引为2处的元素  
  226.         list.delete(2);  
  227.         System.out.println(list);  
  228.         //获取cccc字符串在链表中的位置  
  229.         System.out.println("cccc在链表中的位置:"   
  230.             + list.locate("cccc"));  
  231.         System.out.println("链表中索引2处的元素:"   
  232.             + list.get(2));  
  233.     }  
  234. }  
 

链式线性表的实现:双向链表

 

Java代码   收藏代码
  1. public class DuLinkList<T>  
  2. {  
  3.     //定义一个内部类Node,Node实例代表链表的节点。  
  4.     private class Node  
  5.     {  
  6.         //保存节点的数据  
  7.         private T data;  
  8.         //指向上个节点的引用  
  9.         private Node prev;  
  10.         //指向下个节点的引用  
  11.         private Node next;  
  12.         //无参数的构造器  
  13.         public Node()  
  14.         {  
  15.         }  
  16.         //初始化全部属性的构造器  
  17.         public Node(T data , Node prev , Node next)  
  18.         {  
  19.             this.data = data;  
  20.             this.prev = prev;  
  21.             this.next = next;  
  22.         }  
  23.     }  
  24.     //保存该链表的头节点  
  25.     private Node header;  
  26.     //保存该链表的尾节点  
  27.     private Node tail;  
  28.     //保存该链表中已包含的节点数  
  29.     private int size;  
  30.     //创建空链表  
  31.     public DuLinkList()  
  32.     {  
  33.         //空链表,header和tail都是null  
  34.         header = null;  
  35.         tail = null;  
  36.     }  
  37.     //以指定数据元素来创建链表,该链表只有一个元素  
  38.     public DuLinkList(T element)  
  39.     {  
  40.         header = new Node(element , null , null);  
  41.         //只有一个节点,header、tail都指向该节点  
  42.         tail = header;  
  43.         size++;  
  44.     }  
  45.     //返回链表的长度     
  46.     public int length()  
  47.     {  
  48.         return size;  
  49.     }  
  50.   
  51.     //获取链式线性表中索引为index处的元素  
  52.     public T get(int index)  
  53.     {  
  54.         return getNodeByIndex(index).data;  
  55.     }  
  56.     //根据索引index获取指定位置的节点  
  57.     private Node getNodeByIndex(int index)  
  58.     {  
  59.         if (index < 0 || index > size - 1)  
  60.         {  
  61.             throw new IndexOutOfBoundsException("线性表索引越界");  
  62.         }  
  63.         if (index <= size / 2)  
  64.         {  
  65.             //从header节点开始  
  66.             Node current = header;  
  67.             for (int i = 0 ; i <= size / 2 && current != null  
  68.                 ; i++ , current = current.next)  
  69.             {  
  70.                 if (i == index)  
  71.                 {  
  72.                     return current;  
  73.                 }  
  74.             }  
  75.         }  
  76.         else  
  77.         {  
  78.             //从tail节点开始搜索  
  79.             Node current = tail;  
  80.             for (int i = size - 1 ; i > size / 2 && current != null  
  81.                 ; i++ , current = current.prev)  
  82.             {  
  83.                 if (i == index)  
  84.                 {  
  85.                     return current;  
  86.                 }  
  87.             }  
  88.         }  
  89.         return null;  
  90.     }  
  91.     //查找链式线性表中指定元素的索引  
  92.     public int locate(T element)  
  93.     {  
  94.         //从头节点开始搜索  
  95.         Node current = header;  
  96.         for (int i = 0 ; i < size && current != null  
  97.             ; i++ , current = current.next)  
  98.         {  
  99.             if (current.data.equals(element))  
  100.             {  
  101.                 return i;  
  102.             }  
  103.         }  
  104.         return -1;  
  105.     }  
  106.     //向线性链式表的指定位置插入一个元素。  
  107.     public void insert(T element , int index)  
  108.     {  
  109.         if (index < 0 || index > size)  
  110.         {  
  111.             throw new IndexOutOfBoundsException("线性表索引越界");  
  112.         }  
  113.         //如果还是空链表  
  114.         if (header == null)  
  115.         {  
  116.             add(element);  
  117.         }  
  118.         else  
  119.         {  
  120.             //当index为0时,也就是在链表头处插入  
  121.             if (index == 0)  
  122.             {  
  123.                 addAtHeader(element);  
  124.             }  
  125.             else  
  126.             {  
  127.                 //获取插入点的前一个节点  
  128.                 Node prev = getNodeByIndex(index - 1);  
  129.                 //获取插入点的节点  
  130.                 Node next = prev.next;  
  131.                 //让新节点的next引用指向next节点,prev引用指向prev节点  
  132.                 Node newNode = new Node(element , prev , next);  
  133.                 //让prev的next指向新节点。  
  134.                 prev.next = newNode;  
  135.                 //让prev的下一个节点的prev指向新节点  
  136.                 next.prev = newNode;  
  137.                 size++;  
  138.             }  
  139.         }  
  140.     }  
  141.     //采用尾插法为链表添加新节点。  
  142.     public void add(T element)  
  143.     {  
  144.         //如果该链表还是空链表  
  145.         if (header == null)  
  146.         {  
  147.             header = new Node(element , null , null);  
  148.             //只有一个节点,header、tail都指向该节点  
  149.             tail = header;  
  150.         }  
  151.         else  
  152.         {  
  153.             //创建新节点,新节点的pre指向原tail节点  
  154.             Node newNode = new Node(element , tail , null);  
  155.             //让尾节点的next指向新增的节点  
  156.             tail.next = newNode;  
  157.             //以新节点作为新的尾节点  
  158.             tail = newNode;  
  159.         }  
  160.         size++;  
  161.     }  
  162.     //采用头插法为链表添加新节点。  
  163.     public void addAtHeader(T element)  
  164.     {  
  165.         //创建新节点,让新节点的next指向原来的header  
  166.         //并以新节点作为新的header  
  167.         header = new Node(element , null , header);  
  168.         //如果插入之前是空链表  
  169.         if (tail == null)  
  170.         {  
  171.             tail = header;  
  172.         }  
  173.         size++;  
  174.     }  
  175.     //删除链式线性表中指定索引处的元素  
  176.     public T delete(int index)  
  177.     {  
  178.         if (index < 0 || index > size - 1)  
  179.         {  
  180.             throw new IndexOutOfBoundsException("线性表索引越界");  
  181.         }  
  182.         Node del = null;  
  183.         //如果被删除的是header节点  
  184.         if (index == 0)  
  185.         {  
  186.             del = header;  
  187.             header = header.next;  
  188.             //释放新的header节点的prev引用  
  189.             header.prev = null;  
  190.         }  
  191.         else  
  192.         {  
  193.             //获取删除点的前一个节点  
  194.             Node prev = getNodeByIndex(index - 1);  
  195.             //获取将要被删除的节点  
  196.             del = prev.next;  
  197.             //让被删除节点的next指向被删除节点的下一个节点。  
  198.             prev.next = del.next;  
  199.             //让被删除节点的下一个节点的prev指向prev节点。  
  200.             if (del.next != null)  
  201.             {  
  202.                 del.next.prev = prev;  
  203.             }         
  204.             //将被删除节点的prev、next引用赋为null.  
  205.             del.prev = null;  
  206.             del.next = null;  
  207.         }  
  208.         size--;  
  209.         return del.data;  
  210.     }  
  211.     //删除链式线性表中最后一个元素  
  212.     public T remove()  
  213.     {  
  214.         return delete(size - 1);  
  215.     }  
  216.     //判断链式线性表是否为空链表  
  217.     public boolean empty()  
  218.     {  
  219.         return size == 0;  
  220.     }  
  221.     //清空线性表  
  222.     public void clear()  
  223.     {  
  224.         //将底层数组所有元素赋为null  
  225.         header = null;  
  226.         tail = null;  
  227.         size = 0;  
  228.     }  
  229.     public String toString()  
  230.     {  
  231.         //链表为空链表时  
  232.         if (empty())  
  233.         {  
  234.             return "[]";  
  235.         }  
  236.         else  
  237.         {  
  238.             StringBuilder sb = new StringBuilder("[");  
  239.             for (Node current = header ; current != null  
  240.                 ; current = current.next )  
  241.             {  
  242.                 sb.append(current.data.toString() + ", ");  
  243.             }  
  244.             int len = sb.length();  
  245.             return sb.delete(len - 2 , len).append("]").toString();  
  246.         }  
  247.     }  
  248.     public String reverseToString()  
  249.     {  
  250.         //链表为空链表时  
  251.         if (empty())  
  252.         {  
  253.             return "[]";  
  254.         }  
  255.         else  
  256.         {  
  257.             StringBuilder sb = new StringBuilder("[");  
  258.             for (Node current = tail ; current != null   
  259.                 ; current = current.prev )  
  260.             {  
  261.                 sb.append(current.data.toString() + ", ");  
  262.             }  
  263.             int len = sb.length();  
  264.             return sb.delete(len - 2 , len).append("]").toString();  
  265.         }  
  266.     }  
  267.       
  268.     public static void main(String[] args)   
  269.     {  
  270.         DuLinkList<String> list = new DuLinkList<String>();  
  271.         list.insert("aaaa" , 0);  
  272.         list.add("bbbb");  
  273.         list.insert("cccc" , 0);  
  274.         //在索引为1处插入一个新元素  
  275.         list.insert("dddd" , 1);  
  276.         //输出顺序线性表的元素  
  277.         System.out.println(list);  
  278.         //删除索引为2处的元素  
  279.         list.delete(2);  
  280.         System.out.println(list);  
  281.         System.out.println(list.reverseToString());  
  282.         //获取cccc字符串在顺序线性表中的位置  
  283.         System.out.println("cccc在顺序线性表中的位置:"   
  284.             + list.locate("cccc"));  
  285.         System.out.println("链表中索引1处的元素:"   
  286.             + list.get(1));  
  287.         list.remove();  
  288.         System.out.println("调用remove方法后的链表:" + list);  
  289.         list.delete(0);  
  290.         System.out.println("调用delete(0)后的链表:" + list);  
  291.     }  
  292. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值