close

#include <iostream>
using namespace std;

/**Stack**/
class Node
{
private:
    int data;
    Node* sptr;

    Node* top;
public:
    Node()
    {
        top = 0;
    }

    void push(int _data)
    {
        Node* current = new Node;
        current->data = _data;
        current->sptr = 0;

        if(isEmpty())
        {
            top = current;
        }
        else
        {
            current->sptr = top;
            top = current;
        }
    }

    void pop()
    {
        if(isEmpty())
        {
            cout<<"stack empty"<<endl;
        }
        else
        {
            Node* current = top;
            top = top->sptr;
            cout<<current->data<<endl;
            delete current;
        }
    }

    bool isEmpty()
    {
        if(top==0)
            return true;
        else
            return false;
    }

    void show()
    {
        Node* current=0;
        cout<<"**stack: ";
        for(current=top;current!=0;current = current->sptr)
        {
            cout<<current->data<<" ";
        }
        cout<<endl;
    }
};

int main()
{
    Node myStack;
    int n,m;
    cout<<"*key 1 is push, key 0 is pop*"<<endl;
    while(cin>>n)
    {
        if(n)
        {
           cin>>m;
           myStack.push(m);
        }
        else
        {
            myStack.pop();
        }
        myStack.show();
    }

    return 0;
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Kuihao 的頭像
    Kuihao

    溫暖午後的金針田__孕育有趣的創新

    Kuihao 發表在 痞客邦 留言(0) 人氣()