顺序表

数据结构——顺序表结构
// 定义接口
public interface IList {

      //  i的 取值范围是  0<=i<length()-1
    public void clear();

    public boolean isEmpty(); 

    public int length(); 

    public Object get(int i) throws Exception;

    public void insert(int i,Object x) throws Exception;

    public void remove(int i) throws Exception;

    public int indexOf(Object x);

    public void display();



}



public class SqList implements IList {

     private Object[] ListElem;   //  线性表的存贮空间
     private int curLen;     //  线性表的实际长度

     // 构造函数
     public SqList(int Maxsize)
     {
         curLen = 0;          
         ListElem = new Object[Maxsize];   // 为顺序表分配Maxsize个存储单元
     }


    @Override
    public void clear() {
        // TODO 自动生成的方法存根

        curLen = 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO 自动生成的方法存根
//      return false;
        return curLen == 0;


    }

    @Override
    public int length() {
        // TODO 自动生成的方法存根
        return curLen ;
    }


    // 该方法是读取线性表中的第i个元素,并返回该值
    // 其中i的取值范围是: 0<=i <=length()-1 ,否则会抛出异常
    @Override
    public Object get(int i) throws Exception {
        // TODO 自动生成的方法存根
          if(0<i||i>curLen-1)

              throw new Exception("第"+i+"个元素不存在");

           return ListElem[i];  
    }
     // 线性表插入
    @Override
    public void insert(int i, Object x)throws Exception {
        // TODO 自动生成的方法存根
        // 1.当线性表已经满了则要抛出异常
        // 2.当i的位置不合法时也要抛出异常
         if(curLen == ListElem.length)
             throw new Exception("顺序表已满");
         if(i<0||i>curLen)
             throw new Exception("插入的位置不合理");
         for(int j = curLen;j>i;j--)
         ListElem[j] = ListElem[j-1];
         ListElem[i] = x;   // 中间插入x
         curLen++;  
    }
   //  线性表的删除
    @Override
    public void remove(int i) throws Exception{
        // TODO 自动生成的方法存根
        //   位置不合法,就要抛出异常。
        if(i<0||i>curLen)
            throw new Exception("删除的位置不合理");
        for(int j = i; j<curLen-1;j++)
            ListElem[j] = ListElem[j+1];
        curLen++;



    }

    //  顺序表的查找,主要查找满足指定条件的数据元素初次出现的位置。
    @Override
    public int indexOf(Object x) {
        // TODO 自动生成的方法存根
         int j = 0;
         // 关键代码: !ListElem[j].equals(x)
         while(j<curLen&&!ListElem[j].equals(x))

             j++;
         if(j<curLen)
             return j;
         else 
             return -1;  
    }

     // 输出线性表所有元素
    @Override
    public void display() {
        // TODO 自动生成的方法存根
        for(int j = 0;j<curLen;j++)
         System.out.print(ListElem[j]+"  ");
        System.out.println(" ");



    }

}



// 题目:题目: 从一下顺序表中找出元素'z'首次出现的位置 
// {'a','z','d','m','z'}

public class Example2_1 {
    public static void main(String[] args) throws Exception
    {
        SqList L = new SqList(10);
        L.insert(0, 'a');
        L.insert(1, 'z');
        L.insert(2, 'd');
        L.insert(3, 'm');
        L.insert(4, 'z');

        int order = L.indexOf('z');





         if(order != -1)
            System.out.println("顺序表中第一次出现的值为'z'的数据的位置是:"+order);
         else 
             System.out.println("顺序表中不包含值为'z'的数据元素");

    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值