用顺序表创建学生信息花名册

问题:用顺序表创建学生信息包括姓名,学号,语数外3科成绩,并对顺序表实现基本的操作。

1、LinearList.h

#ifndef _LINEARLIST_H_
#define _LINEARLIST_H_
#include<iostream>
using namespace std;
template<class T>          //T为数据类型
class LinearList           //模板名
{
private:
    int length;            //当前数组元素个数
    int Maxsize;           //线性表中最大元素个数
    T*element;             //一维动态数组
public:
    LinearList(int LLMaxsize);          //构造函数,创建空表
    ~LinearList();                      //析构函数,删除表
    LinearList<T>&InSert(int k, const T & x);  //在第K个位置插入元素x,返回插入后的线性表
    bool IsEmpty()const;  //判断表是否为空,表空返回true,非空返回false
    int GetLength()const;  //返回表中数据元素个数
    bool GetData(int k, T & x);  //将表中第k个元素返回到x中,不存在返回false
    bool ModifyData(int k, const T & x);  //将表中第k个元素修改为x,不存在返回false
    int Find(const T &x);  //返回x在表中的位置,不存在返回0
    LinearList<T>&DeleteByIndex(int k, T & x);  //删除表中第k个元素,并把它保存在x中,返回修改后的线性表
    LinearList<T>&DeleteByKey(const T & x, T & y);  //删除表中关键字为x元素,返回删除后的线性表
    void OutPut(ostream&out)const;   //输出线性表的每个元素


};

template <class T>//实现构造函数
LinearList<T>::LinearList(int LLMaxsize)
{
    Maxsize = LLMaxsize;
    element = new T[LLMaxsize];
    length = 0;
}

template <class T> //实现析构函数
LinearList<T>::~LinearList()
{
    delete[] element;
}
template <class T>//实现插入新数据元素
LinearList<T> & LinearList<T>::InSert(int k, const T & x)
{
    if (k<1 || k>length + 1)
        cout << "元素下标越界,添加元素失败" << endl;
    else
        if (length == Maxsize)
            cout << "此表已满,无法添加新元素" << endl;
        else
        {
            for (int i = length; i > k - 1; i--)
                element[i] = element[i - 1];
            element[k - 1] = x;
            length++;
        }
    return *this;
}
template <class T>//实现判断是否为空表
bool LinearList<T>::IsEmpty() const
{
    return length == 0;
}
template <class T>//求当前表的长度
int LinearList<T>::GetLength() const
{
    return length;
}

template <class T>//实现按位置取元素
bool LinearList<T>::GetData(int k, T&x)
{
    if (k<1 || k>length)
        return false;
    else
    {
        x = element[k - 1];
        return true;
    }
}

template <class T>//实现按位置修改元素
bool LinearList<T>::ModifyData(int k, const T&x)
{
    if (k<1 || k>length)
        return false;
    else
    {
        element[k - 1] = x;
        return true;
    }
}

template <class T>//实现按关键字查找
int LinearList<T>::Find(const T&x)
{
    for (int i = 0; i < length; i++)
        if (element[i] == x)
            return i + 1;
    return 0;
}

template <class T>//实现按位置删除
LinearList<T> & LinearList<T>::DeleteByIndex(int k, T & x)
{
    if (GetData(k, x))
    {
        for (int i = k - 1; i < length - 1; i++)
            element[i] = element[i + 1];
        length--;
    }
    else
        cout << "元素下标越界,删除失败" << endl;
    return *this;
}

template <class T>//实现按关键字删除
LinearList<T> & LinearList<T>::DeleteByKey(const T & x, T&y)
{
    int index = Find(x);
    if (index != 0)
        return DeleteByIndex(index, y);
    else
    {
        cout << "没有此元素,删除失败" << endl;
    }
    return *this;
}

template <class T>//实现数据元素的输出
void LinearList<T>::OutPut(ostream & out) const
{
    for (int i = 0; i < length; i++)
        out << element[i] << endl;
}

//全局函数,实现重载运算符<<
template<class T>
ostream& operator<<(ostream &out, const LinearList<T> & x)
{
    x.OutPut(out);
    return out;
}
#endif 


 

2、Node.h

//数据元素Node类
#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
#include <string>
using namespace std;
class Node
{
private:
    string StdNumber;
    string StdName;
    int Score[3];
public:
    Node(string & NumberOfStudent, string & NameOfStudeng, int grade[]);//有参构造函数
    Node();//无参构造函数
    Node&GetNode();//得到结点数据
    void OutPutNode(ostream&out)const;//输出结点数据
};

//实现构造函数
Node::Node(string & NumberOfStudent, string & NameOfStudeng, int grade[])
{
    StdNumber= NumberOfStudent;
    StdName= NameOfStudeng;
    for (int i = 0;i < 3; i++)
        Score[i] = grade[i];

}
 Node::Node()
{

}
Node & Node::GetNode()
{
    return *this;
}

void Node::OutPutNode(ostream&out)const
{
    out << StdNumber << StdName << endl;
    out << "语文:" << Score[0];
    out << "数学:" << Score[1];
    out << "英语:" << Score[2];

}
//全局函数,重载插入运算符
ostream&operator<<(ostream&out, const Node&x)
{
    x.OutPutNode(out);
    return out;

}

#endif 

3、main

#include "Node.h"
#include "LinearList.h"
#include<iostream>
#include<string>
using namespace std;

int main()
{
    LinearList<Node> NodeLList(100);
    int grade1[3] = { 99,100,95 };
    int grade2[3] = { 95,198,95 };
    int grade3[3] = { 99,89,95 };
    string StdNumber1 = "100001";          //创建学生1
    string StdName1 = "tom";
    Node Node1(StdNumber1, StdName1, grade1);
    string StdNumber2 = "100002";          //创建学生2
    string StdName2 = "jerry";
    Node Node2(StdNumber2, StdName2, grade2);
    string StdNumber3 = "100003";         //创建学生3
    string StdName3 = "xiaoming";
    Node Node3(StdNumber3, StdName3, grade1);
    cout << Node1 << endl;               //输出学生1
    Node x;                              //创建空对象
    NodeLList.InSert(1, Node1);         //插入学生1
    NodeLList.InSert(1, Node2);         //插入学生2
    NodeLList.InSert(1, Node3);         //插入学生3
    cout << "当前表的长度为:" << NodeLList.GetLength() << endl;
    cout << NodeLList << endl;      //输出此时链表
    int getnum=NodeLList.GetData(2, x);
    if(getnum==1)
        cout << x << endl;             //输出第二个位置的元素
    NodeLList.DeleteByIndex(3, x);//删除第三个位置的元素
    cout << NodeLList << endl;

}

4、测试结果

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
顺序表是一种线性表,它的元素在内存是连续存储的。在存储学生信息时,可以使用结构体来定义学生信息的数据类型,然后将结构体数组作为顺序表的元素存储。以下是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 20 // 定义学生信息结构体 typedef struct { char no[10]; // 学号 char name[20]; // 姓名 int score; // 成绩 } Student; // 定义顺序表结构体 typedef struct { Student elem[MAX_SIZE]; // 存储学生信息的数组 int length; // 当前存储的学生信息数量 } SeqList; // 初始化顺序表 void init(SeqList *L) { L->length = 0; } // 插入学生信息 int insert(SeqList *L, Student s) { if (L->length >= MAX_SIZE) { return 0; // 顺序表已满,插入失败 } int i; for (i = L->length - 1; i >= 0 && strcmp(L->elem[i].no, s.no) > 0; i--) { L->elem[i + 1] = L->elem[i]; // 将比插入学生信息学号大的学生信息后移 } L->elem[i + 1] = s; // 插入学生信息 L->length++; // 学生信息数量加1 return 1; // 插入成功 } // 按学号查找学生信息 int search(SeqList L, char no[], Student *s) { int i; for (i = 0; i < L.length; i++) { if (strcmp(L.elem[i].no, no) == 0) { *s = L.elem[i]; // 找到学生信息,将其赋值给s return 1; // 查找成功 } } return 0; // 未找到学生信息,查找失败 } // 按成绩查找学生信息 void searchByScore(SeqList L, int score) { int i; for (i = 0; i < L.length; i++) { if (L.elem[i].score >= score) { printf("学号:%s 姓名:%s 成绩:%d\n", L.elem[i].no, L.elem[i].name, L.elem[i].score); } } } int main() { SeqList L; init(&L); // 初始化顺序表 Student s1 = {"001", "张三", 80}; Student s2 = {"002", "李四", 90}; Student s3 = {"003", "王五", 85}; insert(&L, s1); // 插入学生信息 insert(&L, s2); insert(&L, s3); Student s; if (search(L, "002", &s)) { // 按学号查找学生信息 printf("学号:%s 姓名:%s 成绩:%d\n", s.no, s.name, s.score); } searchByScore(L, 85); // 按成绩查找学生信息 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值