注册 登录
编程论坛 C++教室

【求助】C++压栈出栈问题

山丹老司机 发布于 2017-05-24 17:08, 2494 次点击
程序代码:
#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;
}

有什么需要改进的吗?
5 回复
#2
rjsp2017-05-24 17:31
让大家猜你的问题是什么?
#3
yangfrancis2017-05-24 18:31
b的类型是char. a是stack类型。不能就这样压入吧
#4
山丹老司机2017-05-26 15:16
回复 2楼 rjsp
不是啊 我也说不清我的问题,就是想问问如何压进去,然后弹出来
#5
rjsp2017-05-26 16:19
回复 4楼 山丹老司机
:) 你连你要问什么都不知道?
是要别人解决你的代码语法的问题(此时你应该贴出编译器给出的错误信息),还是要别人解决你算法逻辑的问题(此时你应该告诉别人你想实现什么功能、但实际结果和你的预期哪里出现了偏差)?

程序代码:
#include <cstddef>
#include <cassert>

template<class T,size_t SIZE=10>
class Stack
{
public:
    Stack();
    bool empty() const;
    bool full() const;

    void push( const T& item );
    T pop();
    void clear();
    const T& peek() const;

private:
    T list_[SIZE];
    size_t end_;
};

template<class T,size_t SIZE>
Stack<T,SIZE>::Stack() : end_(0)
{
}

template<class T,size_t SIZE>
bool Stack<T,SIZE>::empty() const
{
    return end_ == 0;
}

template<class T,size_t SIZE>
bool Stack<T,SIZE>::full() const
{
    return end_ == SIZE;
}

template<class T,size_t SIZE>
void Stack<T,SIZE>::push( const T& item )
{
    assert( !full() );
    list_[end_++] = item;
}

template<class T,size_t SIZE>
T Stack<T,SIZE>::pop()
{
    assert( !empty() );
    return list_[--end_];
}

template<class T,size_t SIZE>
const T& Stack<T,SIZE>::peek() const
{
    assert( !empty() );
    return list_[end_-1];
}

template<class T,size_t SIZE>
void Stack<T,SIZE>::clear()
{
    end_ = 0;
}

#include <iostream>
using namespace std;

int main( void )
{
    Stack<int,2> a;

    cout << "full = " << boolalpha << a.full() << endl;
    cout << "empty = " << boolalpha << a.empty() << endl;

    a.push(1);

    cout << "full = " << boolalpha << a.full() << endl;
    cout << "empty = " << boolalpha << a.empty() << endl;

    a.push(2);

    cout << "full = " << boolalpha << a.full() << endl;
    cout << "empty = " << boolalpha << a.empty() << endl;

    cout << a.pop() << endl;
    cout << a.pop() << endl;

    return 0;
}

#6
山丹老司机2017-05-27 23:45
回复 5楼 rjsp
我也不是很懂,老师布置的作业。现在明白了。写个栈,将123,ABC,压入并弹出。然后加个clock类,然后写入时间,再弹出来。谢谢你。
1