A. DS顺序表--类实现

题目描述
实现顺序表的用C++语言和类实现顺序表

属性包括:数组、实际长度、最大长度(设定为1000)

操作包括:创建、插入、删除、查找

类定义参考

输入
第1行先输入n表示有n个数据,即n是实际长度;接着输入n个数据
第2行输入要插入的位置和新数据
第3行输入要插入的位置和新数据
第4行输入要删除的位置
第5行输入要删除的位置
第6行输入要查找的位置
第7行输入要查找的位置

 

输出
数据之间用空格隔开

第1行输出创建后的顺序表内容,包括顺序表实际长度和数据

每成功执行一次操作(插入或删除),输出执行后的顺序表内容

每成功执行一次查找,输出查找到的数据

如果执行操作失败(包括插入、删除、查找等失败),输出字符串error,不必输出顺序表内容

样例输入
6 11 22 33 44 55 66
3 777
1 888
1
9
0
5


样例输出
6 11 22 33 44 55 66
7 11 22 777 33 44 55 66
8 888 11 22 777 33 44 55 66
7 11 22 777 33 44 55 66
error
error
44


提示
第i个位置是逻辑意义的位置,从1开始,在实际编程用数组,从0开始,对应数组i-1位置

直接看代码 

#include<iostream>
#include<cstdlib>
using namespace std;

#define INCREAMENT_SIZE 10
#define ok 1
#define error 0

class SeqList {
private:
	int* list;
	int maxsize;//最大长度
	int size;//实际长度
public:
	SeqList();
	~SeqList();
	int getSize();
	int list_insert(int i, int item);//让item成为第i个元素
	int list_del(int i);//删除第i个元素
	int getElement(int i);//得到第i个元素
	void list_display();//打印list
};

void SeqList::list_display() {
	cout << size << ' ';
	for (int i = 0; i < size; i++) {
		cout << list[i] << ' ';
	}
	cout << endl;
}

int SeqList::getElement(int i) {
	//判断i的可行性
	if (i<1 || i>size) {
		return 0;
	}
	return list[i - 1];
}

int SeqList::list_del(int i) {
	//判断i的可行性
	if (i<1 || i>size) {
		return 0;
	}

	for (int j = i-1; j < size-1; j++) {
		list[j] = list[j + 1];//从前往后遍历赋值
	}
	
	size--;//删除成功
	return 1;
}


int SeqList::list_insert(int i, int item) {
	//判断i的可行性
	if (i<1 || i>size+1) {
		return 0;
	}
	//先判断表还能不能插入元素
	if (size == maxsize) {//已经达到最大元素个数了
		//重新分配空间
		list = (int*)realloc(list, (maxsize + INCREAMENT_SIZE) * sizeof(int));
		if (list == nullptr) {
			exit(1);//分配失败
		}
		maxsize += INCREAMENT_SIZE;//空间多了
	}
	for (int j = size; j >= i; j--) {
		list[j] = list[j - 1];//从后往前遍历赋值空出位置插入
	}
	list[i - 1] = item;
	size++;//插入成功
	return 1;
}

int SeqList::getSize() {
	return size;
}

SeqList::SeqList() {//初始化
	maxsize = 1000;
	size = 0;
	list = (int*)malloc(maxsize * sizeof(int));
	if (list == nullptr) {//分配失败
		exit(1);
	}
}

SeqList::~SeqList() {
	free(list);//释放空间
}

int main() 
{
	SeqList List;
	int n, ele, where;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> ele;
		List.list_insert(i, ele);
	}
	List.list_display();
	for (int i = 0; i < 2; i++) {//插入
		cin >> where >> ele;
		int check = List.list_insert(where, ele);
		if (check==0) {
			cout << "error" << endl;
		}
		else {
			List.list_display();
		}
	}
	for (int i = 0; i < 2; i++) {//删除
		cin >> where;
		int check = List.list_del(where);
		if (check == 0) {
			cout << "error" << endl;
		}
		else {
			List.list_display();
		}
	}
	for (int i = 0; i < 2; i++) {//查找
		cin >> where;
		int check = List.getElement(where);
		if (check == 0) {
			cout << "error" << endl;
		}
		else {
			cout << check << endl;
		}
	}



	return 0;
}

使用c++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZZWWWFFF_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值