1. 程式人生 > >資料結構實驗一線性表的基本操作實現及其應用

資料結構實驗一線性表的基本操作實現及其應用

一.實驗名稱

線性表的基本操作實現及其應用

二.實驗目的

  1. 熟練掌握線性表的結構特點,掌握順序表的基本操作。
  2. 學會使用順序表解決實際問題。

三.實驗內容

建立 n 個元素的順序表(n 的大小和表裡資料自己確定),實現相關的操作:輸出,插入,刪除,查詢等功能。編寫完整程式實現,程式語言不限定,使用技術形式不定。

四.演算法實現

本實驗我採用java語言進行實現。

順序表設定:

class SqlList <T>{
    private int index ; //索引
    private Object[] DateType; //存放的陣列
    // 單參構造 輸入要建立的順序表的大小
public SqlList(int MaxSize) { this.index = 0 ; DateType = new Object [MaxSize]; } // 單參構造 批量匯入要建立的順序表的資料 public SqlList(String[] dateType) { super(); this.index = dateType.length ; DateType = dateType; } //getter setter public Object[] getDateType
() { return DateType; } public void setDateType(Object[] dateType) { DateType = dateType; } //獲取順序表的長度 public int getDateTypeLength() { return this.DateType.length; } }

新增操作:

public boolean add(T date ) throws Exception{ //增加資料
  if  (date == null){
    throw
new Exception ("輸入的資料為空"); } if (index == DateType.length) { throw new Exception ("順序表已滿"); } this.DateType[index ++] = date ; return true ;

首先判斷新增的資料是否合法,是否為空,然後通過判斷索引index和陣列的長度的關係來判斷是否能進行新增,新增完成後索引index增加1.

批量匯入多個數據:

public boolean addAll(T date[]) throws Exception {
  if (date == null) {
    throw new Exception ("輸入的資料為空");            
  }
  for (int x = 0;x < date.length ; x++) {
    this.DateType[index ++] = date [x] ;
  }
  return true ;
}

依舊首先判斷新增的資料是否合法,是否為空,然後通過判斷索引index和陣列的長度的關係來判斷是否能進行新增。之後使用一個for迴圈將輸入的數組裡的資料進行匯入新增。

統計順序表資料的數量

public int getIndex() {
  return this.index ;
}

這個操作比較簡單 ,直接返回設定好的索引index的值即可。

通過給定的資料來查詢資料:

public void contains(T date) throws Exception  {
  if (date == null ){
  throw new Exception ("輸入的資料為空");
  }

  if (this.index == 0) {
  throw new Exception ("順序表為空") ;
  }
  int x = 0;
  while (x < this.index && ! DateType[x].equals(date)) {
    x ++ ;
    if(x == this.index ) {
      System.out.println("沒找到該資料");
    }
  }
  System.out.println("找到該資料,其索引是:"+ x + ",其順序是第" + (x+1) + "個");
}

首先判斷新增的資料是否合法,是否為空,然後通過判斷索引index是否為0來判斷順序表是否有儲存資料,接著通過一個while迴圈進行查詢,while迴圈的兩個判斷條件分別是查詢的腳標x是否大於順序表的索引index或者是否成功對比到相符合的資料。若比對到最後x==index時還未找到,說明該資料並不存在於順序表 。若找到該資料 ,則會跳出該迴圈,接著顯示x的值,並顯示它在順序表中是第幾位。

通過索引來獲取資料:

public Object getDate(int index)  throws Exception  {

  if (this.index == 0) {
    throw new Exception ("順序表為空") ;
  } 
  if (index <= 0 && index > this.index) {
    throw new Exception ("輸入的資料有誤") ;
  }
   return this.DateType[index];
}

這個操作沒什麼特別的地方 ,還是需要注意先判斷輸入的index的合法性,先判斷順序表的index是否為空,再判斷填入的index是否小於等於0,然後判斷是否比順序表的index還要大。

通過索引移除資料:

public void remove(int index) throws Exception{
  if (this.index == 0) {
    throw new Exception ("順序表為空") ;
  } 
  if (index > this.index && index < 0) {
    throw new Exception ("輸入的資料有誤") ;
  }

  for (int y = 0 ; y < this.index ;y++ ) {
    this.DateType[index] = this.DateType[index+1];
  }
  this.index--;
  System.out.println("刪除成功");

顯示所有新增的資料:

public void PrintList(){
  for (int x = 0 ; x < index ; x ++) {
    System.out.println("第"+ (x+1) +"個數據為:"+ DateType[x]);
  }
  System.out.printf("\n"); 
}
  }

測試資料

public class TestDemo{
     public static void main(String[] args) throws Exception{
         SqlList<String> sql = new SqlList<String>(100) ;
         sql.add("第1個");
         sql.add("第2個");
         sql.add("第3個");
         sql.PrintList();
         sql.contains("第2個");
         System.out.println("索引1的資料為"+sql.getDate(1));
         sql.remove(1);
         sql.PrintList();        
    }
}

執行效果

這裡寫圖片描述

五.總結與心得:

學習java沒多久,這次的順序表實驗是我通過之前對連結串列的學習的知識轉化而來的。錯誤應該頗多,如果有哪裡需要改進請指出。

附:原始碼

package cn.hgh.demo;

class SqlList <T>{
    private int index ; //索引
    private Object[] DateType; //存放的陣列
    // 單參構造 輸入要建立的順序表的大小
    public SqlList(int MaxSize) {
        this.index = 0 ;
        DateType = new Object [MaxSize];
     }
    // 單參構造 批量匯入要建立的順序表的資料
    public SqlList(String[] dateType) {
        super();
        this.index = dateType.length ; 
        DateType = dateType;
    }
    //getter setter 
    public Object[] getDateType() {
        return DateType;
    }
    public void setDateType(Object[] dateType) {
        DateType = dateType;
    }
    //獲取順序表的長度
    public int getDateTypeLength() {
        return this.DateType.length;
    }
    //增加資料
    public boolean add(T date ) throws Exception{ //增加資料
        if  (date == null){
            throw new Exception ("輸入的資料為空");
        }
        if (index == DateType.length) {
            throw new Exception ("順序表已滿");
        }
        this.DateType[index ++] = date ;
        System.out.println("資料"+this.index+"建立成功!");
        System.out.printf("\n"); 
        return true ;
    }
    //批量匯入多個數據
    public boolean addAll(T date[]) throws Exception {
        if (date == null) {
            throw new Exception ("輸入的資料為空");            
        }
        for (int x = 0;x < date.length ; x++) {
            this.DateType[index ++] = date [x] ;
        }
        System.out.println("匯入成功!");
        System.out.printf("\n"); 
        return true ;
    }
    //統計順序表資料的數量,返回index
    public int getIndex() {
        return this.index ;
    }
    public void contains(T date) throws Exception  {
        if (date == null ){
        throw new Exception ("輸入的資料為空");
        }
        if (this.index == 0) {
        throw new Exception ("順序表為空") ;
        }
        int x = 0;
        while (x < this.index && ! DateType[x].equals(date)) {
            x ++ ;
            if(x == this.index ) {
                System.out.println("沒找到該資料");
                System.out.printf("\n"); 
            }
        }
        System.out.println("找到該資料,其索引是:"+ x + ",其順序是第" + (x+1) + "個");
        System.out.printf("\n"); 
    }
    public Object getDate(int index)  throws Exception  {

        if (this.index == 0) {
            throw new Exception ("順序表為空") ;
        }   
        if (index <= 0 || index > this.index) {
            throw new Exception ("輸入的資料有誤") ;
        }
         return this.DateType[index];
    }
    public void remove(int index) throws Exception{
        if (this.index == 0) {
            throw new Exception ("順序表為空") ;
        }   
        if (index > this.index && index < 0) {
            throw new Exception ("輸入的資料有誤") ;
        }

        for (int y = 0 ; y < this.index ;y++ ) {
            this.DateType[index] = this.DateType[index+1];
        }
        this.index--;
        System.out.println("刪除成功");
        System.out.printf("\n"); 
    }
    public void PrintList(){
        System.out.println("該資料表儲存的資料有:");
        for (int x = 0 ; x < index ; x ++) {
            System.out.println("第"+ (x+1) +"個數據為:"+ DateType[x]);
        }
        System.out.printf("\n"); 
    }
        }

public class TestDemo{
     public static void main(String[] args) throws Exception{
         SqlList<String> sql = new SqlList<String>(100) ;
         sql.add("第1個");
         sql.add("第2個");
         sql.add("第3個");
         sql.PrintList();
         sql.contains("第2個");
         System.out.println("索引1的資料為"+sql.getDate(1));
         System.out.printf("\n"); 
         sql.remove(1);
         sql.PrintList();        
    }
}