私信TA

用户名:1678463512

访问量:7290

签 名:

等  级
排  名 2703
经  验 2105
参赛次数 0
文章发表 20
年  龄 20
在职情况 待业
学  校 秋长中学
专  业

  自我简介:

解题思路:,先定义结构体,循环开辟节点,动态录入学生数据,连接a,b链表,只需要a链表的尾节点指向b链表的第一个数据节点,连接以后使用冒泡排序(嵌套循环),两种方法,1,交换节点数据,不改变链表结构,2,直接交换节点

注意事项:

参考代码:

#include <stdio.h>

#include <stdlib.h>

//#include <Windows.h>

#define N m+n  //a,b链表合并后链表数据节点的个数

struct students

{

   int num;//学号

   int score;//成绩

   struct students * next;//结构指针

};


struct students1

{

   int num;//学号

   int score;//成绩

   struct students1 * next;//结构指针

};



int main()

{

int temp2,temp3;

int m,n,i,j;//m为a链表元素数量,n为b链表元素的数量

struct students * h = NULL,*temp = NULL,*t = NULL,*p = NULL,*p1p,*p3p;

struct students1 * h1 = NULL,*temp1 = NULL,*t1 = NULL,*p1 = NULL;

struct students * a = NULL;

h = (struct students *)malloc(sizeof(struct students));

h1 = (struct students *)malloc(sizeof(struct students));

t = h;

t1 = h1;

//printf("请输入a,b链表元素的数量:\n");

scanf("%d %d",&m,&n);

//printf("a\n");

for(i = 0;i < m;i++)//动态录入链表a的数据域

{

temp = (struct students *)malloc(sizeof(struct students));

scanf("%d %d",&temp->num,&temp->score);

t->next = temp;

t = temp;

   

}

t->next = NULL;

//printf("b\n");

for(i = 0;i < n;i++)//动态录入链表b的数据域

{

temp1 = (struct students *)malloc(sizeof(struct students));

scanf("%d %d",&temp1->num,&temp1->score);

t1->next = temp1;

t1 = temp1;

   

}

t1->next = NULL;

//可以先插入链表最后按学号升序排列

//把b链表插入a链表中-直接把b链表的第一个数据节点插入即可

a = h1->next;//现在a是待插入的节点

//while(a != NULL)

//{

t = h->next;//节点将要插入的链表

    while(t != NULL)

    {

    if(t->next == NULL)//找到了尾节点

    {

    t->next = a;//原来的尾节点指向待插入的节点

    //a->next = NULL;//新插入的节点指向为空

break;

    }

   t = t->next;//循环变量的更新

    }

//a = a->next;

//}

//两个链表已合并,按学号排序a链表


for(i = 0;i < N - 1;i++)

{

        a = h->next;

for(j = 0;j < N - i - 1;j++)

{

if(a->num > a->next->num)

{

for(p = h;p !=NULL;p = p->next)

{

if(p->next == a)

p1p = p;

if(p->next == a->next)

p3p = p;

}

//直接交换节点

p = a->next->next;//储存尾节点

p1p->next = a->next;

a->next->next = a;

a->next = p;

/*

temp2 = a->num;//交换链表数据域的内容

temp3 = a->score;

a->num = a->next->num;

a->score = a->next->score;

a->next->num = temp2;

a->next->score = temp3;

*/

}

else

{

    a = a->next;

}

}

}



//输出链表

//printf("输出链表\n");

    p = h->next;

    if(p == NULL)printf("空表");

    else

    {

        while(p != NULL)

        {

            printf("%d %d\n",p->num,p->score);

            p = p->next;

        }



    }


return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区