C++栈和队列

栈与队列标准库使用

使用标准库的栈和队列时,先包含相关的头文件

#include<stack>

#include<queue>

定义栈如下:

stack<int> stk;

定义队列如下:

queue<int> q;

栈提供了如下的操作

s.empty()               如果栈为空返回true,否则返回false
s.size()                返回栈中元素的个数
s.pop()                 删除栈顶元素但不返回其值
s.top()                 返回栈顶的元素,但不删除该元素
s.push()                在栈顶压入新元素


队列提供了下面的操作

q.empty()               如果队列为空返回true,否则返回false
q.size()                返回队列中元素的个数
q.pop()                 删除队列首元素但不返回其值
q.front()               返回队首元素的值,但不删除该元素
q.push()                在队尾压入新元素
q.back()                返回队列尾元素的值,但不删除该元素


 

### C++队列的实现方法 #### 使用数组实现C++ 中,可以通过数组手动实现是一种后进先出 (LIFO, Last In First Out) 的数据结构。以下是基于数组的手动实现: ```cpp #include <iostream> using namespace std; class Stack { private: int top; int capacity; int* array; public: Stack(int size) : top(-1), capacity(size) { array = new int[size]; } ~Stack() { delete[] array; } bool isEmpty() const { return top == -1; } bool isFull() const { return top == capacity - 1; } void push(int value) { if (!isFull()) { array[++top] = value; } else { cout << "Stack Overflow!" << endl; } } int pop() { if (!isEmpty()) { return array[top--]; } else { cout << "Stack Underflow!" << endl; return -1; // Assuming no negative values are pushed into the stack. } } int peek() const { if (!isEmpty()) { return array[top]; } else { cout << "Stack is empty!" << endl; return -1; } } }; ``` 以上代码展示了如何通过数组创建一个简单的类[^1]。 --- #### 使用标准库 `std::stack` 实现 C++ 提供了标准模板库中的 `std::stack` 容器适配器,可以更方便地使用功能: ```cpp #include <iostream> #include <stack> int main() { std::stack<int> s; s.push(10); s.push(20); s.push(30); cout << "Top element: " << s.top() << endl; // 输出顶部元素 s.pop(); // 删除顶部元素 cout << "After popping, Top element: " << s.top() << endl; cout << "Size of stack: " << s.size() << endl; while (!s.empty()) { // 当不为空时循环弹出所有元素 cout << s.top() << " "; s.pop(); } return 0; } ``` 此代码片段演示了如何利用 STL 中的 `std::stack` 来快速实现的功能[^2]。 --- #### 使用数组实现队列 队列是一种先进先出 (FIFO, First In First Out) 的数据结构。下面是一个基于数组的手动实现方式: ```cpp #include <iostream> using namespace std; class Queue { private: int front, rear, capacity; int* array; public: Queue(int size) : front(0), rear(0), capacity(size) { array = new int[size]; } ~Queue() { delete[] array; } bool isEmpty() const { return front == rear; } bool isFull() const { return ((rear + 1) % capacity == front); } void enqueue(int value) { if (!isFull()) { array[rear] = value; rear = (rear + 1) % capacity; } else { cout << "Queue Overflow!" << endl; } } int dequeue() { if (!isEmpty()) { int temp = array[front]; front = (front + 1) % capacity; return temp; } else { cout << "Queue Underflow!" << endl; return -1; // 假设不会插入负数到队列中 } } int getFront() const { if (!isEmpty()) { return array[front]; } else { cout << "Queue is empty!" << endl; return -1; } } }; ``` 这段代码展示了一个环形缓冲区形式的队列实现[^3]。 --- #### 使用标准库 `std::queue` 实现队列 类似于C++ 标准库也提供了 `std::queue` 容器适配器用于简化队列的操作: ```cpp #include <iostream> #include <queue> int main() { std::queue<int> q; q.push(10); q.push(20); q.push(30); cout << "Front element: " << q.front() << endl; // 获取队首元素 q.pop(); // 移除队首元素 cout << "New Front element after pop: " << q.front() << endl; cout << "Rear element: " << q.back() << endl; // 获取队尾元素 cout << "Queue size: " << q.size() << endl; while (!q.empty()) { // 循环移除直到队列为空 cout << q.front() << " "; q.pop(); } return 0; } ``` 该示例说明了如何借助 STL 中的 `std::queue` 轻松管理队列操作。 --- ### 总结 无论是手动实现还是使用 STL 提供的标准容器适配器,都可以灵活选择适合项目需求的方式完成队列的设计开发。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值