1. Heap basic knowledge
S.top(): 栈顶
S.size():栈中元素个数
S.empty(): 栈是否为空
S.push(x):入栈
S.pop():出栈
test1: basic knowledge of heap (using STL)
#include <cstdio>
#include <stack>
using namespace std;
int main(){
stack<int> S;
if (S.empty()){
printf("S is empty!\n");
}
S.push(5);
S.push(6);
S.push(10);
printf("S.top = %d\n", S.top());
S.pop();
S.pop();
printf("S.top = %d\n", S.top());
printf("S.size = %d\n", S.size());
return 0;
}
2. Basic knowledge of the queue
Q.top(): 栈顶
Q.empty(): 队列是否为空
Q.front():返回队头元素
Q.back() :返回队尾元素
Q.push(x):入队
Q.pop():出队
Q.size():队中元素个数
test2: Basic knowledge of queues (using STL)
#include <cstdio>
#include <queue>
using namespace std;
int main(){
queue<int> Q;
if (Q.empty()){
printf("Q is empty!\n");
}
Q.push(5);
Q.push(6);
Q.push(10);
printf("Q.front = %d\n", Q.front());
Q.pop();
Q.pop();
printf("Q.front = %d\n", Q.front());
Q.push(1);
printf("Q.back = %d\n", Q.back());
printf("Q.size = %d\n", Q.size());
return 0;
}
2. Basic knowledge of heap
/*同queue*/
heap.empty(): 队列是否为空
heap.front():返回队头元素
heap.back() :返回队尾元素
heap.push(x):入队
heap.pop():出队
heap.size():队中元素个数
Large top heap (default) Small top heap
priority_queue<int> big_heap; //默认大顶堆
priority_queue<int, vector<int>, greater<int> > small_heap; //小顶堆
priority_queue<int, vector<int>,less<int> > big_heap2; //大顶堆
Structure + heap sort
priority_queue<node>q;//或 priority_queue< node,vector<node>,less<node> >q;
//1 在struct 外重载
bool operator<(node a,node b)//推荐写(const node &a,const node &b)
{
return a.y>b.y;//以y从小到大排序
}
//2.1 在定义struct node时重载
struct node
{
int x,y;
bool operator<(const node &a) const{
return y<a.y; //以y从大到小排序
}
};
//2.2 定义友元操作类重载函数
struct node
{
int x,y;
friend bool operator<(const node &a,const node &b){
return a.y<b.y; //按y从大到小排列
}
};
//3 自定义比较函数模板结构
struct cmp
{
bool operator ()(const node &a, const node &b)
{
return a.v>b.v;// 按照v从小到大排列
}
};
test3: Basic knowledge of queues (using STL)
#include <cstdio>
#include <queue>
using namespace std;
int main(){
priority_queue<int> big_heap; //默认大顶堆
priority_queue<int, vector<int>, greater<int> > small_heap; //小顶堆
priority_queue<int, vector<int>,less<int> > big_heap2; //大顶堆
if (big_heap.empty()){
printf("big_heap is empty!\n");
}
int test[] = {6, 10, 1, 7, 99, 4, 33};
for (int i = 0; i < 7; i++){
big_heap.push(test[i]);
}
printf("big_heap.top = %d\n", big_heap.top());
big_heap.push(1000);
printf("big_heap.top = %d\n", big_heap.top());
for (int i = 0; i < 3; i++){
big_heap.pop();
}
printf("big_heap.top = %d\n", big_heap.top());
printf("big_heap.size = %d\n", big_heap.size());
return 0;
}
Examples of expansion:
Leetcode 225 uses the queue to implement the stack
#include <stdio.h>
#include <queue>
using namespace std;
class MyStack {
public:
MyStack() {
}
void push(int x) {
queue<int> temp_queue; //临时队列实现
temp_queue.push(x);
while(!_data.empty()){
temp_queue.push(_data.front());
_data.pop();
}
while(!temp_queue.empty()){
_data.push(temp_queue.front());
temp_queue.pop();
}
}
int pop() {
int x = _data.front();
_data.pop();
return x;
}
int top() {
return _data.front();
}
bool empty() {
return _data.empty();
}
private:
queue<int> _data;
};
int main(){
MyStack S;
S.push(1);
S.push(2);
S.push(3);
S.push(4);
printf("%d\n", S.top());
S.pop();
printf("%d\n", S.top());
S.push(5);
printf("%d\n", S.top());
return 0;
}
leetcode 232 uses the stack to implement the queue
#include <stdio.h>
#include <stack>
using namespace std;
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stack<int> temp_stack;
while(!_data.empty()){
temp_stack.push(_data.top());
_data.pop();
}
temp_stack.push(x);
while(!temp_stack.empty()){
_data.push(temp_stack.top());
temp_stack.pop();
}
}
int pop() {
int x = _data.top();
_data.pop();
return x;
}
int peek() {
return _data.top();
}
bool empty() {
return _data.empty();
}
private:
stack<int> _data;
};
int main(){
MyQueue Q;
Q.push(1);
Q.push(2);
Q.push(3);
Q.push(4);
printf("%d\n", Q.peek());
Q.pop();
printf("%d\n", Q.peek());
return 0;
}
leetcode 155 MinStack minimum stack (O (1) returns the smallest value in the stack)
Idea: Use a stack to record the minimum value of each time
/*最小值栈,利用最小值栈实现,需要改变push()*/
#include <stdio.h>
#include <stack>
using namespace std;
class MinStack {
public:
MinStack() {
}
void push(int x) {
_data.push(x);
if (_min.empty()){
_min.push(x);
}
else{
if (x > _min.top()){
x = _min.top();
}
_min.push(x);
}
}
void pop() {
_data.pop();
_min.pop();
}
int top() {
return _data.top();
}
int getMin() {
return _min.top();
}
private:
stack<int> _data;
stack<int> _min;
};
int main(){
MinStack minStack;
minStack.push(-2);
printf("top = [%d]\n", minStack.top());
printf("min = [%d]\n\n", minStack.getMin());
minStack.push(0);
printf("top = [%d]\n", minStack.top());
printf("min = [%d]\n\n", minStack.getMin());
minStack.push(-5);
printf("top = [%d]\n", minStack.top());
printf("min = [%d]\n\n", minStack.getMin());
minStack.pop();
printf("top = [%d]\n", minStack.top());
printf("min = [%d]\n\n", minStack.getMin());
return 0;
}
Programming example:
# Given an array, determine whether a sequence is the correct pop sequence of the array elements.
Ideas:
1.出栈结果存储在队列order中
2.按元素顺序,将元素push进入栈
3.每push-个元素,即检查是否与队列首部元素相同若相同则弹出队列首元素,弹出栈顶元素,直到两元素不同结束
4.若最终栈为空,说明序列合法,否则不合法
#include <stdio.h>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
bool check_is_valid_order(queue<int> &order){
stack<int> S;
int n = order.size();
for (int i = 1; i <= n; i++){
S.push(i);
while(!S.empty() && order.front() == S.top()){
S.pop();
order.pop();
}
}
if (!S.empty()){
return false;
}
return true;
}
int main(){
int n;
scanf("%d",&n);
int a[n];
while(scanf("%d",&a[0])!=EOF){
queue<int> order;
order.push(a[0]);
for(int i = 1; i < n; i++){
scanf("%d", &a[i]);
order.push(a[i]);
}
if (check_is_valid_order(order)){
printf("Yes\n");
}
else{
printf("No\n");
}
memset(a,0,sizeof(a));
}
return 0;
}
Test Results: