算法笔记-第七章-队列

算法笔记-第七章-队列

队列的相关知识点

大佬的讲解

c++中队列queue用法

一:queue是一种容器转换器模板,调用#include< queue>即可使用队列类

二:使用queue 表示队列

三:相关函数

1.push() 在队尾插入一个元素

2.pop() 删除队列第一个元素

3.size() 返回队列中元素个数

4.empty() 如果队列空则返回true

5.front() 返回队列中的第一个元素

6.back() 返回队列中最后一个元素

队列的操作序列

dart 复制代码
//队列的操作
//简单形式压入和输出
#include <iostream>
#include <string>
#include <queue>
using namespace std;

int main() {
    int n, k;
    cin >> n;

    string action;//这个是输出判断的字符串

    queue<int> q;//定义队列
    for (int i = 0; i < n; i++)
    {
        cin >> action;

        if (action == "push") 
        {
            cin >> k;
            q.push(k);
        }
        else
        {
            if (q.empty()) 
            {
                cout << -1 << endl;   
            }
            else    
            {
                cout << q.front() << endl;   
                q.pop();   
            }
        }
    }
    return 0;   
}

求和队列


dart 复制代码
#include <cstdio>
#include <queue>
using namespace std;

int main() 
{
    int n, x;
    scanf("%d", &n);
    queue<int> q;

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &x);  
        q.push(x);  
    }
    //思路:取出一个数,再取出一个数,然后相加再加入队列中  
    while (q.size() > 1)   
    {
        int front1 = q.front();  
        q.pop();  
        int front2 = q.front();  
        q.pop();  
        q.push(front1 + front2);  
    }
    printf("%d", q.front());  
    return 0;  
}

约瑟夫环-队列


dart 复制代码
//约瑟夫环 - 队列
#include <cstdio>
#include <queue>
using namespace std;

int main() 
{
    int n, k;
    scanf("%d%d", &n, &k);//输入多少个数,匹配到那个位置
    queue<int> q;//队列

    for (int i = 1; i <= n; i++) //先输入数据到队列中
    {
        q.push(i);
    }


    while (!q.empty()) //用一个循环,不断的输出队列里面的数
    {
        for (int i = 0; i < k - 1; i++) //将第k的顺序的数排到队列的第一个位置
        {
            int front = q.front();//输出第一个数   

            q.pop();//删除第一个数操作   

            q.push(front);//在队尾插入第一个数   

        }
        printf("%d", q.front());   
        q.pop();   

        if (!q.empty())    
        {
            printf(" ");   
        }
    }
    return 0;   
}

匹配队列



dart 复制代码
#include <cstdio>
#include <queue>//队列
using namespace std;

int main() 
{
    int n, x;
    scanf("%d", &n);
    queue<int> q1, q2;

    for (int i = 0; i < n; i++) //先搞定队列q1
    {
        scanf("%d", &x);
        q1.push(x);
    }

    for (int i = 0; i < n; i++) //再搞定队列q2
    {
        scanf("%d", &x);
        q2.push(x);
    }


    int counter = 0;//表示需要执行的操作数

    while (!q1.empty()) //这个是以q1为主导的
                        //q2是进行匹配的次要
    {
        if (q1.front() == q2.front()) 
        {
            q1.pop();
            q2.pop();
        }
        else 
        {
            q2.push(q2.front());//匹配不成功那就进行将q2的前面放到后面
            q2.pop();
        }
        counter++;
    }
    printf("%d", counter);//输出匹配的数目
    return 0;
}
相关推荐
weixin_441003644 分钟前
廖华英《中国文化概况》修订版+批注版+译文版+笔记+课件PPT+配套题库 PDF
笔记·pdf·中国文化概况
CyberMuse6 分钟前
408习题集-数据结构101
算法
迷海8 分钟前
力扣原题《有效的数独游戏》,纯手搓,已验证
算法·leetcode·游戏
freshman_y12 分钟前
经典的C语言题型
c语言·开发语言·算法
small_wh1te_coder12 分钟前
拷打字节技术总监: 详解c语言嵌入式多线程编程中的头文件 #总结 上下篇合 #
c语言·开发语言·算法·操作系统·嵌入式
字节高级特工16 分钟前
C++从入门到熟悉:深入剖析const和constexpr
前端·c++·人工智能·后端·算法
Cathy Bryant16 分钟前
聊聊拓扑学
笔记·算法·数学建模·拓扑学·高等数学
Lisssaa16 分钟前
打卡第二十七天
算法
The森21 分钟前
macOS 26(M芯片)部署 cocos2d-x(C++)全链路指南——Xcode + Rosetta
c++·经验分享·笔记·macos·xcode·cocos2d
XWalnut21 分钟前
LeetCode刷题 day2
算法·leetcode·职场和发展