C语言中的指针与结构体
多文件结构 工作区 工程1 工程2 工程n 头文件 源文件 …… …… …… 例:VC中的多文件结构 示例 #include #include #include #include //常量定义 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define True 1 #define False 0 //函数返回值类型定义 typedef int Status; //表节点数据类型定义 typedef int ElemType; //顺序表类型定义 typedef struct{ ElemType *elem; int length; int listsize; } SqList; //顺序表各操作声明 Status InitList_Sq(SqList &L); Status DetroyList_Sq(SqList &L); Status ClearList_Sq(SqList &L); int ListEmpty_Sq(SqList L); int ListLength_Sq(SqList L); Status GetElem_Sq(SqList L,int i,ElemType &e); Status ListInsert_Sq(SqList &L,int i,ElemType e); Status ListDelete_Sq(SqList &L,int i,ElemType &e); void PrintList_Sq(SqList L); 头文件:SqList.h #include "SqList.h" //各操作的实现 Status InitList_Sq(SqList &L) { L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!L.elem) exit(OVERFLOW); L.length=0; L.listsize=LIST_INIT_SIZE; return OK; } Status DetroyList_Sq(SqList &L) {if (L.elem) free(L.elem); return OK; } Status ClearList_Sq(SqList &L) {if (L.elem) {L.length=0; L.listsize=0; } return OK; } 源文件:SqList.cpp int ListEmpty_Sq(SqList L) { return (L.length==0); } int ListLength_Sq(SqList L) { return L.length; } Status GetElem_Sq(SqList L,int i,ElemType &e) { if (i<1 || i>=L.length) return ERROR; e=L.elem[i-1]; return OK; } Status ListInsert_Sq(SqList &L,int i,ElemType e) { ElemType *newbase,*p,*q; if (i<1 || i>L.length+1) return ERROR; if (L.length>=L.listsize) { newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if (!newbase) exit(OVERFLOW); L.elem=newbase; L.listsize+=LISTINCREMENT; } q=&(L.elem[i-1]); for (p=&L.elem[L.length-1];p>=q;p--) *(p+1)=*p; *q=e; L.length++; return OK; } Status ListDelete_Sq(SqList &L,int i,ElemType &e) { ElemType *p,*q; if (i<1 || i>L.length) return ERROR; p=&(L.elem[i-1]); e=*p;