首页 团队成员,上杉九月的文章

先放一下代码,原理分析以后讲

#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct node
{
    string name;
    int score;
}node;
int main()
{
    string name;
    int score;
    cin>>name>>score;

    node *p = NULL;
    p = new node;
    p->name = name;
    p->score = score;

    cout<<p->name<<"  "<<p->score<<endl;

    system("pause");
    return 0;
}
#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct node
{
    string name;
    int score;
    node* next;
}node;
int main()
{
    string name[3];
    int score[3];
    for(int i = 0; i < 3; i++)
    {
        cin>>name[i]>>score[i];
    }
    node* first;
    first=new node;
    first->next=NULL;

    node *tmp;
    int num = 3;
    while(num > 0)
    {
        num--;
        tmp = new node;
        tmp->next = first->next;
        tmp->name = name[num];
        tmp->score = score[num];
        first->next = tmp;
    }


    node *p=first->next;
    while(p)
    {
        cout<<p->name<<"  "<<p->score<<endl;
        p=p->next;
    }

    system("pause");
    return  0;
}
#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct node
{
    string name;
    int score;
    node* next;
}node;
node* create(node* first,string name[],int score[],int len)
{
    first = new node;
    first->next=NULL;
    node *tmp;
    for(int i = 0; i < len; i++)
    {
        tmp = new node;
        tmp->next = first->next;
        tmp->name = name[i];
        tmp->score = score[i];
        first->next = tmp;
    }
    return  first;
}

void show(node *first)
{
        node* p = first->next;
        while(p != NULL)
        {
            cout<<p->name<<"  "<<p->score<<endl;
            p=p->next;
        }
}
int main()
{
    int n;
    cin>>n;
    string name[n];
    int score[n];
    node* first;

    for(int i = 0; i < n; i++)
    {
        cin>>name[i]>>score[i];
    }

    first=create(first,name,score,n);
    show(first);

    system("pause");
    return  0;
}
#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct node
{
    string name;
    int score;
    node* next;
}node;
node* create(node* first,string name[],int score[],int len)
{
    node *r, *tmp;
    first = new node;
    first->next = NULL;
    r = first;

    for(int i = 0; i < len; i++)
    {
        tmp = new node;
        tmp->name = name[i];
        tmp->score = score[i];
        tmp->next = r->next;
        r->next = tmp;
        r = tmp;
    }

    return  first;
}

void show(node *first)
{
        node *p = first->next;
        while(p != NULL)
        {
            cout<<p->name<<"  "<<p->score<<endl;
            p=p->next;
        }
}
int main()
{
        int n;
        cin>>n;
        string name[n];
        int score[n];
        node* first;

        for(int i = 0; i < n; i++)
        {
            cin>>name[i]>>score[i];
        }
        
        first=create(first,name,score,n);
        show(first);

        system("pause");
        return  0;
}



文章评论

目录