输出顺序表中所有元素

题目

输出顺序表中所有元素。

#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版》严蔚敏 李冬梅 吴伟民

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值