建立一个链表,每个结点存储一个学生的姓名和成绩,将a,b,c三个结点按照分数由高到低链接起来,然后输出,编写链表结点连接函数connect。
链表的结构定义方式如下:
struct Node {
char *name;
int score;
struct Node *next;
};
请你完成以下函数代码
struct Node * connect(struct Node *x, struct Node *y, struct Node *z)
{
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char *name;
int score;
struct Node *next;
};
struct Node * connect(struct Node *x, struct Node *y, struct Node *z);
int main(int argc, char *argv[]) {
struct Node a, b, c, *p;
a.name = "LiPing";
b.name = "LiuHai";
c.name = "FengYun";
scanf("%d,%d,%d", &a.score, &b.score, &c.score);
p= connect(&a,&b,&c);
printf("%s-%d\n",p->name,p->score);
p=p->next;
printf("%s-%d\n",p->name,p->score);
p=p->next;
printf("%s-%d\n",p->name,p->score);
return 0;
}
struct Node * connect(struct Node *a, struct Node *b, struct Node *c)
{
struct Node * head,*p,*q,*temp;
int i=0;
head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
head->next=a;
a->next=b;
b->next=c;
c->next=NULL;
q=(struct Node *)malloc(sizeof(struct Node));
temp=a;
if(b->score>temp->score)
temp=b;
if(c->score>temp->score)
temp=c;
if(temp!=a)
{
q->score=a->score;
strcpy(q->name,a->name);
a->score=temp->score;
strcpy(a->name,temp->name);
temp->score=q->score;
strcpy(temp->name,q->name);
}
temp=b;
if(c->score>temp->score)
temp=c;
if(temp!=b)
{
q->score=b->score;
strcpy(q->name,b->name);
b->score=temp->score;
strcpy(b->name,temp->name);
temp->score=q->score;
strcpy(temp->name,q->name);
}
return head->next;
}
题主的代码修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char* name;
int score;
struct Node* next;
};
struct Node* connect(struct Node* x, struct Node* y, struct Node* z);
int main(int argc, char* argv[]) {
struct Node a, b, c, * p;
a.name = "LiPing";
b.name = "LiuHai";
c.name = "FengYun";
scanf("%d %d %d", &a.score, &b.score, &c.score);
p = connect(&a, &b, &c);
printf("%s-%d\n", p->name, p->score);
p = p->next;
printf("%s-%d\n", p->name, p->score);
p = p->next;
printf("%s-%d\n", p->name, p->score);
return 0;
}
struct Node* connect(struct Node* a, struct Node* b, struct Node* c)
{
struct Node* head, * p; //, * q, * temp;
//int i = 0;
head = (struct Node*)malloc(sizeof(struct Node));
head->next = NULL;
head->next = a;
a->next = NULL;
if (b->score > a->score) {
b->next = head->next;
head->next = b;
}
else{
a->next = b;
b->next = NULL;
}
p = head;
while (p->next && p->next->score > c->score)
p = p->next;
c->next = p->next;
p->next = c;
#if 0 // 以下删除
a->next = b;
b->next = c;
c->next = NULL;
q = (struct Node*)malloc(sizeof(struct Node));
temp = a;
if (b->score > temp->score)
temp = b;
if (c->score > temp->score)
temp = c;
if (temp != a)
{
q->score = a->score;
strcpy(q->name, a->name);
a->score = temp->score;
strcpy(a->name, temp->name);
temp->score = q->score;
strcpy(temp->name, q->name);
}
temp = b;
if (c->score > temp->score)
temp = c;
if (temp != b)
{
q->score = b->score;
strcpy(q->name, b->name);
b->score = temp->score;
strcpy(b->name, temp->name);
temp->score = q->score;
strcpy(temp->name, q->name);
}
#endif
return head->next;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:由于没有提供具体的出错语句和错误提示,我无法确定导致错误的原因。但是,以下是一些可能导致错误的常见问题和解决方案:
没有正确初始化参数指针。 在调用connect函数时,必须传递指向链表头部的指针,否则程序会崩溃。请确保在调用函数之前初始化参数指针,并将它们指向链表头,否则在访问链表节点时会出错。
没有正确连接链表节点。 在链接三个节点时,必须正确设置它们之间的指针。请确保按照分数由高到低将节点x,y,z连接起来,即先将x的next指针指向y,再将y的next指针指向z。
没有正确释放内存。 在连接链表节点时,可能需要动态分配内存。请确保在使用完节点之后,使用free()函数释放节点的内存。
以下是一个可能的解决方案:
struct Node *connect(struct Node *x, struct Node *y, struct Node *z) {
struct Node *head, *tail;
head = tail = NULL;
// 按照分数由高到低连接节点
if (x->score >= y->score && x->score >= z->score) {
head = tail = x;
if (y->score >= z->score) {
tail->next = y;
tail = y;
tail->next = z;
} else {
tail->next = z;
tail = z;
tail->next = y;
}
} else if (y->score >= x->score && y->score >= z->score) {
head = tail = y;
if (x->score >= z->score) {
tail->next = x;
tail = x;
tail->next = z;
} else {
tail->next = z;
tail = z;
tail->next = x;
}
} else {
head = tail = z;
if (x->score >= y->score) {
tail->next = x;
tail = x;
tail->next = y;
} else {
tail->next = y;
tail = y;
tail->next = x;
}
}
// 输出连接结果
struct Node *ptr = head;
while (ptr != NULL) {
printf("%s %d\n", ptr->name, ptr->score);
ptr = ptr->next;
}
// 释放节点内存
free(x);
free(y);
free(z);
return head;
}
这个解决方案将节点x,y,z按照分数由高到低连接起来,并输出连接后的链表结果。在函数结尾,使用free()函数释放节点的内存,避免程序内存泄漏。
struct Node *connect(struct Node *x, struct Node *y, struct Node *z) {
struct Node *head = NULL;
struct Node *temp = NULL;
// 将结点连接成链表
if (x != NULL) {
head = x;
temp = x;
while (temp->next != NULL) {
temp = temp->next;
}
}
if (y != NULL) {
if (head == NULL) {
head = y;
} else {
temp->next = y;
temp = y;
while (temp->next != NULL) {
temp = temp->next;
}
}
}
if (z != NULL) {
if (head == NULL) {
head = z;
} else {
temp->next = z;
temp = z;
}
}
// 对链表进行降序排序
struct Node *curr, *nextNode;
int swapped;
do {
swapped = 0;
curr = head;
while (curr->next != NULL) {
nextNode = curr->next;
if (curr->score < nextNode->score) {
// 交换节点位置
struct Node *tempName = curr->name;
int tempScore = curr->score;
curr->name = nextNode->name;
curr->score = nextNode->score;
nextNode->name = tempName;
nextNode->score = tempScore;
swapped = 1;
}
curr = curr->next;
}
} while (swapped);
return head;
}