S SmartDocs
系列: Algorithms java 70 行 · 更新于 2026-03-02

#include <iostream>.java

Algorithms/#include <iostream>.java

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

class Stack {
private:
    Node* top;

public:
    Stack();
    bool isEmpty();
    bool isFull();
    void push(int x);
    void pop();
};

Stack::Stack() {
    top = nullptr;
}

bool Stack::isEmpty() {
    return (top == nullptr);
}

bool Stack::isFull() {
    Node* temp = new Node;

    if (!temp) {
        return true;
    }

    delete temp;
    return false;
}

void Stack::push(int x) {
    if (isFull()) {
        return;
    }

    Node* newNode = new Node;
    newNode->data = x;
    newNode->next = top;
    top = newNode;
}

void Stack::pop() {
    if (isEmpty()) {
        return;
    }

    Node* temp = top;
    top = top->next;
    delete temp;
}

int main() {
    Stack s;

    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    
    return 0;
}

相关文章