C++数据结构___栈

数据结构探险—栈篇
C++数据结构___栈_第1张图片
C++实现栈记录

MyStack.h

#pragma once
#include
using namespace std;


class MyStack
{
public:
	MyStack(int size);//分配内存初始化,设定栈容量,栈顶
	~MyStack();//回收栈内存空间
	bool stackEmpty();//判断栈是否为空
	bool stackFull();//判断栈满
	void clearStack();//清空栈
	int stackLength();//已有元素的个数
	bool push(char elem);//元素入栈,栈顶上升
	bool pop(char& elem);//元素出栈,栈顶下降
	void stackTraverse(bool isFromButtom);//遍历栈中所有元素

private:
	char* m_pBuffer;//占空间指针
	int m_iSize;//栈容量
	int m_iTop;//栈顶,栈中元素个数
};

MyStack.cpp

#include"MyStack.h"

//初始化
MyStack::MyStack(int size)
{
	m_iSize = size;
	m_pBuffer = new char[size];
	m_iTop = 0;
}

//析构函数
MyStack::~MyStack()
{
	delete[] m_pBuffer;
}

//判断栈是否为空
bool MyStack::stackEmpty()
{
	if(0 == m_iTop)//if(m_iTop == 0)//区别赋值
	{
		return true;
	}
	return false;
}

//判断栈满
bool MyStack::stackFull()
{
	if (m_iTop == m_iSize)//>=
	{
		return true;
	}
	return false;
}

//清空栈
void MyStack::clearStack()
{
	m_iTop = 0;
}

//已有元素的个数
int MyStack::stackLength()
{
	return m_iTop;
}

//元素入栈,栈顶上升
bool MyStack::push(char elem)
{
	if (stackFull())
	{
		return false;
	}
	m_pBuffer[m_iTop] = elem;
	m_iTop++;
	return true;
}

//元素出栈,栈顶下降
bool MyStack::pop(char& elem)
{
	if (stackEmpty())
	{
		return false;
	}
	m_iTop--;
	elem = m_pBuffer[m_iTop];
	return true;
}

//char MyStack::pop()
//{
//	if (stackEmpty())
//	{
//		throw 1;
//	}
//	else
//	{
//		m_iTop--;
//		return m_pBuffer[m_iTop];
//	}
//}

//遍历栈中所有元素
void MyStack::stackTraverse(bool isFromButtom)
{
	if (isFromButtom)
	{
		for (int i = 0; i < m_iTop; i++)
		{
			cout << m_pBuffer[i] << ",";
		}
	}
	else
	{
		for (int i = m_iTop - 1; i >= 0; i--)
		{
			cout << m_pBuffer[i] << ",";
		}
	}
	
}

demo.cpp

#include
using namespace std;
#include"MyStack.h"


int main()
{
	MyStack* pStack = new MyStack(5);

	pStack->push('h');//底
	pStack->push('e');
	pStack->push('l');
	pStack->push('l');
	pStack->push('o');//顶

	pStack->stackTraverse(true);

	char elem = 0;
	pStack->pop(elem);

	cout << endl;
	cout << elem << endl;

	//pStack->clearStack();

	cout << pStack->stackLength() << endl;

	if (pStack->stackEmpty())
	{
		cout << "栈为空" << endl;
	}

	if (pStack->stackFull())
	{
		cout << "栈为满" << endl;
	}

	delete pStack;
	pStack = NULL;
	system("pause");
	return 0;
}


改造后

Coordinate.h

#pragma once
#include
using namespace std;


class Coordinate
{
public:
	Coordinate(int x, int y);
	void printCoordinate();
private:
	int m_iX;
	int m_iY;
};

Coordinate.cpp

#include"Coordinate.h"

Coordinate::Coordinate(int x = 0, int y = 0)
{
	m_iX = x;
	m_iY = y;
}


void Coordinate::printCoordinate()
{
	cout << "(" << m_iX << "," << m_iY << ")" << endl;
}

MyStack.h修改

public:
    bool push(Coordinate elem);//元素入栈,栈顶上升
	bool pop(Coordinate& elem);//元素出栈,栈顶下降
private:
	Coordinate* m_pBuffer;//占空间指针

MyStack.cpp修改

//初始化
MyStack::MyStack(int size)
{
	m_iSize = size;
	//m_pBuffer = new char[size];
	m_pBuffer = new Coordinate[size];
	m_iTop = 0;
}

//元素入栈,栈顶上升
//bool MyStack::push(char elem)
bool MyStack::push(Coordinate elem)
{
	if (stackFull())
	{
		return false;
	}
	m_pBuffer[m_iTop] = elem;
	m_iTop++;
	return true;
}

//元素出栈,栈顶下降
//bool MyStack::pop(char& elem)
bool MyStack::pop(Coordinate& elem)
{
	if (stackEmpty())
	{
		return false;
	}
	m_iTop--;
	elem = m_pBuffer[m_iTop];
	return true;
}

//遍历栈中所有元素
void MyStack::stackTraverse(bool isFromButtom)
{
	if (isFromButtom)
	{
		for (int i = 0; i < m_iTop; i++)
		{
			//cout << m_pBuffer[i] << ",";
			m_pBuffer[i].printCoordinate();
		}
	}
	else
	{
		for (int i = m_iTop - 1; i >= 0; i--)
		{
			//cout << m_pBuffer[i] << ",";
			m_pBuffer[i].printCoordinate();
		}
	}
	
}

demo.cpp

#include
using namespace std;
#include"MyStack.h"


int main()
{
	MyStack* pStack = new MyStack(5);

	pStack->push(Coordinate(1,2));//底
	pStack->push(Coordinate(3,4));

	pStack->stackTraverse(true);

	pStack->stackTraverse(false);


	cout << pStack->stackLength() << endl;

	if (pStack->stackEmpty())
	{
		cout << "栈为空" << endl;
	}

	if (pStack->stackFull())
	{
		cout << "栈为满" << endl;
	}

	delete pStack;
	pStack = NULL;

	system("pause");
	return 0;
}

高级—类模板

将普通栈改造为类模板栈,使其可以适用于任何数据类型

MyStack.h

#pragma once
#include
using namespace std;

template<class T>
class MyStack
{
public:
	MyStack(int size);//分配内存初始化,设定栈容量,栈顶
	~MyStack();//回收栈内存空间
	bool stackEmpty();//判断栈是否为空
	bool stackFull();//判断栈满
	void clearStack();//清空栈
	int stackLength();//已有元素的个数
	bool push(T elem);//元素入栈,栈顶上升
	bool pop(T& elem);//元素出栈,栈顶下降
	void stackTraverse(bool isFromButtom);//遍历栈中所有元素
private:
	T* m_pBuffer;//占空间指针
	int m_iSize;//栈容量
	int m_iTop;//栈顶,栈中元素个数
};

//初始化
template<class T>
MyStack<T>::MyStack(int size)
{
	m_iSize = size;
	//m_pBuffer = new char[size];
	//m_pBuffer = new Coordinate[size];
	m_pBuffer = new T[size];
	m_iTop = 0;
}

//析构函数
template<class T>
MyStack<T>::~MyStack()
{
	delete[] m_pBuffer;
}

//判断栈是否为空
template<class T>
bool MyStack<T>::stackEmpty()
{
	if (0 == m_iTop)//if(m_iTop == 0)//区别赋值
	{
		return true;
	}
	return false;
}

//判断栈满
template<class T>
bool MyStack<T>::stackFull()
{
	if (m_iTop == m_iSize)//>=
	{
		return true;
	}
	return false;
}

//清空栈
template<class T>
void MyStack<T>::clearStack()
{
	m_iTop = 0;
}

//已有元素的个数
template<class T>
int MyStack<T>::stackLength()
{
	return m_iTop;
}

//元素入栈,栈顶上升
template<class T>
bool MyStack<T>::push(T elem)
{
	if (stackFull())
	{
		return false;
	}
	m_pBuffer[m_iTop] = elem;
	m_iTop++;
	return true;
}

//元素出栈,栈顶下降
template<class T>
bool MyStack<T>::pop(T& elem)
{
	if (stackEmpty())
	{
		return false;
	}
	m_iTop--;
	elem = m_pBuffer[m_iTop];
	return true;
}

//遍历栈中所有元素
template<class T>
void MyStack<T>::stackTraverse(bool isFromButtom)
{
	if (isFromButtom)
	{
		for (int i = 0; i < m_iTop; i++)
		{
			cout << m_pBuffer[i];
		}
	}
	else
	{
		for (int i = m_iTop - 1; i >= 0; i--)
		{
			cout << m_pBuffer[i];
		}
	}
}

Coordinate.h

#pragma once
#include
using namespace std;

class Coordinate
{
	friend ostream& operator<<(ostream& out, Coordinate& coor);
public:
	Coordinate(int x = 0, int y = 0);
	void printCoordinate();
private:
	int m_iX;
	int m_iY;
};

Coordinate.cpp

#include"Coordinate.h"

Coordinate::Coordinate(int x, int y)
{
	m_iX = x;
	m_iY = y;
}

void Coordinate::printCoordinate()
{
	cout << "(" << m_iX << "," << m_iY << ")" << endl;
}

ostream& operator<<(ostream& out, Coordinate& coor)
{
	cout << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
	return out;
}

demo.cpp

#include
using namespace std;
#include"MyStack.h"
#include"Coordinate.h"


int main()
{
	MyStack<Coordinate> *pStack = new MyStack<Coordinate>(5);

	pStack->push(Coordinate(1,2));//底
	pStack->push(Coordinate(3,4));

	pStack->stackTraverse(true);

	pStack->stackTraverse(false);


	cout << pStack->stackLength() << endl;

	if (pStack->stackEmpty())
	{
		cout << "栈为空" << endl;
	}

	if (pStack->stackFull())
	{
		cout << "栈为满" << endl;
	}

	delete pStack;
	pStack = NULL;

	system("pause");
	return 0;
}

栈应用–进制转换

demo.cpp

#include 
#include "stdlib.h"
#include "MyStack.h"
#include "Coordinate.h"

using namespace std;

/*************************************************************************/
/*
	栈应用——数制转换
	描述:输入任意的十进制正整数N,分别输出该整数N的二进制、八进制、十六进制的数
	公式:N=(N div d) * d + N mod d (div表示整除,mod表示求余)
	(1348)(十进制) = (2504)(八进制) = (544)(十六进制) = (10101000100)(二进制)
	短除法
			N				N div 8		N mod 8
			1348		168				4
			168			21				0
			21			2					5
			2				0					2

			N				N div 16		N mod 16
			1348		84				4
			84			5					4
			5				0					5

			目的:通过实例灵活掌握栈机制的使用技巧
	
	*/
/*************************************************************************/

#define BINARY					2
#define OCTONARY			8
#define HEXADECTMAL		16
int main(void)
{
	MyStack<int> *pStack = new MyStack<int>(30);

	int N = 1348;
	int mod = 0;

	while (N != 0)
	{
		mod = N % OCTONARY;
		pStack->push(mod);
		N = N / OCTONARY;
	}

	pStack->stackTraverse(false);
	delete pStack;
	pStack = NULL;
	system("pause");
	return 0;
}

对于十六进制还需改进:

demo.cpp

#define BINARY					2
#define OCTONARY			8
#define HEXADECTMAL		16
int main(void)
{
	char num[] = "0123456789ABCDEF";
	MyStack<int> *pStack = new MyStack<int>(30);

	int N = 2016;
	int mod = 0;

	while (N != 0)
	{
		mod = N % HEXADECTMAL;
		pStack->push(mod);
		N = N / HEXADECTMAL;
	}

// 	pStack->stackTraverse(false);
	
// 	for (int i = pStack->stackLength() - 1; i >= 0; i--)
// 	{
// 		num[pStack[i]]
// 	}

	int elem = 0;
	while (!pStack->stackEmpty())
	{
		pStack->pop(elem);
		cout << num[elem];
	}
	delete pStack;
	pStack = NULL;
	system("pause");
	return 0;
}

应用–括号匹配

#include 
#include "stdlib.h"
#include "MyStack.h"
#include "Coordinate.h"

using namespace std;

/*************************************************************************/
/*
	栈应用——括号匹配
	描述:任意输入一组括号,可以判断括号是否匹配
	字符串示例:[()]		[()()]		[()[()]]		[[()]
	目的:通过实例灵活掌握栈机制的使用技巧
	*/
/*************************************************************************/

int main(void)
{
	MyStack<char> *pStack = new MyStack<char>(30);
	MyStack<char> *pNeedStack = new MyStack<char>(30);

	char str[] = "[()]]";
	char currentNeed = 0;

	for (int i = 0; i < strlen(str); i++)
	{
		if (str[i] != currentNeed)
		{
			pStack->push(str[i]);
			switch (str[i])
			{
				case '[':
					if (currentNeed != 0)
					{
						pNeedStack->push(currentNeed);
					}
					currentNeed = ']';
					break;
				case '(':
					if (currentNeed != 0)
					{
						pNeedStack->push(currentNeed);
					}
					currentNeed = ')';
					break;
				default:
					cout << "字符串括号不匹配" << endl;
					system("pause");
					return 0;
			}
		}
		else
		{
			char elem;
			pStack->pop(elem);
			if (!pNeedStack->pop(currentNeed))
			{
				currentNeed = 0;
			}
		}
	}
	
	if (pStack->stackEmpty())
	{
		cout << "字符串括号匹配" << endl;
	}
	else
	{
		cout << "字符串括号不匹配" << endl;
	}
	delete pStack;
	pStack = NULL;

	delete pNeedStack;
	pNeedStack = NULL;

	system("pause");
	return 0;
}

你可能感兴趣的:(C++)