| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2303 人关注过本帖
标题:【求助】C++压栈出栈问题
取消只看楼主 加入收藏
山丹老司机
Rank: 2
等 级:论坛游民
帖 子:20
专家分:12
注 册:2016-11-1
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:2 
【求助】C++压栈出栈问题
程序代码:
#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;
}

有什么需要改进的吗?
搜索更多相关主题的帖子: class int SIZE Stack const 
2017-05-24 17:08
山丹老司机
Rank: 2
等 级:论坛游民
帖 子:20
专家分:12
注 册:2016-11-1
收藏
得分:0 
回复 2楼 rjsp
不是啊 我也说不清我的问题,就是想问问如何压进去,然后弹出来
2017-05-26 15:16
山丹老司机
Rank: 2
等 级:论坛游民
帖 子:20
专家分:12
注 册:2016-11-1
收藏
得分:0 
回复 5楼 rjsp
我也不是很懂,老师布置的作业。现在明白了。写个栈,将123,ABC,压入并弹出。然后加个clock类,然后写入时间,再弹出来。谢谢你。
2017-05-27 23:45
快速回复:【求助】C++压栈出栈问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026898 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved