- /*
- *Copyright © 2017, 烟台大学计算机学院
- *All rights reserved.
- *文件名称:shiyan.cpp
- *作 者:张俊杰
- *完成日期:2017年9月21日
- *版本号:v1.0
- *
- *问题描述:创建一个线性表并且初始化,查找,插入,删除,输出元素
- *输入描述:
- *程序输出:见截图
- */
- #include <iostream>
- #include <malloc.h>
- #include <cstdio>
- #include "list.h"
- using namespace std;
- int main()
- {
- SqList *sq;
- ElemType x[6]= {5,8,7,2,4,9};
- ElemType a;
- int loc;
- CreateList(sq, x, 6);
- DispList(sq);
- cout<<endl;
- printf("线性表长度为:%d\n", ListLength(sq)); //测试求长度
- cout<<endl;
- if(GetElem(sq, 3, a)) //测试在范围内的情形
- printf("第3个元素值为:%d\n", a);
- else
- printf("第3个元素超出范围!\n");
- cout<<endl;
- if(GetElem(sq, 15, a)) //测试不在范围内的情形
- printf("第15个元素值为:%d\n", a);
- else
- printf("第15个元素超出范围!\n");
- cout<<endl;
- if((loc=LocateElem(sq, 8))>0) //测试能找到的情形
- printf("值为8的元素是第 %d 个元素\n", loc);
- else
- printf("没有值为8的元素!\n");
- cout<<endl;
- if((loc=LocateElem(sq, 17))>0) //测试不能找到的情形
- printf("值为17的元素是第 %d 个元素\n", loc);
- else
- printf("没有值为17的元素!\n");
- cout<<endl;
- printf("插入值为10的元素!\n");
- ListInsert(sq ,7,10);
- DispList(sq);
- cout<<endl;
- printf("删除第二个元素!\n");
- Deletelist(sq,2);
- DispList(sq);
- printf("销毁线性表\n");
- DestroyList(sq);
- cout<<endl;
- return 0;
- }
运行结果