编写函数生成含有N个节点的,带头结点单向动态链表,并输出链表。

编写函数生成含有N个节点的,带头结点单向动态链表,并输出链表。 有3个函数,一个链表生成函数,一个链表读取函数,一个主函数。

第1个回答  2014-06-25
我把我的宝贝给你 单向动态链表的创建、输入、插入、删除、输出 #include <stdio.h> #include <malloc.h>/*如以Turbo C作编译工具请删除此句*/ #define LEN sizeof(struct student) struct student { long num; char name[20]; float score; struct student *next; }; int n; struct student *creat(void) { struct student *head; struct student *pa,*pb; n=0; pa=pb=(struct student *)malloc(LEN); scanf("%ld %s %f",&pa->num,pa->name,&pa->score); head=NULL; if(pa->num==0) return(head); while((pa->num!=0)&&(pa->next!=NULL)) { n=n+1; if(n==1) head=pa; else pb->next=pa; pb=pa; pa=(struct student *)malloc(LEN); scanf("%ld %s %f",&pa->num,pa->name,&pa->score); } pb->next=NULL; return(head); } struct student *del(struct student *head,long num) { struct student *pa,*pb; pa=head; if(head==NULL) return(head); while((pa->num!=num)&&(pa->next!=NULL)) { pb=pa; pa=pa->next; } if(pa->num==num) { if(pa==head) head=pa->next; else if(pa->next!=NULL) pb->next=pa->next; else pb->next=NULL; n=n-1; } else printf("%ld not been found!\n",num); return(head); } struct student *insert(struct student *head,struct student *stu) { struct student *pa,*pb,*pc; pa=head; pc=stu; if(head==NULL) return(head); while((pc->num>pa->num)&&(pa->next!=NULL)) { pb=pa; pa=pa->next; } if(pc->num<=pa->num) { if(pa==head) head=pc; else pb->next=pc; pc->next=pa; n=n+1; } else { pa->next=pc; pc->next=NULL; } return(head); } main() { struct student *creat(void); struct student *del(struct student *,long); struct student *insert(struct student *,struct student *); struct student *stu,*creat_head,*del_head,*insert_head; long data; printf("Please input coverds:\n"); creat_head=creat(); printf("Please input will delete number:"); scanf("%ld",&data); del_head=del(creat_head,data); if(del_head==NULL) printf("Delete faild, List null or %ld not been found.\n",data); else { printf("Delete successd, %ld been delete.\n",data); printf("There are %d coverds.\n",n); while(del_head->num!=0) { printf("%ld, %s, %g\n",del_head->num,del_head->name,del_head->score); if(del_head->next!=NULL) del_head=del_head->next; else break; } stu=(struct student *)malloc(LEN); scanf("%ld %s %f",&stu->num,stu->name,&stu->score); while(stu->num!=0) { insert_head=insert(creat_head,stu); while(insert_head->num!=0) { printf("%ld %s %g\n",insert_head->num,insert_head->name,insert_head->score); if(insert_head->next!=NULL) insert_head=insert_head->next; else break; } stu=(struct student *)malloc(LEN); scanf("%ld %s %f",&stu->num,stu->name,&stu->score); } stu->next=NULL; } return 0; }

记得采纳啊本回答被提问者采纳
相似回答