当前位置:   article > 正文

【力扣】栈&队列——笔记_力扣只能用vector吗

力扣只能用vector吗

初始化

在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;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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

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

内置队列库

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

  • 1
  • 2
  • 3
  • 4
二、操作
	//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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/588102
推荐阅读
相关标签
  

闽ICP备14008679号