順序表的9種基本運算為:
我發現嚴蔚敏版的《資料結構》裡這9種運算沒有都給出,所以自己做了總結:initlist(sqlist &l) // 初始化順序表
listempty(sqlist &l) // 判斷順序表是否為空
displist(sqlist &l) // 列印順序表
listinsert(sqlist &l,int i,int e) // 將新元素 e 插入到表 l 的第 i 個位置之前
listdelete(sqlist &l, int i, elemtype &e) // 刪除表 l 中的第 i 個元素並用 e 返回出來
listlength(sqlist &l) // 列印順序表 l 的長度
getelem(sqlist &l, int i, elemtype &e) // 獲取表 l 中的第 i 個元素並用 e 返回
locateelem(sqlist &l, elemtype e) // 獲得表 l 中第乙個與 e 相等的元素的邏輯位序
destroylist(sqlist &l) // 銷毀順序表 l
執行結果示意圖:#include#include#include#define ok 1
#define error 0
#define true 1
#define false 0
#define infeasible -1
#define overflow -2
#define list_init_size 5
#define listincrement 2
typedef int elemtype;
typedef int status;
typedef struct
sqlist;
status initlist(sqlist &l)
//initlist
status listempty(sqlist &l)
//listempty
status displist(sqlist &l)
for(int i = 0; i < l.length; i++)
printf("%d\t", l.elem[i]);
return ok;
}//displist
status listinsert(sqlist &l,int i,int e)
if (l.length >= l.listsize)
// 將插入位置之後的資料元素後移,為插入元素挪出位置
for (int a=l.length-1; a>=i-1; a--)
l.elem[a+1]=l.elem[a];
// 插入資料元素
l.elem[i-1] = e;
l.length++;
return ok;
}//listinsert
status listdelete(sqlist &l, int i, elemtype &e)
//listdelete
status listlength(sqlist &l)
//listlength
status getelem(sqlist &l, int i, elemtype &e)
//getelem
status destroylist(sqlist &l)
//destroylist
status locateelem(sqlist &l, elemtype e)
return ok;
}//locateelem
status judgeelem(sqlist &l, elemtype e)
int main()
printf("\n建立好的順序表為:\n");
displist(l);
// getelem
printf("\n請輸入想要獲取的元素的邏輯位序:\n");
scanf("%d",&k);
if(getelem(l, k, b)) printf("\n該元素為:%d\n", b);
else printf("\n輸入有誤!\n");
// 刪除
printf("\n請輸入想要刪除的元素的邏輯位序:\n");
scanf("%d", &l);
if(listdelete(l, l, c)) printf("\n刪除的元素為:%d\n刪除後的順序表為:\n", c);
displist(l);
// listempty
if(listempty(l)) printf("\n空\n");
else printf("\n非空\n");
// locateelem()
printf("\n請輸入想要判斷其位序的元素:\n");
scanf("%d", &d);
printf("\n該元素的邏輯位序為:%d\n", locateelem(l, d));
if(!judgeelem(l, d)) printf("\n沒找到該元素\n");
// 銷毀
printf("\n準備銷毀的順序表為:\n");
displist(l);
destroylist(l);
printf("\n銷毀後的順序表為:\n");
displist(l);
return ok;
}
順序表的基本運算
問題及 標頭檔案 2015,煙台大學計算機與控制工程學院 完成日期 2015年9月7日 版本 v1.0 問題描述 測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。要輸出線性表,還要判斷表是否為空,這樣,實現判斷線性表是否為空的演算法l...
順序表的基本運算
順序表的基本運算 以順序表為例 1 目的是要測試 建立線性表 的演算法createlist,為檢視建表的結果,需要實現 輸出線性表 的演算法displist。在研習displist中發現,要輸出線性表,還要判斷表是否為空,這樣,實現判斷線性表是否為空的演算法listempty成為必要。這樣,再加上m...
順序表的基本運算
問題及 2015,煙台大學計算機與控制工程學院 完成日期 2015年9月14日 版本 v1.0 問題描述 運用插入資料元素,刪除資料元素,初始化線性表,銷毀線性表 include include define maxsize 50 maxsize將用於後面定義儲存空間的大小 typedef int ...