您的位置:首页 > 编程语言 > C语言/C++

用两个栈实现一个队列功能

2013-03-14 16:25 513 查看
1、c++实现

#include<iostream>
#include<stack>
using namespace std;

/*使用两个堆栈实现队列
s1:实现入队,s2:实现出队
首先,将数据存放在栈s1中,然后将数据push进s2中,再将s2中的数据pop
出队列:
(1)如果栈B不为空,直接弹出栈B的数据
(2)如果栈B为空,则依次弹出栈A的数据,放入栈B中,再弹出栈B的数据
*/
template<class T> struct Queue
{
stack<T> s1;
stack<T> s2;

void push(T &t)
{
s1.push(t);
}

T front()
{
if(s2.empty())
{
if(s1.size()==0)
throw;
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}

return s2.top();
}

void pop()
{
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
}
};

2、c实现

/*
* 用两个栈实现队列
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct stack
{
//用来保存元素
int *a;
//当前的下标
int loc;
//栈中最多可以容纳的元素个数
int max;
}st;
/*
* 初始化指定大小的栈
*/
st *initstack(int num)
{
st *s=(st *)malloc(sizeof(st));
if(s==NULL)
{
printf("create fail!\n");
exit(1);
}
s->loc=0;
s->max=num;
s->a=(int *)malloc(sizeof(int)*num);
if(s->a==NULL)
{
printf("create fail!\n");
exit(1);
}
return s;
}
/*
* 判断栈是否为空(空:返回1 非空:返回0)
*/
int isstackempty(st *s)
{
//栈为空
if(s->loc==0)return 1;
else return 0;
}
/*
* 判断栈是否为满(满:返回1 非满:返回0)
*/
int isstackfull(st *s)
{
//栈为满
if(s->loc==s->max)return 1;
else return 0;
}
/*
* 栈中元素个数
*/
int getstacknum(st *s)
{
if(isstackempty(s)==1)return 0;
else return s->loc;
}
/*
* 入栈
*/
void pushstack(st *s,int value)
{
//栈满,无法入栈
if(isstackfull(s)==1)
{
printf("栈已满,无法入栈!\n");
exit(1);
}
s->a[s->loc]=value;
s->loc++;
}
/*
* 出栈
*/
int popstack(st *s)
{
if(isstackempty(s)==1)
{
printf("栈为空,无法出栈!\n");
exit(1);
}
int temp=s->a[s->loc-1];
s->loc--;
return temp;
}
/*
* 查看栈顶元素
*/
int peekstack(st *s)
{
if(isstackempty(s)==1)
{
printf("栈为空,无法查看栈顶元素!\n");
exit(1);
}
int temp=s->a[s->loc-1];
return temp;
}
/*
* 将一个栈中的元素拷贝到另一栈中
*/
void copy(st *src,st *dest)
{
//拷贝过程中发生溢出
if((src->loc+dest->loc)>dest->max)
{
printf("拷贝过程中发生溢出!\n");
exit(1);
}
while(isstackempty(src)!=1)
{
int value=popstack(src);
pushstack(dest,value);
}
}
/*
* 打印栈中的各个元素
*/
void printstack(st *s)
{
if(isstackempty(s)==1)
{
printf("栈为空,无法打印栈中各个元素!\n");
exit(1);
}
while(isstackempty(s)!=1)
{
int value=popstack(s);
printf("%d--->",value);
}
}
/*
* 入队列(入队列的元素保存在src中)
*/
void pushqueue(st *src,st *dest,int value)
{
if(src->loc==src->max)
{
printf("队列已满,无法入队!\n");
exit(1);
}
src->a[src->loc]=value;
src->loc++;
}
/*
* 出队列
* (如果dest不为空,则直接从dest弹出一个元素)
* (如果dest为空,src不为空,则先将src中的元素拷贝到dest中,在从dest中弹出一个元素)
*/
int popqueue(st *src,st *dest)
{
if(isstackempty(src)==1&&isstackempty(dest)==1)
{
printf("队列为空,无法出队!\n");
exit(1);
}
if(isstackempty(dest)!=1)
{
int value=popstack(dest);
return value;
}
else if(isstackempty(dest)==1&&isstackempty(src)!=1)
{
copy(src,dest);
int value=popstack(dest);
return value;
}
}
int main()
{
int num=3;
int value=0;
st *src=initstack(num);
st *dest=initstack(num);
pushstack(src,1);
pushstack(src,2);
value=popqueue(src,dest);
printf("弹出的元素=%d\n",value);
printf("\n");
pushstack(src,3);
pushstack(src,4);
pushstack(src,5);
value=popqueue(src,dest);
printf("弹出的元素=%d\n",value);
value=popqueue(src,dest);
printf("弹出的元素=%d\n",value);
value=popqueue(src,dest);
printf("弹出的元素=%d\n",value);
value=popqueue(src,dest);
printf("弹出的元素=%d\n",value);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  队列 堆栈 c c++