zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Git合并
    Git对象
    Git储藏和引用日志
    小小c#算法题 4 子数组最大和
    小小c#算法题 2 求素数
    小小c#算法题 5 插入排序
    小小c#算法题 1 找出数组中满足条件的两个数
    [转] WPF – Editing Mode with Save and Cancel Capability
    小小c#算法题 0 单循环冒泡排序
    小小c#算法题 3 字符串语句反转
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852204.html
Copyright © 2011-2022 走看看