Java数据结构(1)---顺序表

以下程序在JDK1.5.0_05环境下调试通过,程序分3个文件,放在同一目录下

//List.java      顺序表抽象数据类型的接口定义

public interface List
{
public void insert(int i,Object obj) throws Exception;      //插入
public Object delete(int i) throws Exception;               //删除
public Object getData(int i) throws Exception;              //取数据元素
public int size();                                          //求元素个数
public boolean isEmpty();                                   //是否空
}

//SeqList.java      顺序表类

public class SeqList implements List
{
final int defaultSize = 10;

int maxSize;
int size;
Object[] listArray;

public SeqList()
{
      initiate(defaultSize);
}

public SeqList(int size)
{
      initiate(size);
}

private void initiate(int sz)
{
      maxSize = sz;
      size = 0;
      listArray = new Object[sz];
}

public void insert(int i,Object obj) throws Exception
{
      if(size == maxSize)
      {
       throw new Exception("顺序表已满无法插入!");
      }
      if(i < 0 || i > size)
      {
       throw new Exception("参数错误!");
      }

      for(int j = size;j > i;j--)
         listArray[j] = listArray[j-1];

      listArray[i] = obj;
      size++;
}

public Object delete(int i) throws Exception
{
      if(size == 0)
      {
       throw new Exception("顺序表已空无法删除!");
      }
      if(i < 0 || i > size-1)
      {
       throw new Exception("参数错误!");
      }
      Object it = listArray[i];
      for(int j = i;j < size-1;j++)
         listArray[j] = listArray[j+1];

      size--;
      return it;
}

public Object getData(int i) throws Exception
{
      if(i < 0 || i >= size)
      {
       throw new Exception("参数错误!");
      }
      return listArray[i];
}

public int size()
{
      return size;
}

public boolean isEmpty()
{
      return size == 0;
}

public int MoreDataDelete(SeqList L,Object x) throws Exception
{
      int i,j;
      int tag = 0;

      for(i = 0;i < L.size;i++)
      {
       if(x.equals(L.getData(i)))
       {
        L.delete(i);
        i--;
        tag = 1;
       }
      }
      return tag;
}
}

//SeqListTest1.java      示例程序1(主程序)


public class SeqListTest1
{
public static void main(String args[])
{
      SeqList seqlist = new SeqList(100);
      int n = 10;
      try
      {
       for(int i = 0;i < n;i++)
       {
        seqlist.insert(i,new Integer(i+1));
       }

       seqlist.delete(4);

       for(int i = 0;i < seqlist.size;i++)
       {
        System.out.print(seqlist.getData(i)+"      ");
       }
      }
      catch(Exception e)
      {
       System.out.println(e.getMessage());
      }
}
}

//SeqListTest2.java     示例程序2


public class SeqListTest2
{
public static void main(String args[])
{
    SeqList seqList = new SeqList(100);
    Student[] student;
          student = new Student[3];
          student[0] = new Student(2000001,"张三","男",20);
          student[1] = new Student(2000002,"李四","男",21);
          student[2] = new Student(2000003,"王五","女",22);

          int n = 3;
          try
          {
     for(int i = 0;i < n;i++)
     {
      seqList.insert(i,student[i]);
     }

     for(int i = 0;i < seqList.size;i++)
     {
      Student st = (Student)seqList.getData(i);
      System.out.println(st.getNumber()+"     "+st.getName()+"     "+st.getSex()+"     "+st.getAge());
     }
    }
    catch(Exception e)
    {
     System.out.println(e.getMessage());
    }
}
}

class Student
{
private long number;
private String name;
private String sex;
private int age;
Student(long number,String name,String sex,int age)
{
    this.number = number;
    this.name = name;
    this.sex = sex;
    this.age = age;
}

public long getNumber()
{
    return number;
}

public String getName()
{
    return name;
}

public String getSex()
{
    return sex;
}

public int getAge()
{
    return age;
}
}

本站原创,转帖请注明出处:http://hi.baidu.com/jadmin/blog/谢谢!

转载于:https://www.cnblogs.com/jadmin/archive/2007/04/29/2206469.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值