TASK(结点插入)

01 #include <iostream>
02 using namespace std;
03 int s;
04 struct student
05 {
06     int Order;
07     char  Name[10];
08     char Sex[2];
09     int Grade;
10 } temp;
11 struct node
12 {
13     struct student data;
14     struct node *next;
15 }*head;
16 void inserrt(struct node *head,int a,struct student b)
17 {
18     struct node *p,*q;
19     p=head;
20     for(int i=0; i<a&&i<s; i++)
21         p=p->next;
22     q=new node;
23     q->data=b;
24     q->next=p->next;
25     p->next=q;
26     s++;
27 }
28 void show (struct node*head)
29 {
30     struct node*p=head->next;
31     while(p)
32     {
33         cout<<p->data.Order<<" "<<p->data.Name<<" "<<p->data.Sex<<" "<<p->data.Grade<<endl;
34         p=p->next;
35     }
36 }
37 int main()
38 {
39     int a;
40     s=0;
41     head=new node;
42     head->next=NULL;
43     while(cin>>a&&a!=-1)
44     {
45         cin>>temp.Order>>temp.Name>>temp.Sex>>temp.Grade;
46         inserrt(head,a,temp);
47     }
48     show(head);
49     return 0;
50 }

猜你喜欢

转载自blog.csdn.net/beposit/article/details/80700001