以下内容源于慕课网的学习整理,如有侵权,请告知删除。
1、栈
- 后进先出(LIFO)
2、单一数据类型栈
MyStack.h
//在MyStack.h文件中
#ifndef MYSTACK_H
#define MYSTACK_H
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(); //遍历栈中所有元素
private:
char *m_pBuffer; //栈空间指针
int m_iSize; //栈容量
int m_iTop; //栈顶(栈中元素个数)
};
#endif // !MYSTACK_H
MyStack.cpp
//MyStack.cpp文件中
#include <iostream>
#include "MyStack.h"
using namespace std;
MyStack::MyStack(int size)
{
m_iSize = size;
m_iTop = 0;
m_pBuffer = new char[size];
}
MyStack::~MyStack()
{
delete[]m_pBuffer;
}
bool MyStack::StackEmpty()
{
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;
}
void MyStack::StackTraverse()
{
for (int i = 0; i < m_iTop; i++)
{
cout << m_pBuffer[i] << ",";
}
}
demo.cpp
//demo.cpp文件中
#include <iostream>
#include "MyStack.h"
//#include"demo.h"
using namespace std;
int main()
{
MyStack *p = new MyStack(5);
p->Push('h');
p->Push('e');
p->Push('l');
p->Push('l');
p->Push('o');
p->StackTraverse();
char elem = '0';
p->pop(elem);
if (p->StackEmpty())
{
cout << "空栈" << endl;
}
if (p->StackFull())
{
cout << "满栈" << endl;
}
cout << "栈中元素个数:" << p->StackLength() << endl;
p->StackTraverse();
delete p;
p = NULL;
return 0;
}
3、栈模板
MyStack.h
//在MyStack.h文件中
#ifndef MYSTACK_H
#define MYSTACK_H
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(); //遍历栈中所有元素
private:
T *m_pBuffer; //栈空间指针
int m_iSize; //栈容量
int m_iTop; //栈顶(栈中元素个数)
};
template <typename T>
MyStack<T>::MyStack(int size)
{
m_iSize = size;
m_iTop = 0;
m_pBuffer = new T[size];
}
template <typename T>
MyStack<T>::~MyStack()
{
delete[]m_pBuffer;
}
template <typename T>
bool MyStack<T>::StackEmpty()
{
if (m_iTop == 0)
{
return true;
}
return false;
}
template <typename T>
bool MyStack<T>::StackFull()
{
if (m_iTop == m_iSize)
{
return true;
}
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()
{
for (int i = 0; i < m_iTop; i++)
{
cout << m_pBuffer[i];
}
}
#endif // !MYSTACK_H
Test.h
//在Test.h文件中
#ifndef TEST_H
#define TEST_H
#include<ostream>
using namespace std;
class Coordinate
{
friend ostream &operator<<(ostream &out, Coordinate &coor);
public:
Coordinate(int x=0,int y=0);
~Coordinate();
private:
int m_iX;
int m_iY;
};
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
Coordinate::~Coordinate()
{
}
ostream &operator<<(ostream &out, Coordinate &coor)
{
out << "(" << coor.m_iX << "," << coor.m_iY << ") ";
return out;
}
#endif // !TEST_H
demo.cpp
//在Demo.cpp文件中
#include <iostream>
#include "MyStack.h"
#include "Test.h"
using namespace std;
int main()
{
MyStack<Coordinate> *p = new MyStack<Coordinate>(5);
Coordinate c1 = Coordinate(5, 8);
p->Push(Coordinate(3, 2));
p->Push(Coordinate(6, 9));
p->Push(Coordinate(4, 7));
p->Push(Coordinate(2, 5));
p->Push(Coordinate(0, 1));
p->StackTraverse();
p->pop(c1);
if (p->StackEmpty())
{
cout << "空栈" << endl;
}
if (p->StackFull())
{
cout << "满栈" << endl;
}
cout << "栈中元素个数:" << p->StackLength() << endl;
p->StackTraverse();
delete p;
p = NULL;
return 0;
}