21000929ad


私信TA

用户名:21000929ad

访问量:729

签 名:

等  级
排  名 6278
经  验 1381
参赛次数 0
文章发表 16
年  龄 0
在职情况 学生
学  校 广东工业大学
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include<stdio.h>

#include<stdlib.h>

struct Student

{

int num;

int score;

};

struct Node

{

struct Student data;

struct Node* next;

};


struct Node* creatlist()

{

struct Node* headnode = (struct Node*)malloc(sizeof(struct Node));

headnode->next = NULL;

return headnode;

}


struct Node* creatnode(struct Student* data)

{

struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));

newnode->data.num = data->num;

newnode->data.score = data->score;

newnode->next = NULL;

return newnode;

}

void printlist(struct Node* head)

{

struct Node* pmove = head->next;

while (pmove)

{

printf("%d %d\n", pmove->data.num, pmove->data.score);

pmove = pmove->next;

}

}


void insertheadby(struct Node* head, struct Student* data)

{

struct Node* newNode = creatnode(data);

struct Node* posNode = head;

while (posNode->next != NULL)

{

posNode = posNode->next;

}

posNode->next = newNode;

}

//void deletenode(struct Node* head, int data)

//{

//struct Node* postNodeFront = head;

//struct Node* postNode = head->next;

//if (postNode == NULL)

//return;

//while (postNode->data != data)

//{

//postNodeFront = postNode;

//postNode = postNodeFront->next;

//if (postNode == NULL)

//return;

//}

//postNodeFront->next = postNode->next;

//deletenode(head, data);

//}


struct Node* merge(struct Node* head1, struct Node* head2)

{

struct Node* p, * q, * r;

p = head1->next; q = head2->next;

r = head1;//也可以选择开辟新的内存空间 

while (1)

{

if (p != NULL && q != NULL)

{

if(p->data.num<q->data.num)

{

r->next = p;

r = p;

p = p->next;

}

else

{

r->next = q;

r = q; q = q->next;

}

}

else if (p == NULL)//任何一个单链表为null,就可以退出循环了

{

r->next = q;

break;

}

else if (q == NULL)

{

r->next = p;

break;

}

}

return head1;

}

void  BubbleSort(struct Node* list)

{

for (struct Node* first = list->next; first != NULL; first = first->next)

{

for (struct Node* second = list->next; second != NULL; second = second->next)

{

if (second->next != NULL)

{

if (second->data.num > second->next->data.num)

{

struct Student temp =  second->data;

second->data = second->next->data;

second->next->data= temp;

}

}

}

}

}

int main()

{

struct Node* head1 = creatlist();

struct Node* head2 = creatlist();

struct Student* data1 = (struct Student*)malloc(sizeof(struct Student));

int n, i,m;

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

for (i = 0; i < n; i++)

{

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

insertheadby(head1, data1);

}

for (i = 0; i < m; i++)

{

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

insertheadby(head2, data1);

}

struct Node *mergence=merge(head1, head2);

BubbleSort(mergence);

printlist(mergence);

return 0;

}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区