栈 - 从零开始实现by C++
参考链接:数据结构探险—栈篇
学了队列之后,栈就很简单了,换汤不换药。
栈
栈的模型
后进先出(电梯,进制转换,括号的匹配检测)
栈的基本元素
栈顶,栈底(一般很少用到),栈容量,栈长度
注意:栈顶一般指向栈最后一个元素的下一位
标准C++代码
//MyStack.h
#pragma once
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;
};
//MyStack.cpp
#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;
}
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
#include<iostream>
#include"MyStack.h"
using namespace std; int main()
{
MyStack *p = new MyStack(5); p->push('h');
p->push('e');
p->push('l');
p->push('i');
p->push('z'); p->stackTraverse(); char tmp = 0;
p->pop(tmp);
cout << endl << "ding is " << tmp << endl; p->clearStack(); cout << p->stackLength() << endl; if (p->stackEmpty())
{
cout << "stack is empty" << endl;
}
if (p->stackFull())
{
cout << "stack is full" << endl;
}
delete p;
p = nullptr;
return 0;
}
改变栈元素的类型
#pragma once 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;
}
#include<iostream>
#include"MyStack.h"
using namespace std; /******************************************/
/*
栈类
要求:
1.定义Coordinate坐标类
2.改造栈类,使其可以适用于坐标类
目的:灵活掌握栈机制,理解抽象数据类型在栈中的应用
*/
/******************************************/ int main()
{
MyStack *p = new MyStack(5); p->push(Coordinate(1,2));
p->push(Coordinate(3,4)); p->stackTraverse(); Coordinate tmp = 0;
p->pop(tmp);
cout << endl << "tmp is ";
tmp.printCoordinate(); p->clearStack(); cout << "Length: " << p->stackLength() << endl; if (p->stackEmpty())
{
cout << "empty" << endl;
}
if (p->stackFull())
{
cout << "full" << endl;
} delete p;
p = nullptr;
return 0;
}
类模板重写栈
#pragma once
#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;
};
#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;
}
#pragma once 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(); //遍历栈中所有元素
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;
m_pBuffer = nullptr;
} template <typename T>
bool MyStack<T>::stackEmpty()//判定栈是否为空,为空返回true,非空返回false
{
if (0 == m_iTop)
{
return true;
}
return false;
} template <typename T>
bool MyStack<T>::stackFull() //判定栈是否为满,为满返回true,不满返回false
{
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++)
{
//m_pBuffer[i].printCoordinate();//可以使用运算符重载
cout << m_pBuffer[i];
}
}
#include<iostream>
#include"MyStack.h"
using namespace std;
#include"Coordinate.h" /******************************************/
/*
栈 类模板
要求:
将普通栈改造为类模板栈,使其可以适用于任何数据类型
目的:灵活掌握栈机制,理解抽象数据类型在栈中的应用
*/
/******************************************/ int main()
{
MyStack<Coordinate> *p = new MyStack<Coordinate>(5); p->push(Coordinate(1,2));
p->push(Coordinate(3,4)); p->stackTraverse(); Coordinate tmp = 0;
p->pop(tmp);
cout << endl << "tmp is ";
tmp.printCoordinate(); p->clearStack(); cout << "Length: " << p->stackLength() << endl; if (p->stackEmpty())
{
cout << "empty" << endl;
}
if (p->stackFull())
{
cout << "full" << endl;
} delete p;
p = nullptr;
return 0;
}
实际应用一:进制转换
实例应用二:括号匹配
栈 - 从零开始实现by C++的更多相关文章
- 《从零开始做一个MEAN全栈项目》(1)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在本系列的开篇,我打算讲一下全栈项目开发的优势,以及MEAN项目各个模块的概览. 为什么选择全栈开发? ...
- 从零开始写STL—栈和队列
从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...
- 《从零开始做一个MEAN全栈项目》(3)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 上一篇文章给大家讲了一下本项目的开发计划,这一章将会开始着手搭建一个MEAN项目.千里之行,始于足下, ...
- 《从零开始做一个MEAN全栈项目》(2)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 上一节简单介绍了什么是MEAN全栈项目,这一节将简要介绍三个内容:(1)一个通用的MEAN项目的技 ...
- [转贴] 从零开始学C++之异常(二):程序错误、异常(语法、抛出、捕获、传播)、栈展开
一.程序错误 编译错误,即语法错误.程序就无法被生成运行代码. 运行时错误 不可预料的逻辑错误 可以预料的运行异常 例如: 动态分配空间时可能不会成功 打开文件可能会失败 除法运算时分母可能为0 整数 ...
- 从零开始学C++之数据封装与抽象:分别用C和C++来实现一个链栈
下面通过分别用C和C++来实现一个链栈(链表实现),从中体会数据封装抽象的思想: C语言实现: C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- 从零开始的全栈工程师——html篇1
全栈工程师也可以叫web 前端 H5主要是网站 app 小程序 公众号这一块 HTML篇 html(超文本标记语言,标记通用标记语言下的一个应用.) “超文本”就是指页面内可以包含图片.链接,甚至音乐 ...
- 从零开始的全栈工程师——JS面向对象(复习)
作用域 栈内存:js执行的环境堆内存:存放代码块的空间 存放方式 键值对形式存放 字符串的形式存放js在执行之前 浏览器会给他一个全局作用域叫window 每个作用域下都分为两个模块 一个是内存模块一 ...
- 从零开始的全栈工程师——js篇2.5
数据类型与全局属性 js的本质就是处理数据 数据来自于后台的数据库所以变量就起到一个临时存储数据的这作用ECMAscirpt 制定了js的数据类型 一.数据类型 1.基本数据类型 基本数据类型就是简单 ...
随机推荐
- 用re-sign.jar重签名apk后安装失败的解决办法
问题 打开re-sign.jar,将下载好的apk拖入re-sign.jar的界面进行重签名.重签名成功后,通过adb intall命令安装重签名后的apk文件失败.提示:Failure [INSTA ...
- eclipse 下调整jdk和tomcat的jvm参数
eclipse 下调试和运行,往往会出现调整java.lang.OutOfMemoryError: Java heap space 产生的原因我猜测是使用了maven,subversion,myla ...
- 11个让你吃惊的 Linux 终端命令
原文:http://linux.about.com/od/commands/tp/11-Linux-Terminal-Commands-That-Will-Rock-Your-World.htm 作者 ...
- Java-基础编程(螺旋矩阵&乘法表)
package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...
- 动态加载JS
<script> Date.prototype.format = function(format) { var o = { , //month "d+" : this. ...
- Cheatsheet: 2013 08.20 ~ 08.31
.NET Protobuf-net: the unofficial manual 5 Common C# Misconceptions What is new in the Mono project ...
- 【转载】.NET面试题系列[0] - 写在前面
原文:.NET面试题系列[0] - 写在前面 索引: .NET框架基础知识[1] - .NET框架基础知识(1) http://www.cnblogs.com/haoyifei/p/5643689.h ...
- HTML Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用?
X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的.通过在meta中设置X-UA-Compatible的值,可以指定网页的兼容性模式设置. 在网页中指定的模式优 ...
- BP神经网络求解异或问题(Python实现)
反向传播算法(Back Propagation)分二步进行,即正向传播和反向传播.这两个过程简述如下: 1.正向传播 输入的样本从输入层经过隐单元一层一层进行处理,传向输出层:在逐层处理的过程中.在输 ...
- Find Current Job Openings For Oracle Forms & Reports
Oracle Form & Reports developer jobs are always in demand, candidates who have Oracle D2k, Oracl ...