今天复习单项链表。自己编了一上午,蛋疼。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 2
struct student
{
char name[M];
double score;
struct student *next;
};
struct student *creat(int n)
{
struct student *phead, *pnew, *ptail;
pnew = (struct student *)malloc(sizeof(struct student));
phead = pnew;
ptail = pnew;
for (int i = 1; i < n; ++i)
{
pnew = (struct student *)malloc(sizeof(struct student));
ptail->next = pnew;
ptail = pnew;
}
ptail->next = NULL;
return phead;
}
void pri(struct student *head)
{
struct student *p;
p = head;
while(p != NULL)
{
printf("%s %f\n", p->name, p->score);
p = p->next;
}
}
struct student *insert(struct student *head)
{
struct student *p, *pold, *pnew = (struct student *)malloc(sizeof(struct student));
scanf("%s %lf", pnew->name, &pnew->score);
p = head;
if (pnew->score > head->score)
{
pnew ->next = head;
head = pnew;
}
else
{
while(p != NULL && pnew->score < p->score)
{
pold = p;
p = p->next;
}
pnew->next = p;
pold->next = pnew;
}
return head;
}
int main(int argc, char const *argv[])
{
struct student *head;
int n = 0;
scanf("%d", &n);
head = (struct student *)malloc(sizeof(struct student));
scanf("%s %lf", head->name, &head->score);
head->next = NULL;
for (int i = 1; i < n; ++i)
{
head = insert(head);
}
pri(head);
return 0;
}
还有好些错的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 2
struct student
{
// char name[M];
double score;
struct student *next;
};
struct student *creat(int n)
{
struct student *head, *tail, *pnew;
pnew = (struct student *)malloc(sizeof(struct student));
scanf("%d", &pnew->score);
head = pnew;
tail = pnew;
for (int i = 1; i < n; ++i)
{
pnew = (struct student *)malloc(sizeof(struct student));
scanf("%d", &pnew->score);
tail->next = pnew;
tail = pnew;
}
tail->next = NULL;
printf("Created\n");
return head;
}
struct student *sort(struct student *head)
{
struct student *p, *q, *small;
double temp;
for (p = head; p->next != NULL; p = p->next)
{
small = p;
for (q = p->next; q; q = q->next)
{
if (q->score < small->score)
{
small = q;
}
if (small != p)
{
temp = p->score;
p->score = small->score;
small->score = temp;
}
}
}
return head;
}
int main(int argc, char const *argv[])
{
int n = 0;
scanf("%d", &n);
struct student *head, *p, *q;
head = creat(n);
q = sort(head);
for (p = q; p != NULL; p = p->next)
{
printf("%d\n", p->score);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 2
struct student
{
char name[M];
double score;
struct student *next;
};
struct student *creat(int n)
{
struct student *head, *tail, *pnew;
pnew = (struct student *)malloc(sizeof(struct student));
scanf("%s %d", &pnew->name, &pnew->score);
head = pnew;
tail = pnew;
for (int i = 1; i < n; ++i)
{
pnew = (struct student *)malloc(sizeof(struct student));
scanf("%s %d", &pnew->name, &pnew->score);
tail->next = pnew;
tail = pnew;
}
tail->next = NULL;
printf("Created\n");
return head;
}
// void swap(struct student *a, struct student *b)
// {
// struct student c;
// c = *a;
// // a->name = b->name;
// a->score = b->score;
// *b->name = *c.name;
// // b->score =
// // c = *a;
// // *a = *b;
// // *b = c;
// // printf("did swap\n");
// // c->next = a->next;
// // a-next = b->next;
// }
struct student *sort(struct student *head)
{
printf("Began sort!\n");
struct student *p,*q;
// int k = 0;
p = head;
int i = 0;
for (;p->next != NULL; p = p->next)
{
printf("first for\n");
// for (q = p->next; q != NULL; q = q->next, i++)
// {
// if (p->score < q->score)
// {
// // swap(p, q);
// }
// printf("i did it\n");
// }
// for (q = p->next; q != NULL; q = NULL)
// {
// printf("second for\n");
// printf("q is %s\n", q->name);
// if (q->next == NULL)
// {
// printf("q's next == NULL\n");
// }
// printf("q->next = %s\n", q->next->name);
// // // if (q->next->next == NULL)
// // {
// // printf("q-next-next = NULL\n");
// // }
// // printf("%s\n", q->next->name);
// // if (p->score < q->score)
// // {
// // swap(p, q);
// // }
// }
// q = p->next;
// while(q != NULL)
// {
// if (p->score < q->score)
// {
// swap(p, q);
// }
// printf("before q = %s\n", q->next);
// q = q->next;
// printf("after q = %s\n", q->name);
}
// while(p->next != NULL)
// {
// p = p->next;
// }
printf("Sorted\n");
}
int main(int argc, char const *argv[])
{
int n = 0;
scanf("%d", &n);
struct student *head, *p;
head = creat(n);
// head = head->next;
// for (int i = 1; head != NULL; head = head->next, i++)
// {
// printf("%d'th yuansu\n", i);
// printf("%s\n", head);
// }
// if (head->next->next == NULL)
// {
// printf("OK\n");
// }
// printf("%s\n", head);
// printf("fist person is %s\n", head->name);
head = sort(head);
// if (head->next->next == NULL)
// {
// printf("OK\n");
// }
// for (p = head; p != NULL; p = p->next)
// {
// printf("%s %d\n", p->name, p->score);
// }
return 0;
}