Set up the Student Information link list

Source: Internet
Author: User
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;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.