69,112
社区成员




#include<stdio.h>
#include<stdlib.h>
struct Student {
char name[20];
float score;
struct Student* next;
};
struct Student* creat() {
struct Student* head, * p1, * p2;
int n = 0;
head=p1 = p2 = (struct Student*)malloc(sizeof(struct Student));
printf("请输入学生的姓名和成绩:\n");
scanf_s("%s%f", p1->name, &p1->score);
while ((p1->score != 0)&&(p1->name[0]!='q'))
{
n++;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct Student*)malloc(sizeof(struct Student));
scanf_s("%s,%f", p1->name, &p1->score);
return head;
}
}
void print(struct Student* head)
{
struct Student* p;
p = head;
if (head == NULL)
printf("无法打印。");
else
do {
printf("%s,%f", p->name, p->score);
p = p->next;
} while (p != NULL);
}
void sort(struct Student** head)
{
struct Student* p;
int temp;
p = *head;
if (head == NULL) {
printf("无法排序");
}
else
{
while (p != NULL) {
if (p->score > p->next->score) {
temp = p->score;
p->score = p->next->score;
p->next->score = temp;
}
}
}
}
int main()
{
struct Student* head;
head = creat();
print(head);
return 0;
}
请输入学生的姓名和成绩:
sam
90
tom
sam,0.000000
D:\code\Project1\x64\Release\Project1.exe (进程 16392)已退出,代码为 -1073741819。
按任意键关闭此窗口. . .
struct Student* creat() 这是个啥,你要创建一个链表,链表结构定义好了,你直接linklist list_create(){
linklist H;
H=(linklist)malloc(sizeof(listnode));
if( H == NULL){
printf("create node failed\n");
return NULL;
}
H->data = 0;
H->next = NULL;
return H;
}这样就创建了