顺序表线性表(java版)

上一篇 / 下一篇  2008-04-29 16:29:23 / 个人分类:数据结构

1List

package zhangy;

import java.lang.Exception;

 

public interface List {

public   void insertElement(int i,Object o) throws Exception;

public void deleteElement(int i) throws Exception;

public Object getElement(int i) throws Exception;

public int getSize();

public boolean isEmpty();

 

 

}

2SeqList

package zhangy;

 

 

 

public class SeqList implements List {

      //元素个数

      private int size;

      //最大存储元素个数

      private int maxSize;

      //存储

      private Object[] arraylist;

      

      

      

//初始化空表 

SeqList(int maxSize)

{

      size=0;

      this.maxSize=maxSize;

      arraylist=new Object[maxSize];

      

}

 

//插入新元素

 

 

/**

 *把元素0插入到第i个位置

 * @param ii个位置,o元素o

 **/

public void insertElement(int i,Object o)throws Exception

{

      if(size==maxSize)

      {

             throw new Exception("顺序表已满");

      }

      if(i<0||i>size)

      {

             throw new Exception("位置错误");

      }

      

      for(int j=size;j>=i+1;j--)

      {

             arraylist[j]=arraylist[j-1];

             

      }

      arraylist[i]=o;

      size++;

      

      

}

 

/**

 *把第i个元素删除

 * @param ii个元素

 */

 

public void deleteElement(int i) throws Exception

{

 

      if(i>=size||size==0)

      {

             throw new Exception("位置错误");

             

      }

      for(int j=i;j<size;j++)

      {

             arraylist[j]=arraylist[j+1];

      }

      size--;

      

}

 

/**

 *获得第i个元素

 */

public Object getElement(int i) throws Exception

{

 

      if(i<0||i>=size)

      {

             throw new Exception("位置错误");

      }

      

      return arraylist[i];

      

}

 

public int getSize()

{

      

 return size;   

}

 

public boolean isEmpty()

{

      if(size==0)

      {

             return true;

      }

      else

      {

             return false;

             

      }

}

 

}

 


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-07-07  
  12345
6789101112
13141516171819
20212223242526
2728293031  

数据统计

  • 访问量: 46
  • 日志数: 1007
  • 建立时间: 2008-04-29
  • 更新时间: 2008-05-13

RSS订阅

Open Toolbar