动态数组,c++实现

在郝斌老师的课程里,动态数组的内存分配是由malloc()函数实现,释放是由free()函数实现。本文c++是用new实现,但这里也记录一下这个函数的用法,先给出示例代码:

int *p = (int *)malloc(sizeof(int)*4);

代码中sizeof(int)*4表示申请动态内存为4个int类型数据的大小,也代表所分配整型数组长度为4,(int *)将malloc返回的指针进行强制类型转换,表明返回的地址是整形地址,p指针在加一操作时就可以指向第二个整型元素。

本文利用c++在堆区开辟一个动态数组,实现判满(full)、追加(append)、插入(insert)、删除(del)、升序(sort)、倒置(invers)、打印(show)功能。

首先创建一个arr的类,该类中保存了数组的三个属性,camp为该数组的容量,cnt为当前数组中保存的元素个数(初始化为0),*p为该数组首个元素指针(也就是该数组本身)。然后利用带camp参数的构造函数实现在堆区开辟一个数组,利用析构函数实现对堆区数据的释放。

class arr {
public:
	arr(int camp) {
		this->camp = camp;
		this->p = new int[camp];
	}

	~arr() {
		if (this->p != nullptr) {
			delete [] this->p;
			this->p = nullptr;
		}
	}
	int* p;
	int camp;
	int cnt=0;
};

然后为了锻炼c++知识,将判满、追加、插入作为类方法在类内实现,而其他函数在类外,并分别使用引用传递和指针传递。

class arr {
public:
	arr(int camp) {
		this->camp = camp;
		this->p = new int[camp];
	}

	~arr() {
		if (this->p != nullptr) {
			delete [] this->p;
			this->p = nullptr;
		}
	}
	int* p;
	int camp;
	int cnt=0;

	//判断是否满了
	bool full() {
		if (this->cnt< this->camp) return true;
		else return false;
	};

	//追加
	void append(int val) {

		if (full()) {
			cout << "成功追加:" << val << endl;
			this->p[this->cnt] = val;
			this->cnt++;
			return;
		}
		else {
			cout << "追加失败:" << val << endl;
			return;
		}
	};
    
    	//插入:索引前插入
	void insert(int pos, int val) {
		if (this->cnt < this->camp) {
			for (int j = this->cnt; j >= pos; j--) {
				this->p[j] = this->p[j - 1];
			}
			this->p[pos - 1] = val;
			this->cnt++;
			cout << "在位置" << pos << "处成功插入:" << val << "! ";
		}
		else {
			cout << "数组已满,无法插入" << endl;
		}
	}

};

类外函数如下:

//删除指定位置元素(引用传递)
void del(arr& arr,int pos) {
	int x = arr.p[pos - 1];
	if (arr.cnt != 0) {
		for (int j = pos ; j < arr.cnt; j++) {
			arr.p[j-1] = arr.p[j];
		}
		cout << "删除成功,删除的数为:" << x <<"! ";
		arr.cnt--;
	}
	else {
		cout << "数组元素为0个,删除失败" << endl;
	}
}

//升序排列(引用传递)
void sort(arr& arr) {
	for (int j = 0; j < arr.cnt - 1; j++) {
		for (int i = j + 1; i < arr.cnt; i++) {
			if (arr.p[j] < arr.p[i]) {
				int t = arr.p[j];
				arr.p[j] = arr.p[i];
				arr.p[i] = t;
			}
		}
	}
	cout << "升序成功! ";
	return;
}

//倒置数组(指针传递)
void invers(arr *arr) {
	int i = 0;
	int j = arr->cnt - 1;
	while (i < j) {
		int t = arr->p[i];
		arr->p[i] = arr->p[j];
		arr->p[j] = t;
		i++;
		j--;
	}
	cout << "倒置成功! ";
	return;
}

//打印数组(指针传递)
void show(arr* arr) {
	cout << "数组为:";
	for (int i = 0; i < arr->cnt; i++) {
		cout << arr->p[i] << " ";
	}
	cout << endl << endl;
	return;
}

全部测试代码如下:

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


//camp:容量   cnt:元素个数   *p:数组指针
class arr {
public:
	arr(int camp) {
		this->camp = camp;
		this->p = new int[camp];
	}

	~arr() {
		if (this->p != nullptr) {
			delete [] this->p;
			this->p = nullptr;
		}
	}
	int* p;
	int camp;
	int cnt=0;

	//判断是否满了
	bool full() {
		if (this->cnt< this->camp) return true;
		else return false;
	};

	//追加
	void append(int val) {

		if (full()) {
			cout << "成功追加:" << val << endl;
			this->p[this->cnt] = val;
			this->cnt++;
			return;
		}
		else {
			cout << "追加失败:" << val << endl;
			return;
		}
	};

	//插入:索引前插入
	void insert(int pos, int val) {
		if (this->cnt < this->camp) {
			for (int j = this->cnt; j >= pos; j--) {
				this->p[j] = this->p[j - 1];
			}
			this->p[pos - 1] = val;
			this->cnt++;
			cout << "在位置" << pos << "处成功插入:" << val << "! ";
		}
		else {
			cout << "数组已满,无法插入" << endl;
		}
	}

};




//删除指定位置元素
void del(arr& arr,int pos) {
	int x = arr.p[pos - 1];
	if (arr.cnt != 0) {
		for (int j = pos ; j < arr.cnt; j++) {
			arr.p[j-1] = arr.p[j];
		}
		cout << "删除成功,删除的数为:" << x <<"! ";
		arr.cnt--;
	}
	else {
		cout << "数组元素为0个,删除失败" << endl;
	}
}

//升序排列
void sort(arr& arr) {
	for (int j = 0; j < arr.cnt - 1; j++) {
		for (int i = j + 1; i < arr.cnt; i++) {
			if (arr.p[j] < arr.p[i]) {
				int t = arr.p[j];
				arr.p[j] = arr.p[i];
				arr.p[i] = t;
			}
		}
	}
	cout << "升序成功! ";
	return;
}

//倒置数组
void invers(arr *arr) {
	int i = 0;
	int j = arr->cnt - 1;
	while (i < j) {
		int t = arr->p[i];
		arr->p[i] = arr->p[j];
		arr->p[j] = t;
		i++;
		j--;
	}
	cout << "倒置成功! ";
	return;
}

//打印数组
void show(arr* arr) {
	cout << "数组为:";
	for (int i = 0; i < arr->cnt; i++) {
		cout << arr->p[i] << " ";
	}
	cout << endl << endl;
	return;
}

void test() {
	arr arr1(5);
	arr1.append(1);
	arr1.append(2);
	arr1.append(3);
	arr1.append(4);
	show(&arr1);

	arr1.insert(2, 10);
	show(&arr1);

	del(arr1, 2);
	show(&arr1);

	sort(arr1);
	show(&arr1);

	invers(&arr1);
	show(&arr1);

	return;
}
	

void main() {
	test();
	return;
}

测试效果如下:

 对于详细的算法思路,有需要的小伙伴评论留言后,我会添加笔记!

猜你喜欢

转载自blog.csdn.net/qq_43575504/article/details/129682700