数据结构程序代码.docx
- 文档编号:15873372
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:145
- 大小:55.38KB
数据结构程序代码.docx
《数据结构程序代码.docx》由会员分享,可在线阅读,更多相关《数据结构程序代码.docx(145页珍藏版)》请在冰点文库上搜索。
数据结构程序代码
第二章链表
一.单链表的建立,输出,插入和删除
#include"stdlib.h"
structnode
{intdata;
structnode*next;
};
structnode*create()/*用头插法创建一个链表*/
{intx;
structnode*head,*p;
head=(structnode*)malloc(sizeof(structnode));/*建立头结点*/
head->next=NULL;
scanf("%d",&x);
while(x!
=-999)
{
p=(structnode*)malloc(sizeof(structnode));
p->data=x;
p->next=head->next;/*将p放在头结点之后*/
head->next=p;
scanf("%d",&x);
}
return(head);
}
voidoutline(structnode*h)/*输出一个链表*/
{
structnode*p;
p=h->next;
printf("\n");
while(p!
=NULL)
{printf("%5d",p->data);
p=p->next;
}
}
voidinsert(structnode*h,intx,inty)
/*将y插到x后面*/
{structnode*p,*s;
p=h->next;
while(p->data!
=x&&p!
=NULL)
p=p->next;
s=(structnode*)malloc(sizeof(structnode));
s->data=y;
s->next=p->next;
p->next=s;
}
voiddelete(structnode*h,intx)/*删除链表中数据为x的结点*/
{structnode*p,*s;
p=h->next;
while(p->data!
=x&&p!
=NULL)
p=p->next;
s=p->next;
p->next=s->next;
free(s);
}
main()
{inta,b;
structnode*h;
printf("inputa,b:
");
scanf("%d%d",&a,&b);
h=create();
outline(h);
insert(h,a,b);
outline(h);
delete(h,a);
outline(h);
}
二、数组的插入和删除
#include"stdio.h"
#include"stdlib.h"
#definen100
voidinsert(inta[],inti,inty,intm)
{intj;
for(j=m;j>i;j--)
a[j]=a[j-1];
a[i]=y;
m++;
}
voiddelate(inta[],inti,intm)
{intj;
for(j=i;j a[j]=a[j+1]; } main() {inta[n]; intm=1; intx,y,b,i=0,k; printf("inputx: "); scanf("%d",&x); while(x! =-999) {a[i]=x; i++;m++; scanf("%d",&x); } i=0; printf("inputthevalueofinsert"); scanf("%d%d",&k,&y); insert(a,k,y,m); for(i=0;i printf("%d",a[i]); printf("inputthevalueofdelete"); scanf("%d",&i); delate(a,i,m); for(i=0;i printf("%d",a[i]); } 三、对无序链表排序 #include #include structnode { intdata; structnode*next; }; voidcreate(structnode*head) /*尾插法创建链表*/ { structnode*p,*r=head; intx; scanf("%d",&x); while(x! =-999) { p=(structnode*)malloc(sizeof(structnode)); p->data=x; p->next=NULL; r->next=p; r=p; scanf("%d",&x); } } voidoutline(structnode*head) /*输出链表*/ { structnode*p=head->next; printf("\n"); while(p! =NULL) { printf("%5d",p->data); p=p->next; } } voidarrange(structnode*head) /*对无序链表排序*/ { structnode*q,*r,*p,*u; p=head->next; head->next=NULL; while(p! =NULL) { r=head; q=p->next; while(q! =NULL&&q->data<=p->data) { r=q; q=q->next; } u=p->next; p->next=r->next; r->next=p; p=u; } } main() { structnode*head; head=(structnode*)malloc(sizeof(structnode)); head->next=NULL; create(head); outline(head); printf("outputarrange: "); arrange(head); outline(head); }_ 四、将两个有序单链表合并,并保持它的有序性. #include #include structnode { intdata; structnode*next; }; voidcreate(structnode*head) /*尾插法创建链表*/ { structnode*p,*r=head; intx; scanf("%d",&x); while(x! =-999) { p=(structnode*)malloc(sizeof(structnode)); p->data=x; p->next=NULL; r->next=p; r=p; scanf("%d",&x); } } voidoutline(structnode*head) /*链表的输出*/ { structnode*p=head->next; printf("\n"); while(p! =NULL) { printf("%5d",p->data); p=p->next; } } voidmergelist(structnode*head1,structnode*head2) /*将两个有序链表合并*/ { structnode*p,*q,*r; p=head1->next; q=head2->next; r=head1; while(p! =NULL&&q! =NULL) { if(p->data<=q->data) { r->next=p; r=p; p=p->next; } else { r->next=q; r=q; q=q->next; } } if(q==NULL) r->next=p; else r->next=q; } main() { structnode*head1,*head2; head1=(structnode*)malloc(sizeof(structnode)); head2=(structnode*)malloc(sizeof(structnode)); head1->next=NULL; head2->next=NULL; create(head1); create(head2); printf("outputarrange: "); mergelist(head1,head2); outline(head1); }_ 五、 (1)两个一元多项式相加 #include #include structnode { intcoef,exp; structnode*next; }; voidcreat(structnode*head) /*头插法创建链表*/ { structnode*p; inti,j; printf("inputi,j: "); scanf("%d,%d",&i,&j); while(i! =-999&&j! =-999) { p=(structnode*)malloc(sizeof(structnode)); p->coef=i; p->exp=j; p->next=head->next; head->next=p; scanf("%d,%d",&i,&j); } } voidoutline(structnode*head) /*链表的输出*/ { structnode*p=head->next; while(p! =NULL) { printf("%d,%-5d",p->coef,p->exp); p=p->next; } } voidpadd(structnode*head1,structnode*head2) /*两个一元多项式相加*/ { structnode*p=head1->next,*q=head2->next,*r; if(p->exp>=q->exp) { r=p; p=p->next; free(head2); } else { r=q; q=q->next; free(head1); } while(p! =NULL&&q! =NULL) { if(p->exp==q->exp) { p->coef=p->coef+q->coef; if(p->coef! =0) { r=p; p=p->next; q=q->next; } } elseif(p->exp>q->exp) { r->next=p; r=p; p=p->next; } else { r->next=q; r=q; q=q->next; } } if(p==NULL) r->next=q; else r->next=p; } main() { structnode*head1,*head2; head1=(structnode*)malloc(sizeof(structnode)); head2=(structnode*)malloc(sizeof(structnode)); head1->next=NULL; head2->next=NULL; creat(head1); creat(head2); if(head1->next->exp>=head2->next->exp) { padd(head1,head2); printf("\noutput: "); outline(head1); } else { padd(head1,head2); printf("\noutput: "); outline(head2); } }_ (2)计算两个一元多项式之和放到第三个链表 #include"stdlib.h" structnode { intcoef,exp; structnode*next; }; structnode*create() /*用头插法建立双链表*/ { intx,y; structnode*head,*p; head=(structnode*)malloc(sizeof(structnode)); /*建立头结点*/ head->next=NULL; scanf("%d%d",&x,&y); while(x! =-999) { p=(structnode*)malloc(sizeof(structnode)); /*建立新结点*/ p->coef=x;p->exp=y; p->next=head->next; head->next=p; scanf("%d%d",&x,&y); } return(head); } voidoutline(structnode*h)/*输出链表h*/ { structnode*p; p=h->next; printf("\n"); while(p! =NULL) { printf("%d%d**",p->coef,p->exp); p=p->next; } } structnode*linksum(structnode*h1,structnode*h2) /*求两链表之和*/ {structnode*p,*q,*r,*h,*s;intx,y; h=(structnode*)malloc(sizeof(structnode)); r=h;p=h1->next;q=h2->next; while(p! =NULL&&q! =NULL) {if(p->exp==q->exp) /*当x的幂相同时将其系数相加*/ {x=p->coef+q->coef;y=p->exp;p=p->next;q=q->next;} else if(p->exp /*当x的幂不同时将其直接放到第三个链表里*/ {x=q->coef;y=q->exp;q=q->next;} else {x=p->coef;y=p->exp;p=p->next;} s=(structnode*)malloc(sizeof(structnode)); s->coef=x;s->exp=y;r->next=s;r=s;printf("%d%d",x,y); } if(p==NULL) r->next=q; if(q==NULL) r->next=p; return(h); } main() { structnode*h1,*h2,*h; h1=create(); outline(h1); h2=create(); outline(h2); h=linksum(h1,h2); outline(h); }_ (3)将两个链表相加放入第三个链表中 #include"stdlib.h" structnode {intdata; structnode*next; }; structnode*create()/*创建链表*/ {intx; structnode*head,*p; head=(structnode*)malloc(sizeof(structnode)); head->next=NULL; scanf("%d",&x); while(x! =-999) { p=(structnode*)malloc(sizeof(structnode)); p->data=x; p->next=head->next; head->next=p; scanf("%d",&x); } return(head); } voidoutline(structnode*h) /*输出链表*/ { structnode*p; p=h->next; printf("\n"); while(p! =NULL) {printf("%5d",p->data); p=p->next; } } structnode*mul(structnode*h1,structnode*h2) /*将两个链表相加*/ { structnode*h,*p,*q,*r,*s; intx; h=(structnode*)malloc(sizeof(structnode)); r=h;p=h1->next;q=h2->next; while(p! =NULL&&q! =NULL) { x=p->data+q->data; p=p->next;q=q->next;r=r->next; s=(structnode*)malloc(sizeof(structnode));s->data=x;s->next=r->next;r->next=s; } if(p==NULL) r->next=q; if(q==NULL) r->next=p; return(h); } main() { structnode*h1,*h2,*h; h1=create(); outline(h1); h2=create(); outline(h2); h=mul(h1,h2); outline(h); } 六、循环双链表的建立,输出和插入 #include"stdlib.h" #include"stdio.h" structDnode { intdata; structDnode*next; structDnode*prior; }; structDnode*create() /*用尾插法建立循环双链表*/ {intx; structDnode*p,*head,*s; head=(structDnode*)malloc(sizeof(structDnode)); /*建立头结点*/ head->prior=head->next=NULL; p=head; scanf("%d",&x); while(x! =-999) { s=(structDnode*)malloc(sizeof(structDnode)); /*建立新结点*/ s->data=x; p->next=s; s->prior=p; p=s; scanf("%d",&x); } p->next=head; head->prior=p; return(head); } structDnode*insert(structDnode*ha,structDnode*hb,inti) { structDnode*p=ha->next,*q=hb->next,*j=ha->prior,*k=hb->prior; intm,n; m=0;n=0; while(p! =ha)、 /*计算ha的长度*/ { m++; p=p->next; } if(i==0) /*当输入的i=0时,将hb插到ha的最前面*/ { p=ha->next; ha->next=q; q->prior=ha; k->next=p; p->prior=k; } elseif(i>0&&i /*当i>0&&i { while(n {n++; p=p->next; } p->next->prior=k; k->next=p->next; q->prior=p;p->next=q; } elseif(i>=m) /*当i>m||i=m时,将hb连到ha的最后面*/ { j->next=q; q->prior=j; k->next=ha; ha->prior=k; } return(ha); } voidoutline(structDnode*h)/*输出链表h*/ { structDnode*p; p=h->next; printf("\n"); while(p! =h) {printf("%5d",p->data); p=p->next; } } voidmain() { structDnode*ha,*hb,*h; inti; printf("inputi"); scanf("%d",&i); ha=create(); outline(ha); hb=create(); outline(hb); h=insert(ha,hb,i); outline(h); } 七、将两个有循环单链表合并,并保持有序性 #include structnode { intdata; structnode*next; }; voidcreate1(structnode*head) /*头插法创建链表*/ { intx; structnode*p; printf("inputx: "); scanf("%d",&x); while(x! =-999) { p=(structnode*)malloc(sizeof(structnode)); p->data=x; p->next=head->next; head->next=p; scanf("%d",&x); } } voidoutline1(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序代码