[数据结构] 通用栈的写法以及案例(使用template模板)

栈的介绍

什么是栈?古代栈就是牲口棚的意思。
栈是一种机制:后进先出 LIFO(last in first out)
比如电梯,先进电梯位于电梯后部,比前面的人得更晚出来。
在这里插入图片描述

通用栈的写法以及案例

案例来自慕课网https://www.imooc.com/u/1349694/courses?sort=publish
首先是通用栈的写法:
MyStack.h:(因为编译器不支持类模板分开编译,所以只用一个.h文件实现栈)

//MyStack.h

#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>
using namespace std;
template <typename 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:
	int m_iTop;         //栈顶,栈中元素个数
	int m_iSize;			  //栈容量
	T *m_pBuffer;		    //栈空间指针
};

template <typename T>
MyStack<T>::MyStack(int size)
{
	m_iSize = size;
	m_pBuffer = new T[size];
	m_iTop = 0;
}
template <typename T>
MyStack<T>::~MyStack()
{
	delete[]m_pBuffer;
	m_pBuffer = NULL;

}
template <typename T>
bool MyStack<T>::stackEmpty()
{
	if (m_iTop == 0)//if(0 == m_iTop)
	{
		return true;
	}
	else
	{
		return false;
	}
}
template <typename T>
bool MyStack<T>::stackFull()
{
	if (m_iTop == m_iSize)//>=
	{
		return true;
	}
	else
	{
		return false;
	}
}
template <typename T>
void MyStack<T>::clearStack()
{
	m_iTop = 0;//栈顶置零。原栈中所有值无效
}
template <typename T>
int MyStack<T>::stackLength()
{
	return m_iTop;
}
template <typename T>
bool MyStack<T>::push(T elem)//放入栈顶
{
	if (stackFull())
	{
		return false;
	}
	m_pBuffer[m_iTop] = elem;

	m_iTop++;        //栈顶上升
	return true;
}
template <typename T>
bool MyStack<T>::pop(T &elem)  //出栈
{
	if (stackEmpty())
	{
		return false;
	}
	m_iTop--;     //出栈需要--,使栈顶指向下一个空位置
	elem = m_pBuffer[m_iTop];
	return true;
}


template <typename 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];
		}
	}

}

#endif

接下来是具体应用:
Coordinate.h:

//Coordinate.h 实现坐标类

#ifndef COORDINATE_H
#define COORDINATE_H
#include <ostream>
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;
};
#endif

Coordinate.cpp:

//Coordinate.cpp

#include "Coordinate.h"
#include <iostream>
using namespace std;

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)
{
	out << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
	return out;
}

main.cpp:

//main.cpp

#include "Mystack.h"
#include <iostream>
#include <stdlib.h>
#include "Coordinate.h"
using namespace std;
int main(void)
{
	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;
	MyStack<char> *pStack2 = new MyStack<char>(5);

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

	pStack2->stackTraverse(true);
	delete pStack;
	pStack = NULL;

	system("pause");
	return 0;
}

运行结果:

(1,2)
(3,4)
(3,4)
(1,2)
2
hello请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值