【力扣】栈&队列——笔记

初始化

在C++中用到的class{};里,private中只能定义为vector Mylist;
需要在后续的public里的函数中,初始化函数里确定规模Mylist.resize(1000);
public中可以定义vector a(1000);

class MyCircularQueue {
private:
	vector<int> Mylist;
	int head;
	int tail;
	int size;
	int ishead;
public:
	/** Initialize your data structure here. Set the size of the queue to be k. */
	MyCircularQueue(int k) {
		Mylist.resize(k);
		vector<int> data(100, 0);
		head = 0;
		tail = 0;
		size = k;
		ishead = 1;
	}
}

在C语言中的int main(){}中可以定义为vector Mylist(1000);//表示vector的规模

#include<stdio.h>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
	int a,b;
	int i,deng_num,cnt=0;
	scanf("%d %d",&a,&b);
    vector<int> mask;
	vector<bool> temp(b+1,0);
	vector<int> out;
}

我自己理解的循环队列判满、判空

判满:当尾指针追上头指针
判空:当头指针追上尾指针
所以我那个ishead表明,是在做出队操作导致头追上尾,还是在做入队操作导致尾追上头。

内置队列库

一、添加头文件
#include<iostream>
#include<queue>
using namespace std;

二、操作
	//1. 初始化.
	queue <int>q;
	//2. 入队.
    q.push(5);
    q.push(13);
    q.push(8);
    q.push(6);
    // 3. 判空
    if (q.empty()) {
        cout << "Queue is empty!" << endl;
        return 0;
    }
    // 4. 出队.
    q.pop();
    // 5. 得到头元素.
    cout << "The first element is: " << q.front() << endl;
    // 6. 得到尾元素.
    cout << "The last element is: " << q.back() << endl;
    // 7. 得到队列大小.
    cout << "The size is: " << q.size() << endl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值