資料結構實踐 順序表的基本運算2

2021-07-05 07:07:04 字數 1963 閱讀 6360

/*

* 檔名稱:cpp1.cpp

* 版本號:v6.0

* 增加求線性表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)

資料結構 基本運算驗證性實踐路線 順序表

writer 羅海員 date 2015年9月15日 版本 v1.0.1 作業系統 xp 執行環境 vc6.0 問題描述 1.目的是要測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。2.在研習displist中發現,要輸出線性表,還要...

資料結構實踐 順序表應用(2)

檔名稱 main.cpp,list.cpp,list.h 完成日期 2015年9月21日 版本號 vc 6.0 include include define maxsize 50 typedef int elemtype typedef struct sqlist void createlist s...

資料結構之順序表的基本運算

include include include include using namespace std define maxsize 100 typedef char elemtype typedef struct sqlist void initlist sqlist l 初始化順序表l void...