数据结构 实验一 线性表的应用

本文介绍了数据结构实验中线性表的应用,初步实现相关功能,但存在优化空间。
摘要由CSDN通过智能技术生成

实验

#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;	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值