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

简介:

#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;
}

目录
相关文章
|
缓存
指令重排序的探讨
指令重排序是现代处理器为了提高指令级并行性和性能而进行的一种优化技术。在高并发场景下,指令重排序可能会引发一些问题,本文将详细介绍指令重排序的概念、原因、影响以及如何解决这些问题。
372 0
|
Docker 容器
docker swarm 移除 Worker 节点
【10月更文挑战第12天】
229 5
|
前端开发 数据可视化 JavaScript
探索前端可视化开发:低代码平台原理与实践
【4月更文挑战第7天】本文探讨了低代码平台在前端开发中的应用,介绍了其模型驱动、组件化和自动化部署的原理,强调了提升效率、降低技术门槛、灵活适应变更和保证一致性等优势。建议开发者明确适用场景,选择合适平台,并培养团队低代码技能,同时规划与现有技术栈的融合,实施持续优化治理。低代码平台正改变开发格局,为业务创新和数字化转型提供新途径。
557 0
|
存储 网络协议 数据可视化
C++实现socket通信
了解如何实现socket通信以及TCP连接的过程中发生了什么
347 1
|
缓存 网络协议 算法
Golang简单实现 分布式缓存+一致性哈希+节点再平衡(gossip + consistent + rebalance)
Golang简单实现 分布式缓存+一致性哈希+节点再平衡(gossip + consistent + rebalance)
416 0
|
关系型数据库 MySQL 测试技术
MySQL 报错 ERROR 1709: Index column size too large
MySQL 报错 ERROR 1709: Index column size too large
638 4
|
Go
动态并发控制:sync.WaitGroup的灵活运用
动态并发控制:sync.WaitGroup的灵活运用
320 0
动态并发控制:sync.WaitGroup的灵活运用
|
前端开发 JavaScript 中间件
基于最新koa的Node.js后端API架构与MVC模式
基于最新koa的Node.js后端API架构与MVC模式
401 1
|
安全 Unix Linux
【C/C++ 字符串】探索C语言之字符串分割函数:strtok和strsep的区别
【C/C++ 字符串】探索C语言之字符串分割函数:strtok和strsep的区别
592 0
|
机器学习/深度学习 数据可视化 数据挖掘
【视频】结构方程模型SEM分析心理学营销数据路径图可视化|数据分享
【视频】结构方程模型SEM分析心理学营销数据路径图可视化|数据分享