码迷,mamicode.com
首页 > 其他好文 > 详细

线性表顺序表示和实现

时间:2014-12-07 23:03:19      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   java   strong   

        线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。

        线性表中比较重要的有顺序表、单向链表和循环链表。本文主要来谈谈顺序表的实现。

        List.java       线性表的接口类

 1 package com.yeyan.linearlist;
 2 
 3 /**
 4  * 线性表接口
 5  * @author yeyan
 6  * @date   2014-12-07
 7  *  * API:
 8  * isEmpty():                 判断该线性表是否为空
 9  * length():                  表长
10  * get(int index):            获得第index的元素
11  * add(int index, T element): 在index位置之后添加一个元素
12  * remove(index):             删除索引为index的元素 
13  * clear() :                  清除线性表
14  *
15  */
16 
17 public interface List<T> {
18     public boolean isEmpty();
19     public int length();
20     public T get(int index) throws Exception;
21     public boolean add(int index, T element) throws Exception;
22     public T remove(int index) throws Exception;
23     public void clear();
24 }

        SeqList.java

  1 package com.yeyan.linearlist;
  2 /**
  3  * 顺序表实现类
  4  * @author yeyan
  5  * @date   2014-12-07
  6  * @param <T>
  7  */
  8 
  9 public class SeqList<T> implements List<T> {
 10     // 一个基类数组
 11     private Object[] table;
 12     // 当前线性表长度
 13     private int size;
 14     // 最大长度
 15     private int maxSize;
 16     // 默认长度
 17     private final int defaultSize = 20;
 18     /*
 19      * 无参构造函数
 20      */
 21     public SeqList() {
 22         maxSize = defaultSize;
 23         this.size = 0;
 24         table = new Object[maxSize];
 25     }
 26     /**
 27      * 有参构造函数
 28      * @param size
 29      */
 30     public SeqList(int size) {
 31         maxSize = size;
 32         this.size = 0;
 33         table = new Object[maxSize];
 34     }
 35     /**
 36      * 获取元素个数
 37      */
 38     public int length() {
 39         return size;
 40     }
 41     /**
 42      * 是否为空
 43      */
 44     public boolean isEmpty() {
 45         return size == 0;
 46     }
 47 
 48     /**
 49      * 获取索引为index的元素
 50      */
 51     public T get(int index) throws Exception {
 52         if(index < 0 || index > size){
 53             throw new Exception("paramether error!");
 54         }
 55         return (T)table[index];
 56     }
 57     /**
 58      * 添加一个元素到索引为index之后
 59      */
 60     public boolean add(int index, Object element) throws Exception {
 61         if(element == null) return false;
 62         if(size == maxSize){
 63             throw new Exception("linear size is full£¡");
 64         }
 65         if(index < 0 || index > maxSize){
 66             throw new Exception("parameter!");
 67         }
 68         //move element
 69         for(int i = size; i > index; i --){
 70             table[i] = table[i - 1];
 71         }
 72         table[index] = element;
 73         size ++;
 74         return true;
 75     }
 76     /**
 77      * 删除索引为index的元素
 78      */
 79     public T remove(int index) throws Exception {
 80         T element = (T)table[index];
 81         if(isEmpty()){
 82             throw new Exception("linear list is null£¡");
 83         }
 84         if(index < 0 || index > size - 1){
 85             throw new Exception("parameter error£¡");
 86         }
 87         //move element
 88         for(int i = index; i < size - 1; i ++){
 89             table[i] = table[i+1];
 90         }
 91         size --;
 92         return element;
 93     }
 94     /**
 95      * 清空顺序表
 96      */
 97     public void clear() {
 98         for(int i = 0; i < size; i ++){
 99             table[i] = null;
100         }
101         size = 0;
102     }
103 }

        Test.java

 1 package com.yeyan.linearlist;
 2 /**
 3  * 顺序表测试类
 4  * @author yeyan
 5  * @date   2014-12-07
 6  */
 7 public class Test {
 8     public static void main(String[] args) throws Exception{
 9         SeqList<Integer> seqList = new SeqList<Integer>();
10         seqList.add(0, 100);
11         seqList.add(1, 102);
12         seqList.add(2, 104);
13         System.out.println(seqList.length());
14         System.out.println(seqList.isEmpty());
15         System.out.println(seqList.get(0));
16         System.out.println(seqList.remove(1));
17     }
18 }

顺序表操作的效率分析

        顺序表的静态特性很好,动态特性很差,具体说明如下:

        1,顺序表元素的物理存储顺序直接反映线性表元素的逻辑顺序,顺序表示一种随机存取结构。get()、set()方法的时间复杂度为o(1)。

        2,插入和删除效率很低。如果在各个位置插入元素的概率相同,复杂度为O(n)。

线性表顺序表示和实现

标签:style   blog   io   ar   color   sp   for   java   strong   

原文地址:http://www.cnblogs.com/jones-c/p/4150054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!