#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef struct student
{
char name[15];
float score;
struct student* next;
}SS;
int len = sizeof(SS);
int main(void)
{
SS *create(void);
SS *reverse(SS *h);
void print(SS *h);
SS *head;
head=create();
printf("Original:\n");
print(head);
head=reverse(head);
printf("\nReversed:\n");
print(head);
return 0;
}
SS *reverse(SS *h)
{
SS *p=h,*t=NULL;
while(p)
{
p=h->next;
h->next=t;
t=h;
h=p;
}
return t;
}
SS* create(void)
{
SS* head;
SS* p1, * p2;
p1 = p2 = (SS*)malloc(len);
scanf("%s %f", &p1->name, &p1->score);
head = p1;
while (p1->score != -1)
{
p2->next = p1;
p2 = p1;
p1 = (SS*)malloc(len);
scanf("%s %f", &p1->name, &p1->score);
}
p2->next = NULL;
return head;
}
void print(SS *h)
{
while(h)
{
printf("%s %.1f\n",h->name,h->score);
h=h->next;
}
}
PTA7-4 单链表基础应用(4)--单链表逆置
最新推荐文章于 2024-09-08 23:15:50 发布