资源描述:
《数据结构有序表的归并》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1.有序表的归并#include#includetypedefstructList{intdate;structList*next;}List;List*head1,*head2,*head3;intIsEmpty(List*p){p=p->next;if(p==NULL)return0;elsereturn1;//判断是否为空,是的话返回0;}intIsLast(List*p){if(p==NULL)return0;elsereturn1;//判断链表是否达到最后
2、一个,是的话返回0}List*Insert(List*head,intn){List*p,*q;p=(List*)malloc(sizeof(List));p->date=n;p->next=NULL;q=head;while(q->next!=NULL)q=q->next;q->next=p;returnhead;//传入一个date值,将这个值插入到一head为头的链表中,返回head}List*MakeList(List*head,FILE*fp){intlength;fscanf(fp,"%d"
3、,&length);intdat;List*p=NULL;List*q=NULL;q=head;for(inti=0;idate=dat;q->next=p;p->next=NULL;q=p;}returnhead;//创建以head为头,长度为length的链表}voidShowList(List*head,FILE*fp){intflag=1;List*p;p=hea
4、d;while(flag){fprintf(fp,"%d",p->date);p=p->next;flag=IsLast(p);}}intmain(){//以下是实验要求的实现intflag1,flag2;List*p,*q;FILE*fp;fp=fopen("input.txt","r");head1=(List*)malloc(sizeof(List));head2=(List*)malloc(sizeof(List));head3=(List*)malloc(sizeof(List));head1
5、->date=0;head1->next=NULL;head2->date=0;head2->next=NULL;head3->date=0;head3->next=NULL;head1=MakeList(head1,fp);head2=MakeList(head2,fp);fclose(fp);fp=fopen("output.txt","w");p=head1->next;q=head2->next;flag1=IsLast(p);flag2=IsLast(q);while(flag1!=0&&fl
6、ag2!=0){if(p->datedate){head3=Insert(head3,p->date);p=p->next;flag1=IsLast(p);}else{head3=Insert(head3,q->date);q=q->next;flag2=IsLast(q);}}if(flag1!=0){while(flag1){head3=Insert(head3,p->date);p=p->next;flag1=IsLast(p);}}else{while(flag2){head3=Inse
7、rt(head3,q->date);q=q->next;flag2=IsLast(q);}}ShowList(head3->next,fp);fclose(fp);}1.数据转换(用栈实现将十进制转换为二进制)#include#includetypedefstructstack{intdate;structstack*next;}stack;intfulength=100,length=0;stack*top=NULL;intIsFull(intlength);st
8、ack*Create();stack*Push(stack*top,stack*p);stack*Pop(stack*top);intIsEmpty(intlength);stack*Create(){stack*p;intflag;flag=IsFull(length);if(flag==1){printf("stackisfull");returntop;}elsep=(stack*)malloc(sizeof(stack));retu