两个栈实现队列基本要求:
入队元素全部往push_stack里加
出队元素全部从pop_stack里出,如果pop_stack里没有元素,push_stack所有元素压入pop_stack。
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class QUEUE
{
public:
void enqueue(int x);
int dequeue();
int front();
bool empty();
private:
stack<int> push_s, pop_s;
};
void QUEUE::enqueue(int x)
{
push_s.push(x);
}
int QUEUE::dequeue()
{
int res = 0;
if (pop_s.size() != 0)
{
res = pop_s.top();
pop_s.pop();
}
else if (push_s.size() != 0)
{
while (!push_s.empty())
{
int temp = push_s.top();
pop_s.push(temp);
push_s.pop();
}
res = dequeue();
}
else
{
cout << "queue empty" << endl;
}
return res;
}
int QUEUE::front()
{
int res = 0;
if (pop_s.size() != 0)
{
res = pop_s.top();
}
else if (push_s.size() != 0)
{
while (!push_s.empty())
{
int temp = push_s.top();
pop_s.push(temp);
push_s.pop();
}
res = front();
}
else
{
cout << "queue empty" << endl;
}
return res;
}
bool QUEUE::empty()
{
return pop_s.empty() && push_s.empty();
}
int main()
{
QUEUE q;
for (int i = 0; i < 10; i++)
{
q.enqueue(i);
}
for (int i = 0; i < 5; i++)
{
cout << q.dequeue() << endl;
}
for (int i = 10; i < 15; i++)
{
q.enqueue(i);
}
while (!q.empty())
{
cout << q.dequeue() << endl;
}
return 0;
}
两个队列实现一个栈的基本要求:
data_queue装数据,top_queue保存栈顶(size=0或1)。
1、push操作数据全部装入data_q
2、pop操作去top_queue里取数据
情况1:如果top_queue有数据,top_queue出队
情况2:如果没有数据,则把data_queue的(size - 1)个数push到top_queue,然后交换指针。返回情况1。
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class STACK
{
public:
STACK()
{
data_q = new queue<int>;
top_q = new queue<int>;
}
~STACK()
{
delete data_q;
delete top_q;
}
void push(int x)
{
data_q->push(x);
}
void pop()
{
if (!top_q->empty())
{
top_q->pop();
}
else if (!data_q->empty())
{
while (data_q->size() > 1)
{
int temp = data_q->front();
top_q->push(temp);
data_q->pop();
}
swap(top_q, data_q);
pop();
}
else
{
cout << "stack empty" << endl;
}
}
int top()
{
int res = 0;
if (!top_q->empty())
{
res = top_q->front();
}
else if (!data_q->empty())
{
while (data_q->size() > 1)
{
int temp = data_q->front();
top_q->push(temp);
data_q->pop();
}
swap(top_q, data_q);
res = top();
}
else
{
cout << "stack empty" << endl;
}
return res;
}
bool empty()
{
return top_q->empty() && data_q->empty();
}
private:
queue<int> *data_q, *top_q;
};
int main()
{
STACK s;
for (int i = 0; i < 5; i++)
{
s.push(i);
}
while (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
return 0;
}
最小值栈
快速返回栈内最小值
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
class minStack
{
public:
void push(int x)
{
s.push(x);
if (min_s.empty())
{
min_s.push(x);
}
else
{
min_s.push(x < min_s.top() ? x :min_s.top());
}
}
void pop()
{
s.pop();
min_s.pop();
}
int top()
{
return s.top();
}
int min()
{
return min_s.top();
}
bool empty()
{
return s.empty();
}
private:
stack<int> s, min_s;
};
int main()
{
minStack s;
s.push(3);
cout << s.min() << endl;
s.push(6);
cout << s.min() << endl;
s.push(2);
cout << s.min() << endl;
s.push(9);
cout << s.min() << endl;
s.push(0);
cout << s.min() << endl;
s.pop();
cout << s.min() << endl;
s.push(4);
cout << s.min() << endl;
/*
3
3
2
2
0
2
2
*/
return 0;
}