1、/头文件 list.h#include“stdlib.h“#include“stdio.h“typedef char EleType;typedef struct nodeEleType data;struct node * next;ChainNode;typedef structChainNode * head;List;List * CreatList();ChainNode * NewChainNode(EleType data);List * NodeAppend(List * lp,EleType data);ChainNode * GetAddr(List * lp,int n)
2、;List * NodeDelete(List * lp,int n);List * NodeAdd(List * lp,int n,EleType data);List * CreatList()List * lp;ChainNode * p;EleType data=0;lp=(List *)malloc(sizeof(List);if(!lp) return 0;p=NewChainNode(data);if(!p) return 0;lp-head=p;return lp;ChainNode * NewChainNode(EleType data)ChainNode * p;p=(Ch
3、ainNode *)malloc(sizeof(ChainNode);if(!p) return 0;p-data=data;p-next=0;return p;List * NodeAppend(List * lp,EleType data)ChainNode * p;ChainNode * p1;p1=NewChainNode(data);if(!p1) return 0;p=lp-head;for(p;p-next;p=p-next);p-next=p1;return lp;ChainNode * GetAddr(List * lp,int n)ChainNode * p;if(nhea
4、d;int a;for(a=0;anext;return p;List * NodeDelete(List * lp,int n)ChainNode * p;ChainNode * p1;p=GetAddr(lp,n-1);if(!p) return 0;p1=p-next;p-next=p1-next;free(p1);return lp;List * NodeAdd(List * lp,int n,EleType data)ChainNode * p;ChainNode * p1;p1=NewChainNode(data);if(!p1) return 0;p=GetAddr(lp,n-1
5、);if(!p) return 0;p1-next=p-next;p-next=p1;return lp;/测试函数#include“list.h“EleType arr=“Hello world!“;void showlist(List * lp);void main()int i;List * lp;lp=CreatList();if(!lp) printf(“CreatList failed!“);for(i=0;arri;i+)NodeAppend(lp,arri);showlist(lp);NodeDelete(lp,1);showlist(lp);NodeAdd(lp,1,H);showlist(lp);void showlist(List * lp)ChainNode * p;p=lp-head;for(p=p-next;p;p=p-next)printf(“%c“,p-data);printf(“n“);