#include <iostream>
using namespace std;
const int MAXSIZE = 20;
const int ERROR = 0;
const int OK = 1;
typedef int Status;
//顺序表实现
//template <typename T>
typedef struct Student
{
int number;
int grade; //分数为int或double?
Student() {
}
Student(int num, int g):number(num), grade(g) {
}
}Student;
class SqList
{
public:
SqList();
~SqList();
Status ListInsert(int i, Student &e);
Status ListDelete(int i, Student &e);
void OutoutList() const;
void InputList();
void MergeList(const SqList &La, const SqList &Lb);
private:
Student *elem;
int length; //实际存放元素的个数
int listSize; //可以容纳的最大元素的个数
};
//重载输出运算符,直接cout<<student*
ostream& operator << (ostream& os, const Student& x)
{
return os << '(' <<x.number << ',' << x.grade << ')';
}
SqList::SqList()
{
elem = new Student[MAXSIZE];
length = 0; //当前长度
listSize = MAXSIZE;
}
SqList::~SqList()
{
delete[] elem;
}
//在第i个元素之前插入元素
Status SqList::ListInsert(int i, Student &e)
{
Student *p;
//插入位置不合理
if(i<1 || i>length+1)
{
return ERROR;
}
//容量已满扩容
if(length >= MAXSIZE)
{
Student *newbase = new Student[MAXSIZE*2];
if(!newbase)
{
cerr << "内存分配错误!" <<endl;
return ERROR;
}
p = elem;
elem = newbase;
//复制原来的数据
for(int i=0; i<length; i++)
{
elem[i] = p[i];
}
listSize = MAXSIZE * 2;
数据结构 实验一 线性表的应用
最新推荐文章于 2024-08-08 13:38:11 发布
本文介绍了数据结构实验中线性表的应用,初步实现相关功能,但存在优化空间。
摘要由CSDN通过智能技术生成