代码部分测试,等整个系列手写完再完整测试

数组

数组写的不多,刚开始比较懒

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

//void del_vec_foreach(vector<int>& vec) {
//	for (vector<int>::iterator it = vec.begin(); it != vec.end();) {
//		if (*it == 3) {
//			vec.erase(it++);
//		}
//		else {
//			++it;
//		}
//	}
//}

void del_vec_foreach(vector<int>& vec) {
	for (vector<int>::iterator it = vec.begin(); it != vec.end();) {
		if (*it == 3) {
			it = vec.erase(it);
		}
		else {
			++it;
		}
	}
}


void bubbing_sort(vector<int>& vec) {
	int sz = vec.size();

	if (sz < 2)
		return;

	int flag = 0,temp = 0,stop = sz;

	while (1) {
		for (int i = 0, j = 1; j < stop; i++, j++) {

			if (vec[i] > vec[j]) {
				temp = vec[i];
				vec[i] = vec[j];
				vec[j] = temp;

				flag++;
			}
		}
	}
	if (0 == flag)
		return;
	else {
		stop = flag;
		flag = 0;
	}
}




int main() {
	vector<int> test = { 3,3,3,3,3};
	del_vec_foreach(test);

	for (vector<int>::iterator it = test.begin(); it != test.end(); it++) {
		cout << *it << endl;
	}
}

栈与队列

#include<iostream>
#include<vector>

using namespace std;

//从vector创建栈
class stack {
private:
	vector<int> content;
	int top;

public:
	stack() {
		top = -1;
	}

	bool isempty(){ return -1 == top; }

	int get_top() {
		if (isempty()) {
			return content[top];
		}
		else
			return NULL;
	}

	void push(int value) { 
		content.push_back(value);
		top++;
	}

	int pop() {
		if (!isempty()) {
			int res = content[top];
			content.pop_back();
			top--;
			return res;
		}
		else
			return NULL;
	}
};


//从vector创建队列
class queue {
private:
	int first;
	int last;
	vector<int> content;

public:
	queue() {
		first = 0;
		last = -1;
	}

	bool isempty() { return first > last; }
	bool only_one() { return first == last; }

	void push(int value) {
		content.push_back(value);
		last++;
	}

	int pop() {
		if (!isempty()) {
			int res = content[first];
			content.pop_back();
			first++;
			return res;
		}
		else
			return NULL;
	}

	int get_top() {
		if (isempty()) {
			return content[first];
		}
		else
			return NULL;
	}
};

//两个栈实现队列
class stack_to_queue {
private:
	stack *s1, *s2;
	int flag;
public:
	stack_to_queue() {
		flag = 0;
		s1 = new stack();
		s2 = new stack();
	}

	void push(int value) {
		s1->push(value);
	}

	int pop() {
		if (s2->isempty()) {
			if(!s1->isempty()){
				while (!s1->isempty()) {
					s2->push(s1->pop());
				}
				int res = s2->pop();
				return res;
			}
			else
				return NULL;
		}
		else {
			int res = s2->pop();
			return res;
		}
	}

	int get_top() {
		if (!s2->isempty()) {
			return s2->get_top();
		}
		else {
			if (!s1->isempty()) {
				while (!s1->isempty()) {
					s2->push(s1->pop());
				}
				return s2->get_top();
			}
			else
				return NULL;
		}
	}
};

//两个队列实现栈
class queue_to_stack {
private:
	queue* q1, * q2;
	int flag;
public:
	queue_to_stack() {
		flag = 0;	
		q1 = new queue();
		q2 = new queue();
	}

	void push(int value) {
		if (flag) {
			q1->push(value);
		}
		else {
			q2->push(value);
		}
		
	}

	int pop() {
		if (q1->isempty() && q2->isempty())
			return NULL;
		else {
			if (flag) {
				flag--;
				while (!q1->only_one()) {
					q2->push(q1->pop());
				}
				return q1->pop();
			}
			else {
				flag++;
				while (!q2->only_one()) {
					q1->push(q2->pop());
				}
				return q2->pop();
			}
		}
	}

	int get_top() {
		//跟上面改动不多,不写了
	}
};