![]() |
#2
rjsp2017-05-24 17:31
|

#include<iostream>
#include<cassert>
using namespace std;
template<class T,int SIZE=10>
class Stack{
private:
T list[SIZE];
int top;
public:
Stack();
void push(const T &item);
T pop();
void clear();
const T &peek() const;
bool isFull() const;
bool isEmpty()const;
};
template<class T,int SIZE>
Stack<T,SIZE>::Stack:top(-1){}
template<class T,int SIZE>
void Stack<T,SIZE>::push(const T &item){
assert(!isFull());
list[++top]=item;
}
template<class T,int SIZE>
T Stack<T,SIZE>::pop(){
assert(!isEmpty());
return list[top--];
}
template<class T,int SIZE>
const T &Stack<T,SIZE>peek()const {
assert(!isEmpty());
return list[top];
}
template<class T,int SIZE>
bool Stack<T,SIZE>::isEmpty()const{
return top==SIZE-1;
}
template<class T,int SIZE>
void Stack<T,SIZE>clear(){
top=-1;
}
int main () {
Stack<int>a;
a.push(1); //将123压入
a.push(2);
a.push(3);
a.pop();//将123弹出来
a.pop();
a.pop();
Stack<char>b;
b.push(a);//将a压入
b.pop();//将a弹出
return 0;
}
#include<cassert>
using namespace std;
template<class T,int SIZE=10>
class Stack{
private:
T list[SIZE];
int top;
public:
Stack();
void push(const T &item);
T pop();
void clear();
const T &peek() const;
bool isFull() const;
bool isEmpty()const;
};
template<class T,int SIZE>
Stack<T,SIZE>::Stack:top(-1){}
template<class T,int SIZE>
void Stack<T,SIZE>::push(const T &item){
assert(!isFull());
list[++top]=item;
}
template<class T,int SIZE>
T Stack<T,SIZE>::pop(){
assert(!isEmpty());
return list[top--];
}
template<class T,int SIZE>
const T &Stack<T,SIZE>peek()const {
assert(!isEmpty());
return list[top];
}
template<class T,int SIZE>
bool Stack<T,SIZE>::isEmpty()const{
return top==SIZE-1;
}
template<class T,int SIZE>
void Stack<T,SIZE>clear(){
top=-1;
}
int main () {
Stack<int>a;
a.push(1); //将123压入
a.push(2);
a.push(3);
a.pop();//将123弹出来
a.pop();
a.pop();
Stack<char>b;
b.push(a);//将a压入
b.pop();//将a弹出
return 0;
}
有什么需要改进的吗?