欢迎来到天天文库
浏览记录
ID:1745505
大小:36.50 KB
页数:8页
时间:2017-11-13
《数据结构(单链表)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、//头文件list.h#include"stdlib.h"#include"stdio.h"typedefcharEleType;typedefstructnode{EleTypedata;structnode*next;}ChainNode;typedefstruct{ChainNode*head;}List;List*CreatList();ChainNode*NewChainNode(EleTypedata);List*NodeAppend(List*lp,EleTypedata);ChainNode*GetAddr(List*lp,int
2、n);List*NodeDelete(List*lp,intn);List*NodeAdd(List*lp,intn,EleTypedata);List*CreatList(){List*lp;ChainNode*p;EleTypedata=0;lp=(List*)malloc(sizeof(List));if(!lp)return0;p=NewChainNode(data);if(!p)return0;lp->head=p;returnlp;}ChainNode*NewChainNode(EleTypedata){ChainNode*p;p=(
3、ChainNode*)malloc(sizeof(ChainNode));if(!p)return0;p->data=data;p->next=0;returnp;}List*NodeAppend(List*lp,EleTypedata){ChainNode*p;ChainNode*p1;p1=NewChainNode(data);if(!p1)return0;p=lp->head;for(p;p->next;p=p->next);p->next=p1;returnlp;}ChainNode*GetAddr(List*lp,intn){Chain
4、Node*p;if(n<0){printf("n<0");return0;}p=lp->head;inta;for(a=0;anext;}returnp;}List*NodeDelete(List*lp,intn){ChainNode*p;ChainNode*p1;p=GetAddr(lp,n-1);if(!p)return0;p1=p->next;p->next=p1->next;free(p1);returnlp;}List*NodeAdd(List*lp,intn,EleTypedata){ChainNo
5、de*p;ChainNode*p1;p1=NewChainNode(data);if(!p1)return0;p=GetAddr(lp,n-1);if(!p)return0;p1->next=p->next;p->next=p1;returnlp;}//测试函数#include"list.h"EleTypearr[]="Helloworld!";voidshowlist(List*lp);voidmain(){inti;List*lp;lp=CreatList();if(!lp)printf("CreatListfailed!");for(i=0
6、;arr[i];i++){NodeAppend(lp,arr[i]);}showlist(lp);NodeDelete(lp,1);showlist(lp);NodeAdd(lp,1,'H');showlist(lp);}voidshowlist(List*lp){ChainNode*p;p=lp->head;for(p=p->next;p;p=p->next)printf("%c",p->data);printf("");}
此文档下载收益归作者所有