定义一个结构体管理士兵的信息(编号,姓名,兵种,年龄)。输入若干士兵的信息,当输入的士兵编号为 0 时输入结束。建立一个单向链表存放士兵信息并输出。(链表的建立和输出分别定义函数实现
#include#include struct s{ int N; char name[20]; char kind [20]; int age; struct s *next; }; struct s* create(){ struct s *tmp=(struct s *)malloc(sizeof (struct s)); return tmp; } void ps(struct s *head){ struct s *tmp=head->next; while(head->next){ printf("No:%d Name:%s Kind:%s Age:%dn",tmp->N,tmp->name,tmp->kind,tmp->age); head=tmp; tmp=tmp->next; } } int main() { struct s *tmp,*head,*index; head=create(); index=head; int n; scanf("%d",&n); while (1){ if(n!=0){ tmp=(struct s *)malloc(sizeof (struct s)); tmp->N=n; scanf(" %s %s %dn",&tmp->name,&tmp->kind,&tmp->age); index->next=tmp; index=tmp; }else{ index->next=NULL; break; } scanf("%d",&n); } ps(head); return 0; }
测试数据
1 yi 海军 23 2 er 陆军 35 3 san 空军 29 0



