數據結構學習筆記 算法2.3的具體實現

#define TRUE      1
#define FALSE     0
#define OK        1
#define ERROR     0
#define INFEASIBLE  -1
#define OVERFLOW    -2
#define LIST_INIT_SIZE 100//線性表存儲空間的初始分配量 
#define LISTINCREMENT 10 //線性表存儲空間的分配增量
typedef int ElemType;
typedef struct{
	 ElemType *elem;
	 int length;
	 int listsize; 
}SqList;

int InitList_Sq(SqList *L)
{
	//L爲指向結構體類型數據的指針變量,所以要用 L->elem 或 (*L).elem 
	(*L).elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if(!L->elem) exit(OVERFLOW);
	L->length=0;
	L->listsize = LIST_INIT_SIZE;
	return OK;
}


#include <stdio.h>
int main(int argc, char *argv[])
{
	SqList L = {NULL};//必須初始化,因爲含有指針變量 
	printf("初始化前listsize = %d\n",L.listsize);
	InitList_Sq(&L);
	printf("初始化後listsize = %d\n",L.listsize);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章