顺序表的创建算法

时间:2022-10-29 11:17:09

顺序表的创建算法

实现代码:

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef int ElemType;
typedef struct SqList{
	ElemType data[MaxSize];
	int length;
}SqList;
void Init(SqList *&L){
	int i;
	for(i=0;i<L->length;++i){
		printf("%d ",L->data[i]);
	}
}
void CreateList(SqList *&L,ElemType a[],int n){
	int i=0,k=0;
	L=(SqList*)malloc(sizeof(SqList));
	while(i<n){
		L->data[i]=a[i];
		k++;
		i++;
	}
	L->length=k;
}
void Show(SqList *&L){
	int i;
	for(i=0;i<L->length;++i){
		printf("%d ",L->data[i]);
	}
}
int main(int argc, char *argv[])
{
	int a[6]={3,4,6,7,8,9};
	struct SqList *L;
	CreateList(L,a,6);
	Show(L);
	return 0;
}
输出结果:

3 4 6 7 8 9 请按任意键继续. . .