资源描述:
《大数据结构六个主要算法程序(c版).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1.顺序表#include#includetypedefstruct{intnum;charname[20];}Sqstu;Sqstustu[4];Sqstustud;voidcreat();voidinsert();voiddel();voidmain(){creat();printf("请输入要插入学生的信息:");insert();printf("请输入要删除学生的学号:");del();}voidcreat(){inti;for(i=0;i<3;i++)scanf("
2、%d%s",&stu[i].num,stu[i].name);printf("学生的信息为:");for(i=0;i<3;i++)printf("%-10d%s",stu[i].num,stu[i].name);}voidinsert(){inti,j;scanf("%d%s",&stud.num,stud.name);for(i=0;stud.num>stu[i].num;i++)j=i;for(i=3;i>j;i--){stu[i].num=stu[i-1].num;strcpy(stu[i].name,st
3、u[i-1].name);}stu[j+1].num=stud.num;strcpy(stu[j+1].name,stud.name);printf("现在,学生的信息为:");for(i=0;i<4;i++)printf("%-10d%s",stu[i].num,stu[i].name);}voiddel(){inti;scanf("%d",&stud.num);for(i=0;stud.num!=stu[i].num;i++)i=i;for(i;i<4;i++){stu[i]=stu[i+1];}printf
4、("现在,学生的信息为:");for(i=0;i<3;i++)printf("%-10d%s",stu[i].num,stu[i].name);}2.链表#include#include#defineNULL0#defineLENsizeof(structstudent)structstudent{intnum;charname[20];structstudent*next;};intn;structstudent*creat(){structstudent*head;str
5、uctstudent*p1,*p2;n=0;p1=p2=(structstudent*)malloc(LEN);scanf("%d%s",&p1->num,p1->name);head=NULL;while(p1->num!=0){n=n+1;if(n==1)head=p1;elsep2->next=p1;p2=p1;p1=(structstudent*)malloc(LEN);scanf("%d%s",&p1->num,p1->name);}p2->next=NULL;returnhead;}voidprint(str
6、uctstudent*head){structstudent*p;printf("Now,These%drecordsare:",n);p=head;if(head!=NULL)do{printf("%d,%s",p->num,p->name);p=p->next;}while(p!=NULL);}structstudent*insert(structstudent*head,structstudent*stud){structstudent*p0,*p1,*p2;p1=head;p0=stud;if(hea
7、d==NULL){head=p0;p0->next=NULL;}else{while((p0->num>p1->num)&&(p1->next!=NULL)){p2=p1;p1=p1->next;}if(p0->num<=p1->num){if(head==p1)head=p0;elsep2->next=p0;p0->next=p1;}else{p1->next=p0;p0->next=NULL;}}n=n+1;returnhead;}structstudent*del(structstudent*head,intnum
8、){structstudent*p1,*p2;p1=head;if(head==NULL){printf("listnull!");returnhead;}while((num!=p1->num)&&(p1->next!=NULL)){p2=p1;p1=p1->next;}if(num==p1->num){i