C++ 实现基于顺序表(数组) 的 顺序栈 顺序循环队列 二合一实用工具类 泛型类

C++ 实现基于顺序表(数组) 的 顺序栈 顺序循环队列 二合一实用工具类 泛型类

我什么也不想表达,就想直接上代码,就这样
代码很简单,没什么可说的,算是总结一下以前的代码,用泛型的方式实现一遍

调用代码

void printfVectorInt(AlgoVector<int, int> & vec)
{
     
	int size = vec.size();
	printf("size:%d capital:%d\t[",size,vec.capital());
	for (int i = 0; i < size; i++)
	{
     
		printf("%d ",vec[i]);
	}
	printf("]\n");
}
void testVector()
{
     
	//初始化一个数组,大小为8
	AlgoVector<int, int> vec(8);
	//只要不满,一直添加,入栈
	for (int i = 0; i < 100; i++)
	{
     
		if (vec.isFull())
			break;
		vec.push(i);
	}
	printfVectorInt(vec);
	
	//出栈所有
	for (int i = 0; i < 100; i++)
	{
     
		if (vec.isEmpty())
			break;
		vec.pop();
	}
	printfVectorInt(vec);

	//入队,直到满
	for (int i = 0; i < 100; i++)
	{
     
		if (vec.isFull())
			break;
		vec.enque(i);
	}
	printfVectorInt(vec);
	
	//出队全部
	for (int i = 0; i < 100; i++)
	{
     
		if (vec.isEmpty())
			break;
		vec.deque();
	}
	printfVectorInt(vec);
	
	//随机入栈出栈
	for (int i = 0; i < 10; i++)
	{
     
		printf("-------------%d:%d-----------------\n",i,i*10);
		//是否是到随机入栈,机会个一半
		if (rand() % 100 < 50)
		{
     
			for (int j = i * 10; j < i * 10 + 5; j++)
			{
     
				if (vec.isFull() == false)
				{
     
					vec.push(j);
					printf("in:%d\n",vec.tial());
				}
			}
			printf("push:#");
			printfVectorInt(vec);
		}
		else
		{
     
			for (int j = 0; j <3; j++)
			{
     
				if (vec.isEmpty() == false)
				{
     
					printf("ot:%d\n", vec.tial());
					vec.pop();
					
				}
			}
			printf("pop:#");
			printfVectorInt(vec);
		}
	}
	//保证添加几个元素,测试赋值运算符和拷贝构造
	printf("vec:add:0-2\n");
	
	for (int i = 0; i < 3; i++)
	{
     
		vec.push(i);
	}
	printfVectorInt(vec);
	//测试拷贝构造和赋值运算
	printf("#----------------------------#\n");
	AlgoVector<int, int> vec2(vec);
	printfVectorInt(vec2);
	vec2 = vec;
	printfVectorInt(vec2);

	
	printfVectorInt(vec2);
	//重新设置容量测试
	printf("******************************\n");
	bool ret=vec2.setCapital(5);
	printfVectorInt(vec2);
	ret = vec2.setCapital(0);
	printfVectorInt(vec2);
	ret = vec2.setCapital(12);
	printfVectorInt(vec2);

}

及其无聊的测试输出

size:8 capital:9        [0 1 2 3 4 5 6 7 ]
size:0 capital:9        []
size:8 capital:9        [0 1 2 3 4 5 6 7 ]
size:0 capital:9        []
-------------0:0-----------------
in:0
in:1
in:2
in:3
in:4
push:#size:5 capital:9  [0 1 2 3 4 ]
-------------1:10-----------------
ot:4
ot:3
ot:2
pop:#size:2 capital:9   [0 1 ]
-------------2:20-----------------
in:20
in:21
in:22
in:23
in:24
push:#size:7 capital:9  [0 1 20 21 22 23 24 ]
-------------3:30-----------------
in:30
push:#size:8 capital:9  [0 1 20 21 22 23 24 30 ]
-------------4:40-----------------
ot:30
ot:24
ot:23
pop:#size:5 capital:9   [0 1 20 21 22 ]
-------------5:50-----------------
in:50
in:51
in:52
push:#size:8 capital:9  [0 1 20 21 22 50 51 52 ]
-------------6:60-----------------
ot:52
ot:51
ot:50
pop:#size:5 capital:9   [0 1 20 21 22 ]
-------------7:70-----------------
ot:22
ot:21
ot:20
pop:#size:2 capital:9   [0 1 ]
-------------8:80-----------------
ot:1
ot:0
pop:#size:0 capital:9   []
-------------9:90-----------------
pop:#size:0 capital:9   []
vec:add:0-2
size:3 capital:9        [0 1 2 ]
#----------------------------#
size:3 capital:9        [0 1 2 ]
size:3 capital:9        [0 1 2 ]
size:3 capital:9        [0 1 2 ]
******************************
size:3 capital:6        [0 1 2 ]
size:3 capital:6        [0 1 2 ]
size:3 capital:13       [0 1 2 ]
请按任意键继续. . .

实现代码

template<typename T,typename E>
class AlgoVector
{
     
public:
	AlgoVector(E capital)
	{
     
		m_data = NULL;
		InitVector(capital);
	}
	AlgoVector(const AlgoVector& vec)
	{
     
		m_data = NULL;
		copyFromVector(vec);
	}
	AlgoVector& operator=(const AlgoVector& vec)
	{
     
		copyFromVector(vec);
		return *this;
	}
	T& operator[](E index)
	{
     
		return m_data[(m_head+1 + index) % m_capital];
	}
	AlgoVector& push(T data)
	{
     
		m_tial = (m_tial+1) % m_capital;
		m_data[m_tial] = data;
		return *this;
	}
	T pop()
	{
     
		m_tial=(m_tial-1+m_capital)%m_capital;
		return m_data[m_tial];
	}
	T& top()const
	{
     
		return m_data[m_tial];
	}

	bool isFull() const
	{
     
		return (m_tial + 1) % m_capital == m_head;
	}
	bool isEmpty() const
	{
     
		return m_tial == m_head;
	}

	AlgoVector& enque(T data)
	{
     
		m_tial = (m_tial + 1) % m_capital;
		m_data[m_tial] = data;
		return *this;
	}
	T deque()
	{
     
		m_head = (m_head+1) % m_capital;
		return  m_data[m_head];
	}

	T& head()const
	{
     
		return m_data[(m_head + 1)%m_capital];
	}
	T& tial()const
	{
     
		return m_data[m_tial];
	}
	E size()const
	{
     
		return (m_tial + m_capital - m_head) % m_capital;
	}
	E capital() const
	{
     
		return m_capital;
	}
	AlgoVector& clear()
	{
     
		m_head = 0;
		m_tial = 0;
		return *this;
	}
	bool setCapital(E capital)
	{
     
		if (capital == 0)
			return false;

		if (capital < size())
			return false;

		++capital;
		T * srcData = m_data;
		E srcHead = m_head;
		E srcTial = m_tial;
		E srcCapital = m_capital;

		m_capital = capital;
		m_data = new T[capital];
		m_head = 0;
		m_tial = 0;


		while (true)
		{
     
			if (srcHead == srcTial)
				break;
			srcHead = (1 + srcHead) % srcCapital;
			m_tial = (1 + m_tial) % m_capital;
			m_data[m_tial] = srcData[srcHead];
		}

		delete[] srcData;

		return true;
	}
private:
	T * m_data;
	E m_capital;

	E m_head;
	E m_tial;
	void copyFromVector(const AlgoVector & vec)
	{
     
		E srcHead = vec.m_head;
		E srcTial = vec.m_tial;
		
		if (m_data!=NULL)
			delete[] m_data;

		m_capital = vec.m_capital;
		m_data = new T[vec.m_capital];
		m_head = 0;
		m_tial = 0;


		while (true)
		{
     
			if (srcHead == srcTial)
				break;
			srcHead = (1 + srcHead) % vec.m_capital;
			m_tial=(1+m_tial) % m_capital;
			m_data[m_tial] = vec.m_data[srcHead];
		}
	}
	void InitVector(E capital = 64)
	{
     
		if (m_data != NULL)
		{
     
			delete[] m_data;
			m_data = NULL;
		}
		capital++;
		m_data = new T[capital];
		m_head = 0;
		m_tial = 0;
		m_capital = capital;
	}
};

你可能感兴趣的:(C++,开发学习,顺序表,泛型类,顺序栈,顺序循环队列)