一.什么是栈
栈是一种机制:后进先出 LIFO:last in first out
二.单一数据类型栈
main函数文件:
#include <iostream>
#include"stdlib.h"
#include"MyStack.h"
using namespace std;
/**********************************************************************************/
/*
栈类
要求
MyStack(int size); //分配初始内存初始化栈空间,设定栈容量,栈顶
~MyStack(); //回收栈空间内存
bool stackEmpty(); //判断栈是否为空
bool stackFull(); //判断栈是否为满
void clearStack(); // 清空栈
int stackLength(); //已有元素个数
void push(char elem); //元素入栈,栈顶上升
char pop(char &elem); //元素出栈,栈顶下降
void stackTraverse(); //比那里栈汇中所有元素
目的:掌握栈的实现原理和运行机制
*/
/***********************************************************************************/
int main(void)
{
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();
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和MyStack.cpp文件:
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; //栈顶,栈中元素个数
};
#include "MyStack.h"
#include<iostream>
using namespace std;
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)
{
return true;
}
else
{
return false;
}
}
bool MyStack::stackFull()
{
if (m_iTop==m_iSize)
{
return true;
}
else
{
return false;
}
}
void MyStack::clearStack()
{
m_iTop = 0;
}
int MyStack::stackLength()
{
return m_iTop;
}
bool MyStack::push(char elem)
{
if (stackFull())
{
return false;
}
else
{
m_pBuffer[m_iTop] = elem;
m_iTop++;
return true;
}
}
/*char MyStack::pop()
{
if (stackEmpty())
{
throw 1;
}
else
{
m_iTop--;
return m_pBuffer[m_iTop];
}
}*/
bool MyStack::pop(char &elem)
{
if (stackEmpty())
{
return false;
}
else
{
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] << ",";
}
}
else
{
for (int i = m_iTop - 1; i >= 0; i--)
{
cout << m_pBuffer[i] << ",";
}
}
}
三.存放复杂数据类型
栈中存放的是对象的时候要做一些修改,具体实现如下代码
main函数文件:
#include <iostream>
#include"stdlib.h"
#include"MyStack.h"
using namespace std;
/**********************************************************************************/
/*
栈类
要求
MyStack(int size); //分配初始内存初始化栈空间,设定栈容量,栈顶
~MyStack(); //回收栈空间内存
bool stackEmpty(); //判断栈是否为空
bool stackFull(); //判断栈是否为满
void clearStack(); // 清空栈
int stackLength(); //已有元素个数
void push(char elem); //元素入栈,栈顶上升
char pop(char &elem); //元素出栈,栈顶下降
void stackTraverse(); //比那里栈汇中所有元素
目的:掌握栈的实现原理和运行机制
*/
/***********************************************************************************/
int main(void)
{
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;
}
Coordinate.h和Coordinate.cpp文件:
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
public:
Coordinate(int x=0, int y=0);
void printCoordinate();
private:
int m_iX;
int m_iY;
};
#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;
}
MyStack.h和MyStack.cpp文件:
#include"Coordinate.h"
class MyStack
{
public:
MyStack(int size); //分配内存初始化栈空间,设定栈容量,栈顶
~MyStack(); //回收站空间
bool stackEmpty();
bool stackFull();
void clearStack();
int stackLength();
bool push(Coordinate elem);
bool pop(Coordinate &elem);
void stackTraverse(bool isFromButtom); //遍历栈中所有元素
private:
Coordinate *m_pBuffer; //栈空间指针
int m_iSize; //栈容量
int m_iTop; //栈顶,栈中元素个数
};
#include "MyStack.h"
#include<iostream>
using namespace std;
MyStack::MyStack(int size)
{
m_iSize = size;
m_pBuffer = new Coordinate[size];
m_iTop = 0;
}
MyStack::~MyStack()
{
delete[]m_pBuffer;
}
bool MyStack::stackEmpty()
{
if (0 == m_iTop)
{
return true;
}
else
{
return false;
}
}
bool MyStack::stackFull()
{
if (m_iTop==m_iSize)
{
return true;
}
else
{
return false;
}
}
void MyStack::clearStack()
{
m_iTop = 0;
}
int MyStack::stackLength()
{
return m_iTop;
}
bool MyStack::push(Coordinate elem)
{
if (stackFull())
{
return false;
}
else
{
m_pBuffer[m_iTop] = elem;
m_iTop++;
return true;
}
}
/*char MyStack::pop()
{
if (stackEmpty())
{
throw 1;
}
else
{
m_iTop--;
return m_pBuffer[m_iTop];
}
}*/
bool MyStack::pop(Coordinate &elem)
{
if (stackEmpty())
{
return false;
}
else
{
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();
}
}
}
四.栈模板
main函数文件:
#include <iostream>
#include"stdlib.h"
#include"MyStack.h"
using namespace std;
#include"Coordinate.h"
/**********************************************************************************/
/*
栈类
要求
MyStack(int size); //分配初始内存初始化栈空间,设定栈容量,栈顶
~MyStack(); //回收栈空间内存
bool stackEmpty(); //判断栈是否为空
bool stackFull(); //判断栈是否为满
void clearStack(); // 清空栈
int stackLength(); //已有元素个数
void push(char elem); //元素入栈,栈顶上升
char pop(char &elem); //元素出栈,栈顶下降
void stackTraverse(); //比那里栈汇中所有元素
目的:掌握栈的实现原理和运行机制
*/
/***********************************************************************************/
int main(void)
{
MyStack<char> *pStack = new MyStack<char>(5);
pStack->push('h'); //底
pStack->push('l'); //顶
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;
}
Coordinate.h文件:
#ifndef COORDINATE_H
#define COORDINATE_H
#include<ostream>
#include<iostream>
using namespace std;
class Coordinate
{
friend ostream &operator<<(ostream &out, Coordinate &coor);
public:
Coordinate(int x = 0, int y = 0)
{
m_iX = x;
m_iY = y;
}
void printCoordinate()
{
cout << "(" << m_iX << "," << m_iY << ")" << endl;
}
private:
int m_iX;
int m_iY;
};
ostream &operator<<(ostream &out, Coordinate &coor)
{
out << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
return out;
}
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(bool isFromButtom); //遍历栈中所有元素
private:
T *m_pBuffer; //栈空间指针
int m_iSize; //栈容量
int m_iTop; //栈顶,栈中元素个数
};
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;
}
template <typename T>
bool MyStack<T>::stackEmpty()
{
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;
}
else
{
m_pBuffer[m_iTop] = elem;
m_iTop++;
return true;
}
}
/*
template <typename T>
char MyStack::pop()
{
if (stackEmpty())
{
throw 1;
}
else
{
m_iTop--;
return m_pBuffer[m_iTop];
}
}*/
template <typename T>
bool MyStack<T>::pop(T &elem)
{
if (stackEmpty())
{
return false;
}
else
{
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];
//m_pBuffer[i].printCoordinate();
}
}
else
{
for (int i = m_iTop - 1; i >= 0; i--)
{
cout << m_pBuffer[i];
// m_pBuffer[i].printCoordinate();
}
}
}
五.应用——进制转换
mian函数文件:(MyStack.h为栈模板,前面例子有)
#include <iostream>
#include"stdlib.h"
#include"MyStack.h"
using namespace std;
/**********************************************************************************/
/*
栈应用——数据转换
描述:输入任意的十进制正整数N,分别输出改正数N的二进制、八进制、十六进制的数
公式:N=(N div d)*d+N mod d(div表示整除,mod表示求余)
(1348)(十进制)=(2504)(八进制)=(544)(十六进制)=(10101000100)(二进制)
*/
/***********************************************************************************/
#define BINARY 2
#define OCTONARY 8
#define HEXADECIMAL 16
int main(void)
{
char num[] = "0123456789ABCDEF";
MyStack<int> *pStack = new MyStack<int>(20);
int N = 2019;
int mod = 0;
while (N != 0)
{
mod = N % HEXADECIMAL;
pStack->push(mod);
N = N / HEXADECIMAL;
}
//pStack->stackTraverse(false);
//for (int i = p->pStack->stackTraverse(false)-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 <iostream>
#include"stdlib.h"
#include"MyStack.h"
using namespace std;
#include"Coordinate.h"
/**********************************************************************************/
/*
栈应用——括号匹配
描述:任意输入一组括号,可以判断括号是否匹配
字符串示例:[()] [()()] [()[()]] [[()]
*/
/***********************************************************************************/
#define BINARY 2
#define OCTONARY 8
#define HEXADECIMAL 16
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;
pStack = NULL;
system("pause");
return 0;
}