线性表的class实现
线性表的class实现
hello我是爱和九九,一只快乐的小萌新猿~
 今天,是数据结构开课的第八天,也终于开始留作业啦!
 暑假学的都太零散,所以现在要跟着老师的进度,写我的博客。
 今晚做了数据结构作业——实现一个线性表的类,emmm……调了好几个自作多情的bug,把代码粘在下面吧。
 其中,图片是我的测试过程,而main()函数里面的内容是用来测试我书写的类的。
 
代码如下:(我这次写注释了!!!!!)
#include <iostream>
#include<string>
#include<assert.h>
using namespace std;
template<class T>
class LinearList {
private:
	T *arr;
	int capcity;
	int length;
public:
	//构造函数
	LinearList(int cap) {
		assert(cap >= 1);
		capcity = cap;
		length = 0;
		arr = new T[capcity];
	}
	~LinearList() { delete[]arr; }
	//得到数组的长度
	int size() { return length; }
	//得到数组的容量
	int capcityOfthis() { return capcity; }
	//得到相应索引对应的数据元素
	T get(int index) { return arr[index]; }
	//复制构造函数
	LinearList(LinearList &LLL) {
		length = LLL.size();
		capcity = LLL.capcityofthis();
		for (int i = 0; i < length; i++)
			arr[i] = LLL.get(i);
	}
	bool isempty() { return length == 0; }
	//找到对应元素的索引
	int indexOf(T x) {
		bool find = false;
		for (int i = 0; i < length; i++) {
			if (arr[i] == x) {
				find = true;
				return i;
			}
		}
		if (!find)
			return -1;
	}
	//删除指定元素
	void erase(int index) {
		if (index > length) {
			cout << "没有这个位置的元素,删个球球?" << endl; return;
		}
		for (int i = index; i < length - 1; i++) {
			arr[i] = arr[i + 1];
		}
		length--;
	}
	//插入指定元素
	void insert(int index, T x) {
		if (index >= capcity || length == capcity) {
			cout << "没地方了,要么扩容,要么滚。" << endl; return;
		}
		else if (index < 0) {
			cout << "滚!" << endl; return;
		}
		else if  (index >= length && length + 1 <= capcity) {
			arr[length] = x;
			length++;
			return;
		}
		else {
			length++;
			for (int i = length-1; i > index; i--)
				arr[i] = arr[i - 1];
			arr[index] = x;
		}
	}
	//    //用一个数组初始化线性表
	//    LinearList(T* x,int n){
	//        capcity=n*2;
	//        length=n;
	//    for(int i=0;i<length;i++)
	//        arr[i]=x[i];
	//    }
		//扩容
	void extendcapcity(int newcapcity) {
		if (newcapcity < capcity) {
			cout << "分不清大小的傻大个没有资格敲代码。" << endl; return;
		}
		capcity = newcapcity;
		T*a = new T[newcapcity];
		for (int i = 0; i < length; i++) {
			a[i] = arr[i];
		}
		delete []arr;
		arr = a;
	}
	//缩容
	void intendcapcity(int newcapcity) {
		if (length > newcapcity) {
			cout << "特么你缩容之后东西会丢!!!" << endl; return;
		}
		else {
			capcity = newcapcity;
			T*a = new T[newcapcity];
			for (int i = 0; i < length; i++) {
				a[i] = arr[i];
			}
			delete arr;
			arr = a;
		}
	}
	//打印整个线性表
	void output() {
		for (int i = 0; i < length; i++)cout << arr[i] << " ";
	}
};
int main()
{
	//测试这个类
	LinearList<string> student(8);
	cout << "先输入6个元素,利用insert" << endl;
	for (int i = 0; i <= 5; i++) {
		string name; cin >> name;
		student.insert(i, name);
		cout << student.size() << endl;
		student.output(); cout << endl;
	}
	cout << "接下来我将在第二个元素后面插入三个元素,而第三个没有办法插进去。" << endl;
	string name1, name2, name3; cin >> name1>>name2>>name3;
	student.insert(2, name1);
	cout << student.size() << endl;
	student.insert(2, name2);
	cout << student.size() << endl;
	student.insert(2, name3);
	cout << endl;
	cout << student.size() << endl;
	cout << endl;
	student.output();
	cout << "我要扩容了!!!" << endl;
	cout << "目前的容量是:" << student.capcityOfthis() << endl;
	student.extendcapcity(4); cout << endl;
	cout << "目前的容量是:" << student.capcityOfthis() << endl;
	student.extendcapcity(16);
	cout << "目前的容量是:" << student.capcityOfthis() << endl;
	cout << student.size() << endl;
	student.output(); cout << endl;
	cout << "再次在第二项后面插入一个元素" << endl;
	cin >> name3;
	student.insert(2, name3);
	cout << student.size() << endl;
	student.output(); cout << endl;
	cout << "现在删除第二和第三项元素" << endl;
	student.erase(2);
	student.erase(1);
	cout << endl;
	cout << student.size() << endl;
	student.output();
	cout << endl<<"测试完成,熄灯睡觉。" << endl << endl;
	return 0;
}
点个关注,一起编织世界吧!!!
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号