单链表----先联合再排序

本文介绍了一种使用C语言实现的链表数据结构操作方法,包括链表的创建、两个链表的合并以及合并后的链表排序。通过具体代码示例,详细展示了如何通过输入整数来构建两个链表,并在合并后对链表进行排序,最后输出排序后的链表元素。

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

#include <stdio.h>
#include <stdlib.h>
typedef struct stu{
int data;
struct stu *next;
}s,*link;

void output(link h)//输出
{

    s *p;
    p=h->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
link lianhe(link h1,link h2)//将两个链表联合
{
    s *q;
    q=h1;
    while(q->next!=NULL)
    {
        q=q->next;
    }
    q->next=h2->next;
    return h1;
}

void paixu(link h)
{
   s *p,*q;
   int t;
    for(p=h->next;p!=NULL;p=p->next)
 {
    for(q=p->next;q!=NULL;q=q->next)
  {
    if(p->data<q->data)
     {
      t=p->data;
      p->data=q->data;
      q->data=t;
     }
  }
 }
}
int main()
{
    link h1,h2,h3;
    int x,flag=0;
    s *r1,*p1,*r2,*p2;
    h1=(s*)malloc(sizeof(s));
    h2=(s*)malloc(sizeof(s));
    h3=(s*)malloc(sizeof(s));
    h1->next=NULL;h2->next=NULL;h3->next=NULL;
    r1=h1;r2=h2;
    while(scanf("%d",&x)!=EOF)
    {
        if(x==0)
        {
            if(flag==0)
            {
             r1->next=NULL;
             flag++;
            }
            else
            {
                r2->next=NULL;
                break;
            }
        }
        else
        {
        if(flag==0)
        {
        p1=(s*)malloc(sizeof(s));
        p1->data=x;
        r1->next=p1;
        r1=p1;
        }
        else
        {
        p2=(s*)malloc(sizeof(s));
        p2->data=x;
        r2->next=p2;
        r2=p2;
        }
        }
    }
    h3=lianhe(h1,h2);
    paixu(h3);
    output(h3);

    return 0;
}
 

: error C2275: “Node”: 将此类型用作表达式非法 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(32) : error C2065: “slow”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(32) : error C2065: “fast”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(32) : error C2100: 非法的间接寻址 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(33) : error C2065: “fast”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(33) : error C2065: “fast”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(33) : error C2223: “->next”的左侧必须指向结构/联合 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(34) : error C2065: “slow”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(34) : error C2065: “slow”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(34) : error C2223: “->next”的左侧必须指向结构/联合 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(35) : error C2065: “fast”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(35) : error C2065: “fast”: 未声明的标识符 C:\aptat\test_dir\assignment\S\C\2024春 单链表排序_2024010911003_20250331214333\2024春 单链表排序_2024010911003_20250331214333.cpp(35) : error C2223: “->根据报错修改上述代码
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值