栈 - 从零开始实现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.基本数据类型 基本数据类型就是简单 ...
随机推荐
- linux 网络通信
网络命令 历史上最早的即时通信 1 write linzhiling (内容) ctrl+D结束 这样一封信就写出去了,注意:只有用户在线才可以发送 2 wall (write all) 给所有的用户 ...
- 2016年12月4日 星期日 --出埃及记 Exodus 20:25
2016年12月4日 星期日 --出埃及记 Exodus 20:25 If you make an altar of stones for me, do not build it with dress ...
- 2016年11月21日 星期一 --出埃及记 Exodus 20:12
2016年11月21日 星期一 --出埃及记 Exodus 20:12 "Honor your father and your mother, so that you may live lo ...
- 开源的49款Java 网络爬虫软件
参考地址 搜索引擎 Nutch Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting, ...
- AsyncTask的注意事项
1.6之前:AsyncTask是串行的 1.6~2.3:改成了并行的,同时最多只能执行5个 从1.6开始,AsyncTask引入了线程池,支持同时执行5个异步任务,也就是说同时只能有5个线程运行,超过 ...
- Bootstrap_基本HTML模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 【leetcode❤python】171. Excel Sheet Column Number
#-*- coding: UTF-8 -*- # ord(c) -> integer##Return the integer ordinal of a one-character string. ...
- netstat -ano,查看已占用端口,结束已被占用的端口,ntsd,关闭任务管理器杀不了的进程
cmd——回车,输入netstat -ano——回车,可以查看已占用的端口,记下端口的PID,然后打开任务管理器,点查看,选择列,勾选PID确定,找到对应的PID,结束进程,如果结束不了或者结束后还不 ...
- freebsd镜像作用和vmware服务开启
第一个是可以引导的光盘,只能引导系统,通常用于网络安装.基本没用.第二个是系统光盘的第一张.用这张就可以安装一个基本的系统.其他的软件,在系统安装完之后安装.第三个是系统盘的DVD版本.包括的软件比上 ...
- NSKeyedArchive(存储自定义对象)
在viewController.m中: - (void)viewDidLoad { [super viewDidLoad]; ZWPerson *p = [[ZWPerson alloc] init] ...