系列: 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;
}
相關文章
Algorithms
cpp
更新於 2026-04-07
748.cpp
748.cpp — cpp source code from the Algorithms learning materials (Algorithms/748.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
827.cpp
827.cpp — cpp source code from the Algorithms learning materials (Algorithms/827.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
827_best_greedy.cpp
827_best_greedy.cpp — cpp source code from the Algorithms learning materials (Algorithms/827_best_greedy.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
8402.cpp
8402.cpp — cpp source code from the Algorithms learning materials (Algorithms/8402.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
860.cpp
860.cpp — cpp source code from the Algorithms learning materials (Algorithms/860.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-07-24
bin_packing.cpp
bin_packing.cpp — cpp source code from the Algorithms learning materials (Algorithms/Advanced/bin_packing.cpp).
閱讀文章 →