第三週 專案1 順序表的基本運算

2021-08-08 12:58:46 字數 3152 閱讀 7421

問題及描述:
/*

* 作 者:李琳汐

* 完成日期:2017 年 9 月 20 日

* 版 本 號:v1.0

** 問題描述:在已經建立線性表的基礎上,求線性表的長度listlength、求線性表l中指定位置的某個資料元素getelem、查詢元素locateelem的演算法。

* 程式輸出:輸出線性表

*/#include #include #define maxsize 50 //maxsize將用於後面定義儲存空間的大小

typedef int elemtype; //elemtype在不同場合可以根據問題的需要確定,在此取簡單的int

typedef struct

sqlist;

//自定義函式宣告部分

void createlist(sqlist *&l, elemtype a, int n);//用陣列建立線性表

void displist(sqlist *l);//輸出線性表displist(l)

bool listempty(sqlist *l);//判定是否為空表listempty(l)

//實現測試函式

int main()

; createlist(sq, x, 6);

displist(sq);

return 0;

}//下面實現要測試的各個自定義函式

//用陣列建立線性表

void createlist(sqlist *&l, elemtype a, int n)

//輸出線性表displist(l)

void displist(sqlist *l)

//判定是否為空表listempty(l)

bool listempty(sqlist *l)

執行及結果:

在原程式的基礎上增加: 

 增加求線性表的長度listlength的函式並測試; 

 增加求線性表l中指定位置的某個資料元素getelem的函式並測試; 

 增加查詢元素locateelem的函式並測試; 

#include #include #define maxsize 50    //maxsize將用於後面定義儲存空間的大小

typedef int elemtype; //elemtype在不同場合可以根據問題的需要確定,在此取簡單的int

typedef struct

sqlist;

//自定義函式宣告部分

void createlist(sqlist *&l, elemtype a, int n);//用陣列建立線性表

void displist(sqlist *l);//輸出線性表displist(l)

bool listempty(sqlist *l);//判定是否為空表listempty(l)

int listlength(sqlist *l); //求線性表的長度listlength(l)

bool getelem(sqlist *l,int i,elemtype &e); //求某個資料元素值getelem(l,i,e)

int locateelem(sqlist *l, elemtype e); //按元素值查詢locateelem(l,e)

//實現測試函式

int main()

; elemtype a;

int loc;

createlist(sq, x, 6);

displist(sq);

printf("表長度:%d\n", listlength(sq)); //測試求長度

if(getelem(sq, 3, a)) //測試在範圍內的情形

printf("找到了第3個元素值為:%d\n", a);

else

printf("第3個元素超出範圍!\n");

if(getelem(sq, 15, a)) //測試不在範圍內的情形

printf("找到了第15個元素值為:%d\n", a);

else

printf("第15個元素超出範圍!\n");

if((loc=locateelem(sq, 8))>0) //測試能找到的情形

printf("找到了,值為8的元素是第 %d 個\n", loc);

else

printf("值為8的元素木有找到!\n");

if((loc=locateelem(sq, 17))>0) //測試不能找到的情形

printf("找到了,值為17的元素是第 %d 個\n", loc);

else

printf("值為17的元素木有找到!\n");

return 0;

}//下面實現要測試的各個自定義函式

//用陣列建立線性表

void createlist(sqlist *&l, elemtype a, int n)

//輸出線性表displist(l)

void displist(sqlist *l)

//判定是否為空表listempty(l)

bool listempty(sqlist *l)

//求線性表的長度listlength(l)

int listlength(sqlist *l)

//求某個資料元素值getelem(l,i,e)

bool getelem(sqlist *l,int i,elemtype &e)

//按元素值查詢locateelem(l,e)

int locateelem(sqlist *l, elemtype e)

執行結果:

初始化線性表initlist和插入資料元素listinsert兩個演算法:

//實現測試函式

int main()

第三週 專案1 順序表的基本運算

檔名稱 cpp.cpp 作者 王超 完成日期 2015.9.18 問題描述 順序表的基本運算 include include define maxsize 50 typedef int elemtype typedef struct sqlist void createlist sqlist l,e...

第三週 專案1 順序表的基本運算

問題及 檔名稱 test.cpp 作 者 馬笑媛 完成日期 2015.9.16 版 本 號 v1.0 問題描述 目的是要測試建立線性表的演算法。包括求線性表的長度,求線性表中指定位置的某個元素 查詢元素 插入資料元素 刪除資料元素 初始化線性表 銷毀線性表。include include defin...

第三週專案1 順序表的基本運算

問題及 檔名稱 順序表的基本運算 完成日期 2015年9月18日 輸入描述 線性表 程式輸出 測試結果 1 include include define maxsize 50 maxsize將用於後面定義儲存空間的大小 typedef int elemtype elemtype在不同場合可以根據問題...