链表

本文介绍了一种使用C语言实现链表的操作方法,包括创建无序和有序链表、链表排序、链表合并等功能,并提供了完整的源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*该程序是用来进行链表相关操作的*/
/*经过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");
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值