3 Implementation and Application of a linear structure and sequence table

线性结构定义
如果一个数据元素序列满足:
(1)除第一个和最后一个数据元素外,每个数据元素只有一个前驱数据元素和一个后继数据元素
(2)第一个数据元素没有前驱数据元素;
(3)最后一个数据元素没有后记数据元素
则称这样的数据结构为线性结构
线性表抽象数据类型
线性表抽象数据类型主要包括两个方面:既数据集合和该数据集合上的操作集合。
数据集合可以表示为a0,a1,a2,...an-1,每个数据元素的数据类型可以是任意的类型

操作集合包括如下:
1求元素个数
2插入
3删除
4查找
5判断是否为空

顺序表
计算机有两种基本的存储结构:顺序结构,离散结构,使用顺序结构实现的线性表成为顺序表
栈内存:顺序存储结构, 堆,离散存储结构
顺序表效率分析
(1)顺序表插入和删除一个元素的时间复杂度为O(n)
(2)顺序表支持随机访问,顺序表读取一个元素的时间复杂度为O(1)
(3)顺序表的优点是:支持随机访问,空间利用率高
(4)顺序表的缺点是:大小固定,插入和删除元素需要移动大量的数据
A List.java
// linear meter interface
public interface List {
   // table length to obtain a linear
   public int size();
   // table is empty is determined linear
   public boolean isEmpty();
   // insert elements
   public void insert(int index,Object obj) throws Exception;
    // delete elements
   public void delete(int index) throws Exception;
   // Get the specified location element
   public Object get(int index) throws Exception;
}
Two SequenceList.java
public class SequenceList implements List{
    
   // default maximum length sequence table
   final int defaultSize =10;
   //The maximum length
   int maxSize;
   // current length
   int size;
   // array of objects
   Object[] listArray;
   
   
   public SequenceList()
   {
      init(defaultSize);
   }
   // overloaded constructor
   public SequenceList(int size)
   {
      init(size);
   }
   
   // initialization method sequence table
   private void init(int size)
   {
      maxSize = size;
      this.size = 0;
      listArray = new Object[size];
   }
   
   @Override
   public void delete(int index) throws Exception {
      // TODO Auto-generated method stub
      if(isEmpty())
      {
         throw new Exception ( "sequence table is empty, can not be deleted!");
      }
      if(index<0||index>size-1)
      {
         throw new Exception ( "Parameter Error!");
      }
      // move elements
      for(int j=index;j<size-1;j++)
      {
         listArray[j]=listArray[j+1];
      }
      size--;
   }

   @Override
   public Object get(int index) throws Exception {
      // TODO Auto-generated method stub
      if(index<0||index>=size)
      {
         throw new Exception ( "Parameter Error!");
      }
      return listArray[index];
   }

   @Override
   public void insert(int index, Object obj) throws Exception {
      // TODO Auto-generated method stub
      // linear table is full
      if(size==maxSize)
      {
         throw new Exception ( "the order table is full, you can not insert!");
      }
      // insert position number is legitimate
      if(index<0||index>size)
      {
         throw new Exception ( "Parameter Error!");
      }
      // move elements
      for(int j=size;j>index;j--)
      {
         listArray[j]=listArray[j-1];
      }
      
      listArray[index]=obj;
      size++;
      
   }

   @Override
   public boolean isEmpty() {
      // TODO Auto-generated method stub
      return size==0;
   }

   @Override
   public int size() {
      // TODO Auto-generated method stub
      return size;
   }

}
Three Students.java
// class students
public class Students {

   private String sid;// 学号
   private String name; // Name
   private String gender; // Sex
   private int age; // Age
    
   public Students()
   {
      
   }
   
   public Students(String sid,String name,String gender,int age)
   {
      this.sid = sid;
      this.name = name;
      this.gender = gender;
      this.age =age;
   }
   
   public String toString()
   {
      return "Student ID:" + this.getSid () + "Name:" + this.getName () + "Sex:" + this.getGender () + "Age:" + this.getAge ();  
   }
   
   public String getSid() {
      return sid;
   }

   public void setSid(String sid) {
      this.sid = sid;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getGender() {
      return gender;
   }

   public void setGender(String gender) {
      this.gender = gender;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

}


Four Test.java file
public class Test {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
        SequenceList list = new SequenceList(100);
        try
        {
           list.insert(list.size, new Students("S0001","张三","男",18));
           list.insert(list.size, new Students("S0002","李四","男",19));
           list.insert(list.size, new Students("S0003","王五","女",21));
           
           for(int i=0;i<list.size;i++)
           {
              System.out.println(list.get(i));
           }
           
        }
        catch(Exception ex)
        {
           ex.printStackTrace();
        }
        
   }

}

Guess you like

Origin blog.csdn.net/xsjzdrxsjzdr/article/details/89222780