/*该程序是用来进行链表相关操作的*/
/*经过Visual C++ 6.0调试*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct student
{
int data;
struct student *next;
}STU;
/*建立无序链表*/
STU *creatlist1()
{
STU *chead,*clast,*cnew;int n=1;
chead=(STU *)malloc(sizeof(STU));
chead->next=NULL;
clast=chead;
cnew=(STU *)malloc(sizeof(STU));
scanf("%d",&cnew->data);
while(cnew->data)
{
cnew->next=clast->next;
clast->next=cnew;
clast=clast->next;
cnew=(STU *)malloc(sizeof(STU));
scanf("%d",&cnew->data);
}
free(cnew);
return chead;
}
/*建立有序链表*/
STU *creatlist2()
{
STU *head,*ifront,*inew,*ilast;
head=(STU *)malloc(sizeof(STU));
head->next=NULL;
inew=(STU *)malloc(sizeof(STU));
scanf("%d",&inew->data);
while(inew->data)
{
ilast=head->next;
if(head->next==NULL)
{
head->next=inew;
inew->next=NULL;
}
else
{
while((inew->data>ilast->data)&&ilast->next)
{
ifront=ilast;
ilast=ilast->next;
}
if(inew->data<=ilast->data)
{
if(head->next==ilast) head->next=inew;
else ifront->next=inew;
inew->next=ilast;
}
else
{
ilast->next=inew;
inew->next=NULL;
}
}
inew=(STU *)malloc(sizeof(STU));
scanf("%d",&inew->data);
}
free(inew);
return head;
}
/*输出链表*/
void printlist(STU *head)
{
STU *ph;
ph=head->next;
while(ph)
{
printf("%4d",ph->data);
ph=ph->next;
}
printf("/n");
}
/*有序插入结点*/
STU *insert(STU *head,STU *stud)
{
STU *ifront,*inew,*ilast;
inew=stud;
ilast=head->next;
if(head->next==NULL) {head->next=inew;inew->next=NULL;}
else
{
while((inew->data>ilast->data)&&ilast->next)
{
ifront=ilast;
ilast=ilast->next;
}
if(inew->data<=ilast->data)
{
if(head->next==ilast) head->next=inew;
else ifront->next=inew;
inew->next=ilast;
}
else
{
ilast->next=inew;
inew->next=NULL;
}
}
return head;
}
/*将无序链表变成有序链表*/
STU *sort(STU *head)
{
STU *shead,*sp,*th;
shead=(STU *)malloc(sizeof(STU));
shead->next=NULL;
sp=th=head->next;
while(th)
{
th=th->next;
shead=insert(shead,sp);
sp=th;
}
return shead;
}
/*将2个链表合并为1个有序链表*/
STU *unilist1(STU *head1,STU *head2)
{
STU *p;
p=head1->next;
while(p->next)
p=p->next;
p->next=head2->next;
head1=sort(head1);
return head1;
}
/*将2个有序链表合并为1个有序链表*/
STU *unilist2(STU *h1,STU *h2)
{
STU *h3,*tp1,*tp2,*tp3;
h3=(STU *)malloc(sizeof(STU));
h3->next=NULL;
tp1=h1->next;
tp2=h2->next;
tp3=h3;
while(tp1&&tp2)
{
if(tp1->data<=tp2->data)
{
tp3->next=tp1;
tp1=tp1->next;
}
else
{
tp3->next=tp2;
tp2=tp2->next;
}
tp3=tp3->next;
}
if(!tp1) tp3->next=tp2;
if(!tp2) tp3->next=tp1;
return h3;
}
/*将无序链表变成有序链表,菜单选择*/
void slist()
{
STU *sh;
printf("请输入链表的数据:");
sh=creatlist1();/*creatlist2亦可*/
printf("您上面输入的数据是:");
printlist(sh);
sh=sort(sh);
printf("输出排序后的链表如下:/n");
printlist(sh);
}
/*合并链表,菜单选择*/
void ulist()
{
STU *uh1,*uh2;
printf("请输入第一个链表的数据:");
uh1=creatlist1();/*creatlist2亦可*/
printf("您上面输入的数据是:");
printlist(uh1);
printf("请输入第二个链表的数据:");
uh2=creatlist1();/*creatlist2亦可*/
printf("您上面输入的数据是:");
printlist(uh2);
uh1=unilist1(uh1,uh2);/*unilist2亦可*/
printf("输出合并并排序后的链表如下:/n");
printlist(uh1);
}
/*菜单*/
void mmenu()
{
printf("***——————主菜单——————***/n");
printf("1.将一个无序链表排序后输出/n");
printf("2.将两个链表合并后输出一个有序链表/n");
printf("0.退出该程序/n");
printf("***———————————————***/n");
printf("/n");
printf("***——————请注意——————***/n");
printf("1.本程序只能输入数字,按数字/"0/"来结束输入!/n");
printf("2.严格按照所要求的格式操作!/n");
printf("***———————————————***/n");
}
/*选择*/
void mchoice()
{
int ch;
printf("请键入主菜单中相应数字,选择相应操作:");
scanf("%d",&ch);
switch(ch)
{
case 1:slist();break;
case 2:ulist();break;
case 0:exit(0);
default:printf("请键入相应数字进行选择!/n");
}
}
void main()
{
while(1)
{
system("cls");
mmenu();
mchoice();
system("pause");
}
}
链表
最新推荐文章于 2024-09-09 16:39:53 发布