阅读背景:

堆(stack) 之 c 和 c++模板实现(空类默认成员函数 初谈引用 内联函数)

来源:互联网 
//stack 的基本操作
#include <iostream>
using namespace std;
const int maxn = 3;
typedef struct Stack
{
    //NumType num;
    int num;
}Stack;
int top = 0;//当前元素位置的上一个元素
Stack stack[maxn];
bool is_empty();
bool is_full();
int pop();
void push(const int &key);
int get_top();
bool make_empty();

int main()
{
    int tmp;
    cout << get_top() << " " << pop() << endl;// 这句话和下面存在同样的错误
    cout << "Input a set of integers:(-1 is over)" << endl;
    while(cin >> tmp)
    {
        if(tmp == -1) break;
        push(tmp);
    }
    tmp = get_top();
    cout << tmp << endl;
    tmp = pop();
    cout << tmp << endl;
    //cout << pop() << " " << get_top() << endl;
    //cout << get_top() << " " << pop() << endl;// 这样输出会发生非常奇妙的事情的
    return 0;
}
// if stack is empty return true
bool is_empty()
{
    return (top == 0);
}
// if stack is full return true
bool is_full()
{
    return (top == maxn);
}
// pop the top number if not empty
int pop()
{
    if(is_empty())
    {
        cout << "The Stack is empty,Cannot pop..." << endl;
        return -1;
    }
    else
    {
        top --;
        return stack[top].num;
    }

}
// push one number,if not full
void push(const int &key)
{
    if(is_full())
    {
        cout << "The Stack is full,Cannot push..." << endl;
        return;
    }
    else
    {
        stack[top].num = key;
        top ++;
    }
}
// get the top number,not pop
int get_top()
{
    if(is_empty())
    {
        cout << "The Stack is empty,Cannot get top..." << endl;
        return -1;
    }
    else
    {
        return stack[top-1].num;
    }
}
bool make_empty()
{
    top = 0;
}//stack 的基本操作
#include <iostream>
using namespa



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: