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

2021-07-05 07:01:44 字數 2130 閱讀 7276

問題及**:

#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)

執行結果:

成功的實現了以上所有要求。

知識點總結:

建立線性表的演算法createlist

為檢視建表的結果,需要實現輸出線性表的演算法displist

判斷線性表是否為空的演算法listempty

求線性表的長度listlength

求線性表l中指定位置的某個資料元素getelem

查詢元素locateelem的演算法

學習心得:

在建表以及輸出的時候不能僅僅用createlist以及displist,還需要判斷表是否為空listempty。

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

1 目的是要測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。在研習displist中發現,要輸出線性表,還要判斷表是否為空,這樣,實現判斷線性表是否為空的演算法listempty成為必要。這樣,再加上main函式,這個程式由4個函式構...

第三週上機實踐 專案1 順序表的基本運算

檔名稱 test.cpp 完成日期 2015年9月14日 版本 v1.0 問題描述 測試 建立線性表的 的演算法createlist,以及需要實現 輸出線性表 的演算法displist 輸入描述 宣告自定義函式 定義main函式 定義各個自定義函式。程式輸出 輸出所得結果。include inclu...

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

問題與 檔名稱 shun.cpp 完成日期 2015年9月18日 版本號 v1.0 問題描述 測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。在研習displist 中發現,要輸出線性表,還要判斷表是否為空,這樣,實 現判斷線性表是否...