使用两个栈模拟队列的操作

栈A与栈B模拟队列Q,A为插入栈,B为弹出栈,即A提供入队列功能,B提供出队列功能。实现的方法有获得队列元素大小,判空,获得队头元素

入队列:入栈A

出队列:栈B不为空,直接弹出栈B数据

               栈B为空,则弹出栈A的数据,StudyLinkList.h放入栈B中,再弹出栈B的数据

<pre class="cpp" name="code">#include <iostream>
#include <stack>
using namespace std;
template<typename T>           //类模版
class QueueByDobuleStack
{
public:
	size_t size();           //队列元素大小
	bool empty();            //队列判空
	void push(T t);          //入队列
	void pop();              //出队列
	T top();                 //获得队头元素
private:
	stack<T> s1;             //栈
	stack<T> s2;
};
template <typename T>
size_t QueueByDobuleStack<T>::size()
{
	return s1.size()+s2.size();
}

template <typename T>
bool QueueByDobuleStack<T>::empty()
{
	return s1.empty()&&s2.empty();
}

template <typename T>
void QueueByDobuleStack<T>::push(T t)
{
     s1.push(t);
}

//若栈s2为空,则弹出栈s1的数据,放入栈s2中,再弹出栈s2的数据
//若s2不为空,则直接弹出s2的数据
template <typename T>
void QueueByDobuleStack<T>::pop()
{
	if (s2.empty())             
	{
		while (!s1.empty())
		{
			s2.push(s1.top());
			s1.pop();
		}
	}
	s2.pop();
}

template <typename T>
T QueueByDobuleStack<T>::top()
{
	if (s2.empty())
	{
		while (!s1.empty())
		{
			s2.push(s1.top());
			s1.pop();
		}
	}
	return s2.top();
}
int main()
{
	QueueByDobuleStack<int> q;
	if(q.empty())
		printf("p is empty!\n");
	for (int i=0;i<10;++i)
	{
		q.push(i);           //测试入队列
	} 
	printf("%d\n",q.size()); //队列元素个数
	while(!q.empty())
	{
		cout<<q.top()<<" ";  //获得队头元素
		q.pop();             //出队列
	}
	cout<<endl;
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用两个来模拟队列,具体实现如下: 1. 定义两个 Stack1 和 Stack2,用 Stack1 来存放入队的元素,用 Stack2 来处理出队操作。 2. 入队操作:将元素压入 Stack1。 3. 出队操作:首先判断 Stack2 是否为空,如果不为空,则弹出顶元素;如果为空,将 Stack1 中的元素依次出并压入 Stack2,再弹出顶元素。 下面是用 C 语言实现的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义结构体 typedef struct { int data[MAX_SIZE]; int top; } Stack; // 初始化 void initStack(Stack *s) { s->top = -1; } // 判断是否为空 int isStackEmpty(Stack *s) { return s->top == -1; } // 判断是否已满 int isStackFull(Stack *s) { return s->top == MAX_SIZE - 1; } // 入 void push(Stack *s, int value) { if (isStackFull(s)) { printf("Stack overflow!\n"); exit(1); } s->data[++s->top] = value; } // 出 int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack underflow!\n"); exit(1); } return s->data[s->top--]; } // 定义队列结构体 typedef struct { Stack s1; Stack s2; } Queue; // 初始化队列 void initQueue(Queue *q) { initStack(&q->s1); initStack(&q->s2); } // 入队 void enqueue(Queue *q, int value) { push(&q->s1, value); } // 出队 int dequeue(Queue *q) { int value; if (isStackEmpty(&q->s2)) { while (!isStackEmpty(&q->s1)) { value = pop(&q->s1); push(&q->s2, value); } if (isStackEmpty(&q->s2)) { printf("Queue underflow!\n"); exit(1); } } return pop(&q->s2); } int main() { Queue q; initQueue(&q); enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); printf("%d\n", dequeue(&q)); printf("%d\n", dequeue(&q)); printf("%d\n", dequeue(&q)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值