用两个栈实现一个队列(C++)

#include "malloc.h"
#include "iostream"

using namespace std; 

#define STACKINCREMENT 10
#define STACK_INIT_SIZE 10

class stack {
public :
    int * base;
    int * top;
    int stacksize;

    bool initStack()
    {
        base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
        if(!base) return false;  /*分配空间失败*/
        top = base;
        stacksize = STACK_INIT_SIZE;
        return true;            /*初始化栈成功*/
    }

    bool Push(int e) {
        if(top - base >= stacksize){
                    /*栈满,追加空间*/
            base = (int *)realloc(base, (stacksize + 
            STACKINCREMENT)*sizeof(int));
            if(!base) return false;   /*存储分配失败*/
            top = base + stacksize;
            stacksize = stacksize + STACKINCREMENT; /*设置栈的最大容量*/
            }
            *top = e;  /*放入数据*/
            top++;
            return true;
    }

    bool Pop(int &e) {
        if(top == base) return false;  /*栈空,非法操作*/
        e = *--top;
        return true;
    }

    ~stack() {
        if (base != NULL) {
            free(base);                /*析构函数,释放堆内存空间*/
        }
    }
};

class queue {
    stack s1, s2;    /*定义两个栈s1和s2,用它们实现一个队列*/

public :
    bool  initQueue() {
        if (s1.initStack() && s2.initStack() )  {    /*初始化队列,调用stack的初始化*/
            return true;
        }
        return false;
    }

    bool EnQueue(int x) {
        if (s1.Push(x)) {
            return true;        /*入队列,将x压入栈s1*/
        }
        return false;
    }

    bool DeQueue(int &x) {
        int e;
        if (s2.base == s2.top) {
            /*栈s2为空的情况*/
            if (s1.top == s1.base) {
                return false;
            } else {
                while (s1.Pop(e)) {
                    s2.Push(e);
                }
            }
        } 
        s2.Pop(x);         /*出队列,从栈s2中取出数据*/
        return true;
    }

    bool IsEmptyQueue() {
        if (s1.base == s1.top && s2.base == s2.top) {
            return true;    /*队列为空*/    
        } else {
            return false;   /*队列不为空*/
        }
    }

    int getCount() {
        return s1.top - s1.base + s2.top - s2.base; /*计算两个栈当前容量之和*/
    }
};

int main() {
    int i, x=0, e;
    queue q;
    q.initQueue();        /*初始化一个队列*/

    for (i=0; i<10; i++) {
        q.EnQueue(x);            /*入队列操作,0~9*/
        x++;
    }
    for (i=0; i<5; i++) {
        if (q.DeQueue(e)) {
            cout<<e<<" ";    /*出队列操作,连续出队列5次,并打印在屏幕上*/
        }    
    }
                                     /*输出当前队列中元素的个数*/
    cout << "\nThe number of elements in the queue is " <<q.getCount() <<endl;
    for (i=0; i<10; i++) {
        q.EnQueue(x);                /*入队列操作,10~19*/
        x++;
    }
    for (i=0; i<15; i++) {
        if (q.DeQueue(e)) {        /*出队列操作,连续出队列15次,并打印在屏幕上*/
            cout<<e<<" ";
        }    
    }            
    /*输出当前队列中元素的个数*/
    cout << "\nThe number of elements in the queue is " <<q.getCount() <<endl;
    /*打印当前队列是否为空*/
    if (q.IsEmptyQueue()) {
        cout << "The queue is empty" << endl;
    } else {
        cout << "The queue is NOT empty" << endl;
    }
    getchar();
}

猜你喜欢

转载自blog.csdn.net/zrh_csdn/article/details/81174170