题目
输出顺序表中所有元素。
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
#define MAXSIZE 100 //顺序表可能达到的最大长度
typedef struct {
ElemType *elem; //存储空间的基地址
int length; //当前长度
} SqList;
Status InitList(SqList &L) { //初始化创建顺序表
L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
if (!L.elem)
exit(OVERFLOW); //存储分配失败退出
cout<<"初始化表中元素个数:";
int n;
cin>>n;
cout<<"creat SqList:"; //在初始化中创建表
for(int i = 0; i < n; i++){
cin>>L.elem[i];
}
L.length = n; //空表长度为0
return OK;
}
void input(SqList L){//输出表
ElemType i = 0;
while(L.length != i){
cout<<L.elem[i]<<" ";
i++;
}
cout<<endl;
}
int main()
{
SqList a;
InitList(a);
input(a);//输出顺序表
return OK;
}
参考资料:
《数据结构 C语言版 第2版》严蔚敏 李冬梅 吴伟民