栈操作的C++实现

温习一下数据结构,写了一个栈操作的模板...

第一次写的时候还忘了写析构函数,要吸取教训啊。

代码:

#ifndef STACK_H #define STACK_H #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; template<class T> class Stack { public: int top; int maxtop; T *data; Stack(int size); //构造函数 ~Stack(); bool isEmpty(); //判断为空 bool isFull(); //判断为满 void push(T ch/*,Stack s*/); //压栈 T pop(); //出栈 }; template <class T> Stack<T>::Stack(int size) { maxtop=size; top=-1; data=new T[size]; } template <class T> Stack<T>::~Stack() { delete []data; } template <class T> bool Stack<T>::isEmpty() { return top<0; } template <class T> bool Stack<T>::isFull() { return top==maxtop; } template <class T> void Stack<T>::push(T ch/*,Stack s*/) { if (isFull()) { cout<<"The stack is full~"<<endl; return; } data[++top]=ch; } template <class T> T Stack<T>::pop() { if (isEmpty()) { cout<<"The stack is empty~"<<endl; return -1; } T s=data[top]; top--; return s; } #endif

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值