本链表的实现有问题,暂时留作纪念。 typedef char ElementType; typedef struct node { ElementType data; struct node *next; }ChainNode; typedef struct list { ChainNode *head; }LinerList; ChainNode *CreateNode(ElementType e); ChainNode *GetAddr(LinerList *lp,int i); LinerList *CreateList(void); void DestroyList(LinerList *); void ClearList(LinerList *); int ListAppend(LinerList *,ElementType); int ListInsert(LinerList *,int,ElementType); int ListDelete(LinerList *,int); int GetElement(LinerList *,int,ElementType *); void Show(LinerList *); ChainNode *CreateNode(ElementType e) { ChainNode *newp; newp=(ChainNode *)malloc(sizeof(ChainNode)); newp->data=e; newp->next=0; } ChainNode *GetAddr(LinerList *lp,int i) { ChainNode *p; int j; p=lp->head; j=0; while(j<i&&p) { p->next++; j++; } return p; } LinerList *CreateList(void) { LinerList *lp; lp=(LinerList *)malloc(sizeof(LinerList)); if(!lp) { return 0; } lp->head=(ChainNode *)malloc(sizeof(ChainNode)); if(!lp->head) { free(lp); return 0; } lp->head->data=0; lp->head->next=0; return lp; } void DestroyList(LinerList *lp) { ClearList(lp); free(lp->head); free(lp); } void ClearList(LinerList *lp) { while(ListDelete(lp,1)); } int ListAppend(LinerList *lp,ElementType e) { ChainNode *newp; ChainNode *p; newp=CreateNode(e); if(!newp) { return 0; } p=lp->head; while(p->next) { p=p->next; } p->next=newp; return 1; } int ListInsert(LinerList *lp,int i,ElementType e) { ChainNode *p; ChainNode *newp; if(i<1) { return 0; } p=GetAddr(lp,i-1); if(!p) { return 0; } newp=CreateNode(e); if(!newp) { return 0; } newp->next=p->next; p->next=newp; return 1; } int ListDelete(LinerList *lp,int i) { ChainNode *p; ChainNode *newp; if(i<1) { return 0; } p=GetAddr(lp,i-1); if(!p) { return 0; } newp=p->next; p->next=p->next->next; free(newp); return 1; } int GetElement(LinerList *lp,int i,ElementType *e) { ChainNode *p; if(i<1) { return 0; } p=GetAddr(lp,i); if(!p) { return 0; } *e=p->data; return 1; } void Show(LinerList *lp) { ChainNode *p; for(p=lp->head->next;p;p=p->next) { printf("%c ",p->data); } printf("/n"); } /*以下为测试程序*/ main() { LinerList *lp; lp=CreateList(); DestroyList(lp); }