C++ 双端队列

双端队列的基本操作

pop_back()    	//尾部删除
pop_front()		//头部删除
push_back()		//尾部插入
push_front()	//头部插入
insert()		//指定位置插入
erase();		//指定位置删除
clear()			//清空队列

#include "deque.h"
#include <deque>
#include <iostream>
using namespace std;

/*
基本函数
pop_back()
pop_front()
push_back()
push_front()
insert()
erase();
clear()
*/
//使用push_back()方法从尾部插入元素,会不断扩张队列。
void tail_insert()
{

    deque<int>q;
    for(int i = 0;i<5;i++)
    {
        q.push_back(i);
    }
    for(int i = 0;i<5;i++)
    {
        cout<<q[i]<<" ";
    }
    cout<<endl;

}

//从头部插入元素,也会扩张队列。
void front_insert()
{
    deque<int>q;
    q.push_back(1);
    q.push_back(2);
    q.push_back(3);
    q.push_front(10);  //相当于q.insert(q.begin()+1,10);
    q.push_front(20);  //相当于q.insert(q.begin()+2,10);

    for(int i = 0;i<q.size();i++)
    {
        cout<<q[i]<<" ";
    }
    cout<<endl;
}

void deque_traverse()
{
    deque<int>q;
    q.push_back(1);
    q.push_back(2);
    q.push_back(3);
    q.push_front(10);  //相当于q.insert(q.begin()+1,10);
    q.push_front(20);  //相当于q.insert(q.begin()+2,10);

    //顺序遍历
deque<int>::iterator it;
for(it =q.begin();it!= q.end();it++ )
{
    cout<<*it<<" ";
}
cout<<endl;

//逆序遍历
deque<int>::reverse_iterator rit;
for(rit  =q.rbegin();rit != q.rend();rit++)
{
     cout<<*rit<<" ";
}
cout<<endl;
}


//可以从双端队列的手部,尾部,中部删除元素,并可以清空双端队列容器
void control_deque()
{

    deque<int>q;
    q.push_back(1);
    q.push_back(2);
    q.push_back(3);
    q.push_front(10);  //相当于q.insert(q.begin()+1,10);
    q.push_front(20);  //相当于q.insert(q.begin()+2,10);

    cout<<" control_deque():";
    deque<int>::iterator it;
    for(it =q.begin();it!= q.end();it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    //删除队尾元素
    q.pop_back();
    cout<<" pop_back():";

    for(it =q.begin();it!= q.end();it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    //删除队首元素
    q.pop_front();
    cout<<" pop_front():";
    for(it =q.begin();it!= q.end();it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    //删除队列中下标为2的位置元素 参数为指针
    q.erase(q.begin()+2);
    cout<<" erase():";

    for(it =q.begin();it!= q.end();it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    //在下标为1处添加新元素8
    q.insert(q.begin()+1,8);
    cout<<" insert():";
    for(it =q.begin();it!= q.end();it++ )
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    //清空队列
    q.clear();
    cout<<" clear():";
    cout<<" size():"<<q.size();
    cout<<endl;

}
void deque_test()
{
tail_insert();

front_insert();

deque_traverse();

control_deque();
}

int main()
{
    deque_test();
    return 0;
}

输出

0 1 2 3 4
20 10 1 2 3
20 10 1 2 3
3 2 1 10 20
 control_deque():20 10 1 2 3
 pop_back():20 10 1 2
 pop_front():10 1 2
 erase():10 1
 insert():10 8 1
 clear(): size():0
以下是用C++实现双端队列的代码和详细步骤: ```cpp #include <iostream> using namespace std; #define MAXSIZE 100 // 定义双端队列的最大长度 class Deque { private: int data[MAXSIZE]; // 双端队列的数据存储数组 int left; // 左指针 int right; // 右指针 public: Deque() { // 构造函数,初始化左右指针 left = 0; right = 0; } bool isEmpty() { // 判断队列是否为空 return left == right; } bool isFull() { // 判断队列是否已满 return (right + 1) % MAXSIZE == left; } void push_front(int x) { // 在队头插入元素 if (isFull()) { cout << "Deque is full!" << endl; return; } left = (left - 1 + MAXSIZE) % MAXSIZE; data[left] = x; } void push_back(int x) { // 在队尾插入元素 if (isFull()) { cout << "Deque is full!" << endl; return; } data[right] = x; right = (right + 1) % MAXSIZE; } void pop_front() { // 在队头删除元素 if (isEmpty()) { cout << "Deque is empty!" << endl; return; } left = (left + 1) % MAXSIZE; } void pop_back() { // 在队尾删除元素 if (isEmpty()) { cout << "Deque is empty!" << endl; return; } right = (right - 1 + MAXSIZE) % MAXSIZE; } int front() { // 返回队头元素 if (isEmpty()) { cout << "Deque is empty!" << endl; return -1; } return data[left]; } int back() { // 返回队尾元素 if (isEmpty()) { cout << "Deque is empty!" << endl; return -1; } return data[(right - 1 + MAXSIZE) % MAXSIZE]; } }; int main() { Deque dq; dq.push_front(1); dq.push_back(2); dq.push_front(3); dq.push_back(4); cout << dq.front() << endl; // 输出:3 cout << dq.back() << endl; // 输出:4 dq.pop_front(); dq.pop_back(); cout << dq.front() << endl; // 输出:1 cout << dq.back() << endl; // 输出:2 return 0; } ``` 步骤: 1. 定义一个常量MAXSIZE,表示双端队列的最大长度。 2. 定义一个类Deque,包含数据存储数组data和左右指针left、right。 3. 构造函数初始化左右指针。 4. 定义isEmpty()和isFull()函数,分别判断队列是否为空和已满。 5. 定义push_front()和push_back()函数,在队头和队尾插入元素。 6. 定义pop_front()和pop_back()函数,在队头和队尾删除元素。 7. 定义front()和back()函数,分别返回队头和队尾元素。 8. 在main函数创建Deque对象dq,测试双端队列的各种操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael.Scofield

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值