golang,go,博客,开源,编程
栈(Stack) 是一种特殊的线性数据结构,它遵循“后进先出”(LIFO, Last In First Out)原则,即最后插入的元素最先被删除。栈的操作仅限于一端(栈顶),所有的插入(入栈)和删除(出栈)操作都在栈顶进行。
栈支持以下几种常见操作:
Push(x)
将元素 x
插入到栈顶。Pop()
移除并返回栈顶元素。Peek()
或 Top()
返回栈顶元素,但不出栈。isEmpty()
如果栈为空返回 true
,否则返回 false
。Size()
返回栈的元素个数。栈是计算机科学中非常重要的数据结构,它广泛应用于以下几种场景:
栈可以通过数组或链表实现。
在数组实现中,栈是一个固定大小的数组,栈顶指针指向当前栈顶的位置。
#include <iostream>
using namespace std;
#define MAX 100 // 栈的最大容量
class Stack {
private:
int arr[MAX]; // 用数组来存储栈元素
int top; // 栈顶指针
public:
Stack() { top = -1; }
// 判断栈是否为空
bool isEmpty() {
return top == -1;
}
// 判断栈是否已满
bool isFull() {
return top == MAX - 1;
}
// 入栈操作
void push(int x) {
if (isFull()) {
cout << "Stack Overflow!" << endl;
return;
}
arr[++top] = x;
}
// 出栈操作
int pop() {
if (isEmpty()) {
cout << "Stack Underflow!" << endl;
return -1;
}
return arr[top--];
}
// 获取栈顶元素
int peek() {
if (isEmpty()) {
cout << "Stack is Empty!" << endl;
return -1;
}
return arr[top];
}
// 获取栈的大小
int size() {
return top + 1;
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element is: " << s.peek() << endl; // 输出栈顶元素 30
cout << "Stack size: " << s.size() << endl; // 输出栈的大小 3
cout << "Popped element: " << s.pop() << endl; // 弹出栈顶元素 30
cout << "Top element is: " << s.peek() << endl; // 输出栈顶元素 20
return 0;
}
链表实现的栈不需要预先分配固定大小的内存空间,且支持动态扩展。每个节点包含数据和指向下一个节点的指针。
#include <iostream>
using namespace std;
// 链表节点定义
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() { top = nullptr; }
// 判断栈是否为空
bool isEmpty() {
return top == nullptr;
}
// 入栈操作
void push(int x) {
Node* newNode = new Node();
newNode->data = x;
newNode->next = top;
top = newNode;
}
// 出栈操作
int pop() {
if (isEmpty()) {
cout << "Stack Underflow!" << endl;
return -1;
}
Node* temp = top;
int poppedValue = temp->data;
top = top->next;
delete temp;
return poppedValue;
}
// 获取栈顶元素
int peek() {
if (isEmpty()) {
cout << "Stack is Empty!" << endl;
return -1;
}
return top->data;
}
// 获取栈的大小
int size() {
int count = 0;
Node* temp = top;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element is: " << s.peek() << endl; // 输出栈顶元素 30
cout << "Stack size: " << s.size() << endl; // 输出栈的大小 3
cout << "Popped element: " << s.pop() << endl; // 弹出栈顶元素 30
cout << "Top element is: " << s.peek() << endl; // 输出栈顶元素 20
return 0;
}
操作 | 时间复杂度 |
---|---|
入栈(Push) | O(1) |
出栈(Pop) | O(1) |
栈顶元素(Peek) | O(1) |
栈的大小(Size) | O(n) |
判空(isEmpty) | O(1) |
栈是一种非常基础且重要的数据结构,广泛应用于计算机科学中的多种场景,如表达式求值、回溯算法、函数调用、撤销操作等。它可以通过数组或链表来实现,每种实现方式都有其优缺点。在实际使用中,可以根据具体需求选择合适的栈实现方式。