当前位置:首页 > 《数据结构实验指导书》
第一部分 上机实践
int ListLength_sq(SqList L) //求长度操作函数定义 { //代码略 }
Status GetElem_sq(SqList L, int i, LElemType &e) //取元素操作函数定义 { //代码略 }
Status ListInsert_sq(SqList &L, int i, LElemType e) //插入操作函数定义
{ //代码略 }
Status ListDelete_sq(SqList &L, int i, LElemType &e) //删除操作函数定义 { //代码略 }
int compareTo(LElemType e1,LElemType e2); //元素比较函数声明
int LocateElem_sq(SqList L, LElemType e) //查找操作,返回第一个与e相等的元素的位序 { int i;
for(i=0;i if(compareTo(L.Elem[i],e)==0) return i+1; return 0; } void visitListElem(LElemType e); //元素访问函数声明 void ListTraverse_sq(SqList L) //顺序表遍历操作 { int i; for(i=0;i<=L.Length-1; i++) visitListElem(L.Elem[i]); printf(\} void InputElem(LElemType &e); //输入元素e的函数声明 void CreatList_sq(SqList &L, int n) //顺序表创建操作 { int i; LElemType e; InitList_sq(L);; printf(\ for(i=0; i ListInsert_sq(L,i+1, e); } } (2)顺序表应用程序设计 在此以采用顺序表表示集合为例,设计程序实现集合并集、插入元素、删除元素等运算。同学还可自行设计其他顺序表的应用实例。 -3- 第一部分 上机实践 #include typedef int LElemType; //定义元素类型为int类型 #include \ int compareTo(LElemType e1,LElemType e2) //元素比较函数定义 { return e1-e2; } void visitListElem(LElemType e) //元素访问函数定义 { printf(\ \ } void InputElem(LElemType &e) //输入元素e函数定义 { scanf(\ } void SetUnion(SqList &La, SqList Lb) //集合并集运算 { //代码略 } void AddToSet(SqList &L, LElemType e) //将元素e插入集合中 { if(!LocateElem_sq(L,e)) ListInsert_sq(L,ListLength_sq(L)+1,e); } void DecFromSet(SqList &L, LElemType e) //在集合中删除元素e { int i; i=LocateElem_sq(L,e); if(i) ListDelete_sq(L,i,e); } int main() { SqList La,Lb; LElemType e; printf(\ //创建集合La printf(\ //创建集合Lb printf(\ printf(\ SetUnion(La,Lb); printf(\ printf(\ scanf(\ AddToSet(La,e); printf(\ -4- 第一部分 上机实践 printf(\ scanf(\ DecFromSet(Lb,e); printf(\ if(ListEmpty_sq(Lb)) printf(\ else printf(\ return 0; } (3)完整代码 -5- 第一部分 上机实践 -6-
共分享92篇相关文档