数据结构与算法 1:线性表(顺序表)

#pragma once

#include
using namespace std;

template
class SeqList {
public:
	SeqList(int size = defaultSize);
	~SeqList() { delete[] data; }
	bool insert(int i, T& x);
private:
	T* data;
	int maxSize;
	int last;
	enum{defaultSize=20};
};

template
SeqList::SeqList(int size) {
	maxSize = size>defaultSize ? size : defaultSize;
	data = new T[maxSize];
	last = -1;
}

template
bool SeqList::insert(int i, T& x) {
	if (last == maxSize - 1) {
		cout << "数组满" << endl;
		return false;
	}
	if (i<0 || i>last+1) {
		cout << "插入位置非法" << endl;
		return false;
	}
	for (int j = last;j >= i;j--)
		data[j + 1] = data[j];
	data[i] = x;
	last++;
	return true;
}

#include "seqList.h"

int main() {
	SeqList s;

	for (int i = 1;i < 10;i++) 
		s.insert(0, i);

	return 0;
}


进阶版:

#pragma once

#include
using namespace std;

template
class SeqList {
	friend ostream& operator<< (ostream&, SeqList&);
public:
	SeqList(int size = defaultSize);
	SeqList(const SeqList& s);
	~SeqList() { if(data!=nullptr) delete[] data; }
	bool insert(int i, T& x);
	bool remove(int i,T& x);
	int getSize() const { return maxSize; }
	int getLength() const{ return last + 1; }
	bool getItem(int, T&) const;
	int search(T& x) const;

private:
	T* data;
	int maxSize;
	int last;
	enum{defaultSize=20};
	void resize(int size);
};

template
SeqList::SeqList(int size) {
	maxSize = size>defaultSize ? size : defaultSize;
	data = new T[maxSize];
	last = -1;
}

template
SeqList::SeqList(const SeqList& s) {
	maxSize = s.maxSize;
	data = new T[maxSize];
	last = s.last;
	for (int i = 0;i <= last;i++)
		data[i] = s.data[i];

}


template
bool SeqList::insert(int i, T& x) {
	if (i<0 || i>last + 1) {
		cout << "插入位置非法" << endl;
		return false;
	}

	if (last == maxSize - 1) {
		resize(maxSize * 2);
	}
	
	for (int j = last;j >= i;j--)
		data[j + 1] = data[j];
	data[i] = x;
	last++;
	return true;
}

template
void SeqList::resize(int size) {
	T* newData = new T[size];
	for (int i = 0;i <= last;i++)
		newData[i] = data[i];
	delete[] data;
	data = newData;
	maxSize = size;
 }

template
bool SeqList::getItem(int i, T& x) const {
	if (i<0 || i>last)
		return false;
	x = data[i];
	return true;
}

template
int SeqList::search(T& x)const {
	for (int i = 0;i <= last;i++) {
		if (data[i] == x)
			return i;
	}
	return -1;
}

template
bool SeqList::remove(int i,T& x) {
	if (last < 0)
		return false;

	if (i<0 || i>last)
		return false;

	x = data[i];

	for (int j = i;j < last;j++)
		data[j] = data[j + 1];
	last--;
	return true;
}


template
ostream& operator<<(ostream& os, SeqList& s) {
	for (int i = 0;i <= s.last;i++)
		os << s.data[i] << " ";
	return os;
}

#include "seqList.h"

int main() {
	SeqList A, B;

	for (int i = 1;i <= 4;i++)
		A.insert(A.getLength(), i);

	for (int i = 3;i <= 5;i++)
		B.insert(B.getLength(), i);

	cout << "A=" << A << endl;
	cout << "B=" << B << endl;

	int aLen = A.getLength();
	int bLen = B.getLength();

	for (int i=0;i < bLen;i++) {
		int x;
		B.getItem(i, x);
		if (A.search(x) < 0) {
			A.insert(0, x);
		}
	}

	cout << "A=" << A << endl;



	/*int x;
	s.remove(0, x);
	cout << s << endl;
	cout << x << endl;

*/

	//int x;
	//for (int i = 0;i < s.getLength();i++) {
	//	s.getItem(i, x);
	//	cout << x << " ";
	//}
	//cout << endl;

	//x = 6;
	//cout << s.search() << endl;

	return 0;
}

你可能感兴趣的:(数据结构与算法,c++,算法,数据结构)