第三週專案2 建設「順序表」

2021-07-22 16:17:44 字數 4045 閱讀 5308

問題**:

/*    

*煙台大學計算計控制與工程學院

*作 者:劉倩

*完成日期:2023年9月12日

*問題描述:本文為演算法庫中的第乙個,針對線性表中的順序儲存結構,實現各種基本運算。

演算法庫包括兩個檔案:

標頭檔案:list.h,包含定義順序表資料結構的**、巨集定義、要實現演算法的函式的宣告;

原始檔:list.cpp,包含實現各種演算法的函式的定義

*/

1.list.h的**:

#include#include#define maxsize 50  

typedef int elemtype;

typedef struct

sqlist;

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

void initlist(sqlist *&l);//初始化線性表initlist(l)

void destroylist(sqlist *&l);//銷毀線性表destroylist(l)

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

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

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

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

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

bool listinsert(sqlist *&l,int i,elemtype e);//插入資料元素listinsert(l,i,e)

bool listdelete(sqlist *&l,int i,elemtype &e);//刪除資料元素listdelete(l,i,e)

2.list.cpp的**:

#include "list.h"  

//用陣列建立線性表

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

//初始化線性表initlist(l)

void initlist(sqlist *&l) //引用型指標

//銷毀線性表destroylist(l)

void destroylist(sqlist *&l)

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

bool listempty(sqlist *l)

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

int listlength(sqlist *l)

//輸出線性表displist(l)

void displist(sqlist *l)

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

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

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

int locateelem(sqlist *l, elemtype e)

//插入資料元素listinsert(l,i,e)

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

//刪除資料元素listdelete(l,i,e)

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

3.各種main的測試函式的**

(1)測試「建立線性表」的演算法createlist,為檢視建表的結果,需要實現「輸出線性表」的演算法displist。

#include"list.h"

//實現測試函式

int main()

; createlist(sq,x,6);

displist (sq);

return 0;

}

執行結果:

(2)在已經建立線性表的基礎上,求線性表的長度listlength、求線性表l中指定位置的某個資料元素getelem、查詢元素locateelem的演算法都可以實現了。

#include"list.h"

//實現測試函式

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;

}

執行結果:

(3)插入資料元素listinsert、刪除資料元素listdelete、初始化線性表initlist、銷毀線性表destroylist。

#include"list.h"

//實現測試函式

int main()

執行結果:

知識點總結:

實現線性表的基本運算:(1)初始化線性表initlist(&l):構造乙個空的線性表l(2)銷毀線性表destroylist(&l):釋放線性表l占用的記憶體空間(3)判線性表是否為空表listempty(l):若l為空表,則返回真,否則返回假(4)求線性表的長度listlength(l):返回l中元素個數(5)輸出線性表displist(l):當線性表l不為空時,順序顯示l中各節點的值域(6)求線性表l中指定位置的某個資料元素getelem(l,i,&e):用e返回l中第 i 個元素的值(7)查詢元素locateelem(l,e):返回線性表l中第1個與e相等的序號,找不到返回0。(8)插入資料元素listinsert(sqlist *&l,int i,elemtype e):在l的第i個元素之前插入新的元素e,l的長度增加1;(9)刪除資料元素listdele(&l,i,&e):刪除l的第i個資料元素,並用e返回其值,l的長度減1;(10)銷毀線性表destroylist(sqlist *&l):釋放線性表l占用的記憶體空間;

學習心得:

工程量大,但是第乙個做出來之後,這個就簡單了。

第三週 專案2 建設順序表演算法庫

檔名稱 cpp.cpp 作者 王超 完成日期 2015.9.18 問題描述 建設順序表演算法庫 include include define maxsize 50 typedef int elemtype typedef struct sqlist void createlist sqlist l,...

第三週專案2 建設「順序表」演算法庫

問題及 檔名稱 專案2.cbp 作 者 完成日期 2015年9月16日 版 本 號 v1.0 問題描述 請採用程式的多檔案組織形式,在專案1的基礎上,建立 如上的兩個檔案,另外再建立乙個原始檔,編制main函 數,完成相關的測試工作。輸入描述 無 程式輸出 依據各個函式而定 list.h檔案 ifn...

第三週 專案2 建設「順序表」演算法庫

01.04.檔名稱 list.cpp 05.作 者 蘇凱祺 06.完成日期 2014年10月9號 07.版 本 號 v1.0 08.問題描述 建設 順序表 的演算法庫 09.list.h ifndef list h included define list h included define max...