顺序表

来源:互联网 发布:淘宝网经营模式 编辑:程序博客网 时间:2024/04/30 13:49
  1. /* 
  2.  *定义: 
  3. 顺序表是指线性表在顺序储存形式下构成的表 
  4.  
  5. 基本操作包括建立、插入、删除、查找等 
  6.  **/  
  7.    
  8. /* 
  9.  *@void setData(Object[] obj)-建表 
  10.  *@Object[] getData()-得到表的数据 
  11.  *@void setLength(int length)-设置表已有内容长度 
  12.  *@int getLength()-得到表长 
  13.  *@boolean insert(int index, Object obj)-插入数据 
  14.  *@boolean detele(int index)-删除数据 
  15.  *@int find(Object obj)-查找数据(找不到返回-1) 
  16.  *@boolean replace()-替换数据 
  17.  * 
  18.  * 
  19.  *以1为基准 
  20.  **/  
  21.   
  22. class LineList  
  23. {  
  24.     private Object[] data;  
  25.     private int length;  
  26.       
  27.     public LineList(){}  
  28.     public LineList(Object[] data)  
  29.     {  
  30.         this.data = data;  
  31.         length = data.length;  
  32.     }  
  33.     public void setData(Object[] data) {this.data = data;}  
  34.       
  35.     public Object[] getData()   
  36.     {  
  37.         Object[] response = new Object[length];  
  38.         for(int i = 0; i < length; i++)  
  39.             response[i] = data[i];  
  40.         return response;  
  41.     }  
  42.       
  43.     public void setLength(int length) {this.length = length;}  
  44.     public int getLength() {return length;}  
  45.       
  46.     public boolean insert(int index, Object obj)  
  47.     {  
  48.         if(length >= data.length)  
  49.         {  
  50.             System.out.println("The table is overflow.");  
  51.             return false;  
  52.         }  
  53.         if(index < 1 || index > length)  
  54.         {  
  55.             System.out.println("The position is mistake: " + index);  
  56.             return false;  
  57.         }  
  58.           
  59.         --index;  
  60.         for(int i = length-1; i >= index; i--)  
  61.             data[i+1] = data[i];  
  62.           
  63.         data[index] = obj;  
  64.         length++;  
  65.         return true;  
  66.     }  
  67.       
  68.     public boolean delete(int index)  
  69.     {  
  70.         if(length < 1)  
  71.         {  
  72.             System.out.println("The table is null");  
  73.             return false;  
  74.         }  
  75.         if(index < 1 || index > length)  
  76.         {  
  77.             System.out.println("The position is mistake: " + index);  
  78.             return false;  
  79.         }  
  80.           
  81.         int len = length-1;  
  82.         for(int i = index-1; i < len; i++)  
  83.             data[i] = data[i+1];  
  84.         length--;  
  85.         return true;  
  86.     }  
  87.       
  88.     public int find(Object obj)  
  89.     {  
  90.         for(int i = 0; i <= length; i++)  
  91.             if(data[i] == obj)  
  92.                 return i+1;  
  93.                   
  94.         return -1;  
  95.     }  
  96.       
  97.     public boolean replace(int index, Object obj)  
  98.     {  
  99.         if(index < 1 || index > length)  
  100.         {  
  101.             System.out.println("The position is mistak: " + index);  
  102.             return false;  
  103.         }  
  104.           
  105.         data[index-1] = obj;  
  106.         return true;  
  107.     }  
  108. }  
  109.   
  110. class Test  
  111. {  
  112.     public static void main(String[] args)  
  113.     {  
  114.         Integer[] a = {12345678910};  
  115.         LineList lineList = new LineList(a);  
  116.         lineList.insert(05);//已满  
  117.         lineList.delete(2);  
  118.         lineList.insert(513);  
  119.   
  120.         /* 
  121.         //全部删除,没有数据元素了还继续删 
  122.         Object[] response = lineList.getData(); 
  123.         for(int i = 0; i < response.length; i++) 
  124.             lineList.delete(1); 
  125.         lineList.delete(1);  
  126.         */  
  127.     //  lineList.delete(2);  
  128.         lineList.replace(1018);  
  129.         System.out.println("lineList's length: " + lineList.getLength());  
  130.         Object[] response = lineList.getData();  
  131.           
  132.         System.out.println("find " + 5 + ": " + lineList.find(5));  
  133.         for(int i = 0; i < response.length; i++)  
  134.             System.out.print(response[i] + " ");  
  135.     }  
原创粉丝点击