first, the original question
Establish Student Information list (STUDENT.CPP)
Topic Description
Give the name and grade of n students, please create a linked list of student information. And the elements of the linked list are output sequentially. input
First line: An integer n
Next n lines, each line a student's name and age. Output
n lines, each line includes the student's name and age. Sample Input
3
Wuzhenghao 18
Liudeyu 20
liuying Sample Output
Wuzhenghao 18
Liudeyu 20
Liuying Tips
Request the establishment of student linked lists. Second, analysis
According to the title, this problem should be done with a linked list (please consciously), click here to get information about the linked list. Then we first define a struct body and define a structure body pointer inside the structure. As follows:
struct student{
char name[100];
int age;
Student *next;//structure Body Pointer
};//defines a struct
The first person's data has to be handled specifically, so define a head that stores the first person. Then set the next artificial null, and then define a struct as the previous number, which can be convenient for the linked list to be exported later. The cycle is repeated n-1 times. Pointer p for the current one, good for the linked list to facilitate. Then, after the output, complete. For more information, see the source code below. Third, the source code
#include <cstdio> #include <iostream> #include <cstring> #include <
time.h> #include <algorithm> #include <queue> using namespace std;
void Fre () {//Read output function freopen ("student.in", "R", stdin);
Freopen ("Student.out", "w", stdout);
} struct student{char name[100];
int age;
Student *next;//structure Body pointer};//defines a struct body int main () {//fre ();
int n;
cin>>n;//read into circulation quantity student *head=new student; cin>>head->name>>head->age;//read the first person's name and age head->next=null;//set the next person to temporarily empty student *p=head;//definition knot construct for (int i=1;i<n;i++) {//Loop n-1 student *t=new student;//definition structure Body cin>>t->name>>t->
age;//read the name and age of the person t->next=null;//set the person's next person temporarily empty p->next=t;//set up the next man for this man p=p->next; for (p=head;p!=null;p=p->next)//Output cout<<p->name<< "" <<p->age<<endl;}