C++学习平时作业(1)--链表

 

#include <iostream>

usingnamespace std;

//要求创建一个学生成绩信息链表,并输出每个学生的成绩信息。

struct structNode
{
    int num;
    char name[10];
    float English_score;
    float Math_score;
    structNode *next;
};

structNode *head; //链表的头节点的指针
structNode *pre;//链尾插入节点时需要的链尾指针

void init()
{
  head = new structNode;
  if(head!=NULL)
  {
    cout << "OK!" << endl;
    head->next = NULL; //链表头指针初始化
    pre = head;
  }
  else
    cout << "Failed!" << endl;
}

void add()
{
    int n;
  cout << "input student number:" << endl;

  cin >> n;

  structNode *p;//p为工作指针

  for(int i=0; i<n; i++)
  {

    p = new structNode;
    cout << "please input the No." << i+1 <<"'s number";
    cin >> p->num;

    cout << "please input the No." << i+1 <<"'s name";
    cin >> p->name;

    cout << "please input the No." << i+1 <<"'s the English's score";
    cin >> p->English_score;

    cout << "please input the No." << i+1 <<"'s the Math's score";
    cin >> p->Math_score;

    p->next = NULL;
    pre->next = p;
    pre = p;
  }
}

void putout()
{
    structNode *s;

    s = head;

    while(s->next!=NULL)
    {
        s = s->next;
        cout << s->num << "	";
        cout << s->name << "	";
        cout << s->English_score << "	";
        cout << s->Math_score << "	" << endl;
    }
}

int main()
{
    init();
    add();
    cout << "--------------------------------------------------------------------------------" << endl;
    putout();
    cout << "--------------------------------------------------------------------------------" << endl;

    return0;
}
原文地址:https://www.cnblogs.com/AlinaL/p/12852204.html