c++类模板实现栈

特点:先进后出

栈要素:栈顶(会改变),栈底(不会变)
栈不是堆

栈的代码

使用类模板实现栈
MyStack.h

#ifndef MYSTACK_H
#define MYSTACK_H


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



};
#include "MyStack.h"
#include <iostream>
using namespace std;
template <typename T>
MyStack<T>::MyStack(int size)
{
	m_iSize = size;
	m_pBuffer = new T[m_iSize];
	m_iTop = 0;
}
template <typename T>
MyStack<T>::~MyStack()
{
	delete[] m_pBuffer;
}
template <typename T>
bool MyStack<T>::stackEmpty()
{
	return 0 == m_iTop ? true : false;  //反着写,在误操作双等号写成赋值时,系统会报错
}
template <typename T>
bool MyStack<T>::stackFull()
{
	return m_iTop == m_iSize ? true : 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 ifFromBottom)
{
	if (ifFromBottom)
	{
		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] << ",";
			m_pBuffer[i].printCoordinate();
		}
	}
}

#endif // !MYSTACK_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_H
#pragma once

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;
	// TODO: 在此处插入 return 语句
}

demo.cpp

#include <iostream>
#include "MyStack.h"
#include <stdlib.h>
#include "Coordinate.h"
using namespace std;
/*
栈
MyStack(int size);   //分配内存初始化栈空间,设定栈容量,栈顶
~MyStack();          //回收栈空间内存
bool stackEmpty();   //判定栈是否为空,为空返回true,非空返回false
bool stackFull();    //判定栈是否已满,为满返回true,不满返回false
void clearStack();   //清空栈
int stackLength();   //已有元素的个数
bool push(char elem); //元素入栈,栈顶上升
bool pop(char &elem); //元素出栈,栈顶下降
void stackTraverse(bool ifFromBottom);  //遍历栈中所有元素
目的:掌握栈的实现原理和运行机制
*/
int main() {
	MyStack<Coordinate>* pStack = new MyStack<Coordinate>(5);

	pStack->push(Coordinate(1,2));
	pStack->push(Coordinate(5,6));

	pStack->stackTraverse(true);

	pStack->stackTraverse(false);

	//pStack->clearStack();

	cout << pStack->stackLength() << endl;
	
	if (pStack->stackEmpty())
	{
		cout << "栈为空" << endl;
	}
	if (pStack->stackFull())
	{
		cout << "栈为满" << endl;
	}
	delete pStack;
	pStack = NULL;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xgy123xx/article/details/89111959
今日推荐