第三周项目一 (1)

xiaoxiao2021-02-28  5

/* 烟台大学计算机学院 文件名称:xiangmu.cpp 作者:宋昊 完成日期:2017年9月17日 问题描述:顺序表建立 输入描述:无 输出描述:顺序表的值 */ #include <stdio.h> #include <malloc.h> #define MaxSize 50//存储空间大小宏定义 typedef int ElemType; //定义ElemType为int typedef struct { ElemType data[MaxSize]; //利用了前面MaxSize和ElemType的定义 int length; } SqList; void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表 void DispList(SqList *L);//输出线性表DispList(L) bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L) int main()//主函数 { SqList *p; ElemType x[6]={1,2,3,4,5,6}; CreateList(p,x,6); DispList(p); return 0; } void CreateList(SqList *&L, ElemType a[], int n) { int i; L=(SqList *)malloc(sizeof(SqList)); for (i=0; i<n; i++) L->data[i]=a[i]; L->length=n; }//创建线性表 void DispList(SqList *L) { int i; if (ListEmpty(L)) return; for (i=0; i<L->length; i++) printf("%d ",L->data[i]); printf("\n"); }//输出线性表 bool ListEmpty(SqList *L) { return(L->length==0); }//空表判断

运行结果:

学习心得:学习到了用顺序表的基本算法实现程序

转载请注明原文地址: https://www.6miu.com/read-1150407.html

最新回复(0)