这道题可以用头插法创建列表,然后正常输出:
#include<stdio.h> #include<stdlib.h> #define MAX 100 typedef struct Book{ double no; char name[MAX]; double price; struct Book * next; }Book,*BList; //头插法创建链表 void CreatList(BList &B) { B = (BList)malloc(sizeof(Book)); B->next = NULL; BList rear = B; int n; scanf("%d",&n); for(int i=1;i<=8;i++) { BList p = (BList)malloc(sizeof(Book)); scanf("%lf",&p->no); scanf("%s",p->name); scanf("%lf",&p->price); if(p->no==0&&p->name[0]=='0'&&p->price==0) break; p->next = rear->next; rear->next = p; } } void traverse(BList B) { BList p = B->next; while(p) { printf("%.0f ",p->no); printf("%s ",p->name); printf("%.2f",p->price); printf("\n"); p = p->next; } } int main() { BList B; CreatList(B); traverse(B); return 0; }