网站大量收购独家精品文档,联系QQ:2885784924

c语言单链表.doc

  1. 1、本文档共3页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
c语言单链表

1.创建单链表算法描述 (1)定义head (2)令p1指向新生成结点 if(n==1)head=p1;else p2-next=p1; (3)为结构体成员赋值 (4)令p2指向链尾 重复(2)~(4) (5)if(n==0)p2-next=NULL; 即:0为输入结束标志 2.删除结点算法(例如删除2号结点) (1)查询到2号结点 (2)用p2指向2号前一结点,p1指向2号结点 (3)p2-next=p1-next 3.插入结点算法(例如重新插入2号结点) (1)生成2号结点; (2)找到正确插入位置,p2指向1号,p1指向3号 (3)p2-next=p;p-next=p1; 动态链表举例 #include stdio.h #include alloc.h #define LEN sizeof(Node) typedef struct student { int num; char name[16]; char sex; int age; struct student *next; }Node,*pNode; void main() { pNode create(); void print(pNode head); pNode del(pNode head,int del_num); pNode insert(pNode head,pNode p); int num,del_num; char c; pNode head,p; head=create(); print(head); printf(please input No. to del:); scanf(%d,del_num); head=del(head,del_num); print(head); printf(insert Node:\n); p=(pNode)malloc(LEN); printf(input No. :);scanf(%d,p-num); printf(input Name:);scanf(%s,p-name); printf(input sex:); while(c=getchar(),c!=mc!=f); p-sex=c; printf(input Age:);scanf(%d,p-age); head=insert(head,p); print(head);} pNode create() { pNode head,p1,p2; int num,n=1; head=NULL; printf(input No. :); scanf(%d,num); while(num) { char c; p1=(pNode)malloc(LEN); p1-num=num; if(n==1){ head=p1;n=0; } else p2-next=p1; printf(input Name:); scanf(%s,p1-name); printf(input sex:); while(c=getchar(),c!=mc!=f); p1-sex=c; printf(input Age:); scanf(%d,p1-age); p2=p1; printf(input No. :); scanf(%d,num); } if(n==0) p2-next=NULL; return(head);} void print(pNode head) { pNode p; printf(\nNo. NAME SEX AGE\n); p=head; while(p!=NULL) { printf(%-5d%-16s,p-num,p-name); printf(%-3c%6d\n,p-sex,p-age); p=p-next; } } pNode del(pNode head,int del_num) { pNode p1,p2; if(head!=NULL) { p1=head; while(del_num!=p1-nump1-next!=NULL) { p2=p1;p1=p1-next; } if(p1-num==del_num) { if(p

文档评论(0)

yan698698 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档