泛型编程C++实现栈
来源:互联网 发布:淘宝直通车溢价怎么算 编辑:程序博客网 时间:2024/04/30 07:23
泛型编程C++实现栈
Coordinate.h 头文件
#include <iostream>#include <ostream>using namespace std;class Coordinate{ friend ostream &operator<<(ostream &out, Coordinate &coor);public: Coordinate(int x = 0, int y = 0); ~Coordinate(); void printCoordinate();private: int m_iX; int m_iY;};Coordinate::Coordinate(int x, int y){ m_iX = x; m_iY = y;}Coordinate::~Coordinate(){}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;}
MyStack.h 头文件
#ifndef MYSTACK_H#define MYSTACK_H#include <iostream>using namespace std;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 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; m_pBuffer = NULL;}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()) { m_pBuffer[m_iTop] = elem; m_iTop++; return true; } else { return false; }}template <typename T>bool MyStack<T>::pop(T &elem){ if (!stackEmpty()) { m_iTop--; elem = m_pBuffer[m_iTop]; return true; } else { return false; }}template <typename T>void MyStack<T>::stackTraverse(bool isFromButtom){ if (isFromButtom) { for (size_t i = 0; i < m_iTop; i++) { cout << m_pBuffer[i]; } } else for (size_t i = m_iTop -1; i > 0; i--) { cout << m_pBuffer[i]; }}#endif MYSTACK_H
main.cpp 主函数
#include "MyStack.h"#include "Coordinate.h"int main(){ MyStack<char> *pStack = new MyStack<char>(5); pStack->push('h'); pStack->push('e'); pStack->push('l'); pStack->push('l'); pStack->push('o'); pStack->stackTraverse(true); cout << endl; MyStack<Coordinate> *p = new MyStack<Coordinate>(5); p->push(Coordinate(1, 2)); p->push(Coordinate(3, 4)); p->push(Coordinate(5, 6)); p->stackTraverse(true); delete p; p = NULL; delete pStack; pStack = NULL; system("pause"); return 0;}
0 1
- C语言实现泛型编程
- C语言实现泛型编程
- c泛型编程--栈
- C泛型编程实现的快排模版
- 顺序表的实现--C语言泛型编程
- C实现socket编程
- Visual C++DLL编程实现
- Visual C++DLL编程实现
- C编程实现http协议
- C编程实现http协议
- C编程实现http协议
- 编程 找嫌犯--c实现
- C语言编程实现grep
- 龙格库塔C 语言编程实现
- andriod网络编程--C实现
- .Net(C#)实现异步编程
- [C练习]编程实现strstr
- C语言编程实现三子棋
- 设计模式-单例模式
- 对结对编程理解
- 欢迎使用CSDN-markdown编辑器
- 全面认识Depth - 这里有关于Depth的一切
- 51NOD 1105 第K大的数 【二分】
- 泛型编程C++实现栈
- SSH框架之Struts的数据校验(2)
- 常见排序算法的时间复杂度和空间复杂度对比表
- Visual Studio Community 2015 下载链接
- Trapping Rain Water
- springmvc RESTful支持
- 开始
- maven--maven介绍及安装配置
- alexCTF sock题目