数据结构复习之用两个栈模拟队列操作

简介:

#include<iostream> 
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAXSIZE 100
using namespace std;

struct Stack{
    int s[MAXSIZE];
    int top=0;
    bool stackOverFlow(){
        if(top >= MAXSIZE)
            return true;
        return false; 
    }
    bool push(int x){
        if(stackOverFlow())
            return false;
        s[top++] = x;
    } 
    bool isEmpty(){
        return top==0 ? true : false;
    }
    bool pop(int &x){
        if(isEmpty()) return false;
        x = s[--top];
        return true;
    }
    
    int size(){
        return top;
    } 
};

struct Queue{//这种实现方式也是最容易想到的 
    Stack s1, s2;//s1用于队列的push和pop, s2用于是的缓冲(将s1中的数据进行反转,就成了队列的顺序) 
    bool push(int x){
        if(s1.stackOverFlow()) return false;
        int e;
        while(!s1.isEmpty()){
            s1.pop(e);
            s2.push(e);
        } 
        s1.push(x);
        while(!s2.isEmpty()){
            s2.pop(e);
            s1.push(e);
        }
        return true;
    }
    bool pop(int &x){
        if(s1.isEmpty()) return false;
        s1.pop(x);
        return true;
    }
    
    bool isEmpty(){
        return s1.size() == 0 ? true : false;
    }
    
    int size(){
        return s1.size();
    }
};

struct Queue_x{//这种方式的空间利用率更大一些 
    Stack s1, s2;//s1用于队列的push, s2用于队列的pop 
    bool push(int x){
        if(!s1.stackOverFlow()) {
            s1.push(x);
            return true;
        }
        if(s1.stackOverFlow() && !s2.isEmpty()) return false;
        int e;
        while(!s1.isEmpty()){
            s1.pop(e);
            s2.push(e);
        }
        s1.push(x);
        return true;
    }
    bool pop(int &x){
        if(!s2.isEmpty()){
            s2.pop(x);
            return true;
        }
        if(s1.isEmpty()) return false;
        int e;
        while(!s1.isEmpty()){
            s1.pop(e);
            s2.push(e);
        }
        s2.pop(x);
        return true;
    }
    
    bool isEmpty(){
        return s1.size() == 0 && s2.size() == 0;
    }
    
    int size(){
        return s1.size() + s2.size();
    }
};

int main(){
    Queue q;
    for(int i=0; i<10; ++i)
        q.push(i);
    int x;
    for(int i=0; i<10; ++i){
        q.pop(x);
        cout<<x<<endl;
        q.push(100);
    }
    cout<<"队列的当前大小:"<<q.size()<<endl;
    while(!q.isEmpty()){
        q.pop(x);
        cout<<x<<endl;
    }
    
    cout<<"******************************************************"<<endl;
    Queue_x qx;
    for(int i=0; i<10; ++i)
        qx.push(i);
    for(int i=0; i<10; ++i){
        qx.pop(x);
        cout<<x<<endl;
        qx.push(100);
    }
    cout<<"队列的当前大小:"<<qx.size()<<endl;
    while(!qx.isEmpty()){
        qx.pop(x);
        cout<<x<<endl;
    }
    return 0;
}

目录
相关文章
|
存储
初阶数据结构(六)队列的介绍与实现
初阶数据结构(六)队列的介绍与实现
58 0
|
存储 算法 C语言
数据结构刷题训练:队列实现栈
数据结构刷题训练:队列实现栈
37 0
|
5月前
03(数据结构考研)队列相关操作代码
03(数据结构考研)队列相关操作代码
51 0
|
5月前
02(数据结构考研)栈相关操作代码
02(数据结构考研)栈相关操作代码
22 0
|
9月前
数据结构初阶 队列
数据结构初阶 队列
31 0
|
10月前
|
算法 C语言
速学数据结构 | 用队列实现栈你都被难住了?那是你没掌握好技巧
速学数据结构 | 用队列实现栈你都被难住了?那是你没掌握好技巧
68 0
|
存储
【数据结构】优先级队列(堆)重点知识汇总(附有代码)
【数据结构】优先级队列(堆)重点知识汇总(附有代码)
153 0
|
存储 C语言
初阶数据结构之队列的实现(六)
初阶数据结构之队列的实现(六)
127 0
|
存储
【数据结构】—— 队列基础知识以及数组模拟队列的分析、演示及优化
【数据结构】—— 队列基础知识以及数组模拟队列的分析、演示及优化
78 0
一看就懂之与栈结构(FILO)相对的——队列结构(FLFO)
一、什么是队列,什么是FIFO ​ 队列允许在一端进行插入操作,在另一端进行删除操作的线性表,队列是与栈相对的一个数据结构,栈的特点是先进后出,而队列的特点是先进先出,进行插入操作的一端叫队尾,进行删除的一端叫队头。 正如队列的名字一样,我们假设有一个队列(正在排队的一列队伍),一群人,人们依次进入队列进行排队。