📜  在 cpp 中使用链表实现堆栈 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:52.753000             🧑  作者: Mango

代码示例1
#include
using namespace std;
struct node
{
    int data;
    node* next;
};
class Stack
{
private:
    node* top;
public:
    Stack()
    {
        top = NULL;
    }
    void Push(int n)
    {
        node* temp = new node();
        temp->data = n;
        temp->next = NULL;
        if (top == NULL)
        {
            top = temp;
        }
        else
        {
            temp->next = top;
            top = temp;
        }
    }
    void Pop()
    {
        
        if (top == NULL)
        {
            cout << "Error,Stack is Empty!" << endl;
            return;
        }
        node* temp = top;
        top = top->next;
        delete temp;
    }
    void Top()
    {
        cout << "Top Of Stack: " << top->data << endl;
    }
    void Display()
    {
        node* t = top;
        cout << "Stack: ";
        while (t != NULL)
        {
            cout << t->data << " ";
            t = t->next;
        }
        cout << "\n";
    }
    void IsEmpty()
    {
        node* t = top;
        if (t == NULL)
        {
            cout << "Stack Is Empty!" << endl;
        }
        else
        {
            cout << "Stack Is Not Empty!" << endl;
            Display();
        }
    }
};
int main()
{
    Stack s;
    s.Push(1);
    s.IsEmpty();
    s.Pop();
    s.IsEmpty();

    return 0;
}