用两个栈模拟队列

来源:互联网 发布:黑马程序员32期 编辑:程序博客网 时间:2024/06/05 20:04

1、算法思想

由于队列先进先出,而栈后进先出,用两个栈就可以模拟队列操作就和一个队列等效
进队:如果栈1未满则直接进栈;如果栈1满了,栈2不为空,则队列满了;如果栈1满了,栈2为空,则将栈1 中所有元素倒入栈2中,然后元素进栈1
出队:如果栈2不为空则直接出栈;如果栈2为空,栈1也为空,则队空;如果栈2为空,栈1不为空,则将栈1 中所有元素倒入栈2中,然后栈2出栈。

2、代码

#include<iostream>using namespace std;#define NULL 0#define ok 1#define yes 1#define no 0#define error 0#define false 0#define maxsize 10struct stillstack//定义一个静态栈1,栈顶指针指向栈顶元素位置{    int data[maxsize];    int top, bottom;};int initstack(stillstack &s)//对静态栈的初始化{    s.top = s.bottom = -1;    return ok;}int push(stillstack &s, int e)//静态栈入栈{    if (s.top-s.bottom>=maxsize)        return error;    else        s.data[++s.top] = e;    return ok;}int pop(stillstack &s, int &e)//静态栈出栈{    if (s.top == s.bottom)        return error;    else        e = s.data[s.top--];    return ok;}int isempty(stillstack s)//用bottom=top来判定栈空{    if (s.top == s.bottom)        return yes;    else        return no;}int isfull(stillstack s)//用bottom=top来判定栈空{    if ( s.top - s.bottom >= maxsize)        return yes;    else        return no;}int length(stillstack s)//用botto-top来求栈中元素个数{    return s.top - s.bottom;}stillstack s1, s2;int inqueue(int x)//进队{    int e;    if (!isfull(s1))//如果栈1未满直接进站    {        push(s1, x);        return ok;    }    else//否则        if (!isempty(s2))//若栈2不为空则队列满了            return error;        else//若栈2为空,将栈1中所有元素倒入栈2中,然后元素进栈1        {            while (!isempty(s1))            {                pop(s1, e);                push(s2, e);            }            push(s1, x);            return ok;        }}int outqueue(int &x)//出队{    int e;    if (!isempty(s2))//如果栈2不为空则出栈    {        pop(s2, x);        return ok;    }    else//否则    {        if (isempty(s1))//若栈1也为空,则队为空            return error;        else//若栈1不为空,则将栈1的元素倒入栈2,然后栈2出栈        {            while (!isempty(s1))            {                if (pop(s1, e))                    push(s2, e);            }            pop(s2, x);            return ok;        }    }}int queue_isempty()//判队空{    if (isempty(s1) && isempty(s2))//两个栈都为空则队空        return yes;    else        return no;}int queue_isfull()//判队满{    if (isfull(s1) && !isempty(s2))//两个栈都为空则队空        return yes;    else        return no;}int main(){    int x=1,y=1,z;    initstack(s1); initstack(s2);    cout << "请输入要进行的操作,0代表出队,1代表入队,2表示判队是否已满,3表示判队是否为空,999代表停止" << endl;    while (1)    {        cin >> z;        if (z == 1)        {            cout << "请输入要入队的数据" <<"  ";            cin >> x;            if (inqueue(x))                continue;            else            {                cout << "队满,进队失败"<<endl;                continue;            }        }        if (z == 0)        {            if (outqueue(y))               cout << "出队的数据是" << y << endl;            else               cout << "所有数据已经出队" << endl;            continue;        }        if (z == 2)        {            if(queue_isfull())            {                cout << "队满" << endl;                continue;            }            else            {                cout << "队未满" << endl;                continue;            }        }        if (z == 3)        {            if (queue_isempty())            {                cout << "队为空" << endl;                continue;            }            else            {                cout << "队不为空" << endl;                continue;            }        }        if (z = 999)            return ok;    }}
1 0
原创粉丝点击