/*
* all right reserved.
* 檔名稱:creat2.cpp
* 作 者: 姜延鍇
* 完成日期:2023年9月14日
* 版 本 號:v1.9
*
*問題描述:實現順序表基本運算有演算法,依據「最小化」的原則進行測試。所謂最小化
原則,指的是利用盡可能少的基本運算,組成乙個程式,並設計main函式
完成測試。
*輸入描述:無
*程式輸出:依據各個函式而定
*/
(1)目的是要測試「建立線性表」的演算法createlist,為檢視建表的結果,需要實現「輸出線性表」的演算法displist。
在研習displist中發現,要輸出線性表,還要判斷表是否為空,這樣,實現判斷線性表是否為空的演算法listempty成為必要。
#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)
原始檔1
#include"head.h"
int main()
; createlist(sq, x, 6);
displist(sq);
return 0;
}
標頭檔案
#include"head.h"
void createlist(sqlist *&l, elemtype a, int n)
//輸出線性表displist(l)
void displist(sqlist *l)
//判定是否為空表listempty(l)
(2)在已經建立線性表的基礎上,求線性表的長度listlength、求線性表l中指定位置的某個資料元素getelem、查詢元素locateelem的演算法都可以實現了。就在原程式的基礎上增加:
增加求線性表的長度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)
原始檔
#include"head.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;
}
#include"head.h"
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)
#include #include #includeusing namespace std;
#define maxsize 50 //maxsize將用於後面定義儲存空間的大小
typedef int elemtype; //elemtype在不同場合可以根據問題的需要確定,在此取簡單的int
typedef struct
sqlist;
void displist(sqlist *l);//輸出線性表displist(l)
bool listempty(sqlist *l);//判定是否為空表listempty(l)
void initlist(sqlist *&l); //引用型指標
void destroylist(sqlist *&l); //銷毀線性表
bool listinsert(sqlist *&l,int i,elemtype e); //在位置i插入元素
bool listdelete(sqlist *&l,int i,elemtype &e); //刪除位置i的元素
int main()
//判定是否為空表listempty(l)
bool listempty(sqlist *l)
//初始化線性表initlist(l)
void initlist(sqlist *&l) //引用型指標
//銷毀線性表destroylist(l)
void destroylist(sqlist *&l)
//插入資料元素listinsert(l,i,e)
bool listinsert(sqlist *&l,int i,elemtype e)
//刪除資料元素listdelete(l,i,e)
第三週 順序表的基本運算
問題及 2015,煙台大學計算機與控制工程學院 問題描述 1 目的是要測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。在研習displist中發現,要輸出線性表,還要判斷表是否為空,這樣,實現判斷線性表是否為空的演算法listempt...
第三週 順序表的基本運算2
檔名稱 test.cpp 完成日期 2015年9月14日 版本號 v1.0 問題描述 實現順序表基本運算,插入資料元素listinsert 刪除資料元素listdelete 初始化線性表initlist 銷毀線性表destroylist。include include define maxsize ...
第三週實踐專案 順序表的基本運算
1 目的是要測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。在研習displist中發現,要輸出線性表,還要判斷表是否為空,這樣,實現判斷線性表是否為空的演算法listempty成為必要。這樣,再加上main函式,這個程式由4個函式構...