posts - 495,  comments - 11,  trackbacks - 0

以下程序在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/谢谢!

posted on 2007-04-29 17:57 jadmin 阅读(78) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: