初始化
在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;