C语言顺序表的初始化
没学过C语言,将就着看着吧!
#include<stdio.h> #include<math.h> // #define 定义一个标识符来表示一个常量 #define OK 1 #define ERROR -1 #define MAX_SIZE 100 // typedef 关键字来定义自己习惯的数据类型名称 typedef int Status ; typedef int ElemType ; // struct 结构体 typedef struct sqlist{ // 定义一个数组 ElemType Elem_array[MAX_SIZE] ; // 数组的长度 int length ; } SqList ; // 初始化数组方法 // 参数:一个SqList 指针,指针只能是一个结构体的地址。 Status Init_SqList( SqList *L ) { // 动态分配空间 c语言中数组需要分配空间 L->Elem_array[MAX_SIZE]=( ElemType * )malloc(MAX_SIZE*sizeof( ElemType )) ; // 如果数组为空,则报错; if ( !L -> Elem_array ) return ERROR ; // 返回 ok,目前还不知道为什么要L->length= 0 ; 有大佬知道请评论; else { L->length= 0 ; return OK ; } }
下一篇:
工厂模式相关总结和使用