基础数据结构模板
链表
静态链表
单向链表
#include<bits/stdc++.h>
using namespace std;
struct Node{
int num,next;
};
struct node{
Node a[200010];
int head=0,tail=0,len=0;
queue<int> q;
void initList(){//初始化
for(int i=1;i<=200000;i++){
q.push(i);
}
len=0;
}
bool empty(){//判空
return len==0;
}
int size(){//长度
return len;
}
void push_front(int x){//在链表前面插入
int t=q.front();
q.pop();
len++;
a[t].num=x;
a[t].next=head;
head=t;
if(tail==0){
tail=t;
}
}
void push_back(int x){//在链表后面插入
int t=q.front();
q.pop();
a[t].num=x;
a[t].next=0;
if(len!=0){
a[tail].next=t;
}
len++;
tail=t;
if(head==0){
head=t;
}
}
void pop_front(){//删除链表头部
if(len==0){
return;
}
len--;
q.push(head);
if(a[head].next==0){
a[head].num=0;
head=0;
tail=0;
}else{
int t=a[head].next;
a[head].next=0;
a[head].num=0;
head=t;
}
}
void pop_back(){//删除链表尾部
if(len==0){
return;
}
len--;
if(head==tail){
q.push(head);
a[head].num=0;
head=0;
tail=0;
return;
}
int cur=head;
while(a[cur].next!=tail){
cur=a[cur].next;
}
q.push(tail);
a[tail].num=0;
a[cur].next=0;
tail=cur;
}
void insertAfter(int x,int y){//在指定元素后插入
if(head==0){
return;
}
if(x==tail){
push_back(y);
return;
}
int pos=head;
while(pos!=0&&a[pos].num!=x){
pos=a[pos].next;
}
if(pos==0){
return;
}
len++;
int t=q.front();
q.pop();
a[t].num=y;
a[t].next=a[pos].next;
a[pos].next=t;
if(a[t].next==0){
tail=t;
}
}
int front(){//返回头部元素
if(head==0){
return -1;
}
return a[head].num;
}
int back(){//返回尾部元素
if(tail==0){
return -1;
}
return a[tail].num;
}
void eraseValue(int pos){//按值删除
if(len==0||pos==0){
return;
}
if(pos==a[head].num){
pop_front();
return;
}
int cur=head;
while(cur!=0&&a[a[cur].next].num!=pos){
cur=a[cur].next;
}
if(cur==0){
return;
}
len--;
int t=a[a[cur].next].next;
a[a[cur].next].num=0;
a[a[cur].next].next=0;
q.push(a[cur].next);
a[cur].next=t;
if(a[cur].next==0){
tail=cur;
}
}
void eraseAt(int k){//按位置删除
if(k<=0||k>len){
return;
}
if(k==1){
pop_front();
return;
}
int cur=head;
for(int i=2;i<=k-1;i++){
cur=a[cur].next;
}
len--;
int target=a[cur].next;
a[cur].next=a[target].next;
if(target==tail){
tail=cur;
}
q.push(target);
a[target].num=0;
a[target].next=0;
}
void reverse(){//反转链表
if(head==0||a[head].next==0){
return;
}
int prev=0,cur=head,nextnode=0;
while(cur!=0){
nextnode=a[cur].next;
a[cur].next=prev;
prev=cur;
cur=nextnode;
}
int t=head;
head=tail;
tail=t;
}
void clear(){//清空链表
int cur=head;
while(cur!=0){
int nextnode=a[cur].next;
a[cur].num=0;
a[cur].next=0;
q.push(cur);
cur=nextnode;
}
len=0;
head=0;
tail=0;
}
void printList(){//按顺序输出链表
int cur=head;
while(cur!=0){
cout<<a[cur].num<<" -> ";
cur=a[cur].next;
}
cout<<"NULL\n";
}
};
node a;//定义链表
int main(){
a.initList();
if(a.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
a.push_back(10);
a.push_back(20);
a.push_back(30);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.pop_front();
a.pop_front();
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.push_front(40);
a.push_front(50);
a.push_front(60);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.pop_back();
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
if(a.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
a.reverse();
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.eraseAt(2);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.eraseValue(40);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.push_front(40);
a.eraseValue(60);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.eraseValue(100);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.clear();
a.printList();
cout<<a.size()<<endl;
a.push_back(10);
a.push_back(20);
a.push_back(30);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.insertAfter(20,40);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
return 0;
}双向链表
#include<bits/stdc++.h>
using namespace std;
struct Node{
int pre,nxt,num;
};
struct node{
Node a[100010];
int head=0,tail=0,len=0;
queue<int> q;
void initList(){//初始化
for(int i=1;i<=100000;i++){
q.push(i);
}
len=0;
}
bool empty(){//判空
return len==0;
}
int size(){//返回链表长度
return len;
}
void push_front(int x){//从头部插入元素
int t=q.front();
q.pop();
a[t].num=x;
a[t].nxt=head;
a[t].pre=0;
if(len!=0){
a[head].pre=t;
}
head=t;
len++;
if(tail==0){
tail=t;
}
}
void push_back(int x){//从尾部插入元素
int t=q.front();
q.pop();
a[t].num=x;
a[t].nxt=0;
a[t].pre=tail;
if(len!=0){
a[tail].nxt=t;
}
tail=t;
len++;
if(head==0){
head=t;
}
}
void pop_front(){//删除头部元素
if(len==0){
return;
}
len--;
q.push(head);
a[head].num=0;
if(a[head].nxt==0){
head=0;
tail=0;
}else{
int t=a[head].nxt;
a[head].nxt=0;
head=t;
}
}
void pop_back(){//删除尾部元素
if(len==0){
return;
}
len--;
q.push(tail);
a[tail].num=0;
if(a[tail].pre==0){
head=0;
tail=0;
}else{
int t=a[tail].pre;
a[tail].pre=0;
tail=t;
}
}
void insertBefore(int x,int y){//在元素前插入
if(len==0){
return;
}
if(x==head){
push_front(y);
return;
}
int pos=head;
while(pos!=0&&a[pos].num!=x){
pos=a[pos].nxt;
}
if(pos==0){
return;
}
len++;
int t=q.front();
q.pop();
a[t].num=y;
a[t].nxt=pos;
a[t].pre=a[pos].pre;
a[a[pos].pre].nxt=t;
a[pos].pre=t;
}
void insertAfter(int x,int y){//在元素后插入
if(len==0){
return;
}
if(x==tail){
push_back(y);
return;
}
int pos=head;
while(pos!=0&&a[pos].num!=x){
pos=a[pos].nxt;
}
if(pos==0){
return;
}
len++;
int t=q.front();
q.pop();
a[t].num=y;
a[t].nxt=a[pos].nxt;
a[t].pre=pos;
if(a[t].nxt!=0){
a[a[t].nxt].pre=t;
}
a[pos].nxt=t;
if(a[t].nxt==0){
tail=t;
}
}
int front(){//返回头部元素
if(len==0){
return -1;
}
return a[head].num;
}
int back(){//返回尾部元素
if(len==0){
return -1;
}
return a[tail].num;
}
void eraseValue(int pos){//按值删除
if(len==0||pos==0){
return;
}
if(pos==a[head].num){
pop_front();
return;
}
int cur=head;
while(cur!=0&&a[a[cur].nxt].num!=pos){
cur=a[cur].nxt;
}
if(cur==0){
return;
}
len--;
int t=a[a[cur].nxt].nxt;
a[a[cur].nxt].num=0;
a[a[cur].nxt].nxt=0;
a[a[cur].nxt].pre=0;
q.push(a[cur].nxt);
a[cur].nxt=t;
if(a[cur].nxt==0){
tail=cur;
}
}
void eraseAt(int k){//按位置删除
if(k<=0||k>len){
return;
}
if(k==1){
pop_front();
return;
}
int cur=head;
for(int i=2;i<=k-1;i++){
cur=a[cur].nxt;
}
len--;
int t=a[cur].nxt;
a[cur].nxt=a[t].nxt;
if(t==tail){
tail=cur;
}
q.push(t);
a[t].num=0;
a[t].nxt=0;
a[t].pre=0;
}
void reverse(){//反转链表
if(len<=1){
return;
}
int cur=head;
while(cur!=0){
int temp=a[cur].pre;
a[cur].pre=a[cur].nxt;
a[cur].nxt=temp;
cur=a[cur].pre;
}
int temp=head;
head=tail;
tail=temp;
}
void clear(){//清空链表
int cur=head;
while(cur!=0){
int nextnode=a[cur].nxt;
a[cur].num=0;
a[cur].nxt=0;
a[cur].pre=0;
q.push(cur);
cur=nextnode;
}
len=0;
head=0;
tail=0;
}
void printList(){//按顺序输出链表
int cur=head;
while(cur!=0){
cout<<a[cur].num<<" -> ";
cur=a[cur].nxt;
}
cout<<"NULL\n";
}
};
node L;//定义链表
int main(){
L.initList();
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.printList();
L.push_front(5);
L.printList();
L.insertAfter(20,25);
L.printList();
L.insertAfter(30,35);
L.printList();
L.insertBefore(10,8);
L.printList();
L.eraseValue(25);
L.printList();
L.eraseAt(2);
L.printList();
L.pop_front();
L.pop_back();
L.printList();
L.reverse();
L.printList();
cout<<L.front()<<" "<<L.back()<<" "<<L.size()<<endl;
L.clear();
L.printList();
cout<<L.size()<<endl;
return 0;
} 动态链表
单向链表
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
node(int val=0,node *nxt=NULL):data(val),next(nxt){}
void push_front(node *&head,int val){//从前面插入
head=new node(val,head);
}
void push_back(node *&head,int val){//从后面插入
node *newnode=new node(val);
if(head==NULL){
head=newnode;
return;
}
node *cur=head;
while(cur->next!=NULL){
cur=cur->next;
}
cur->next=newnode;
}
void insertAfter(node *cur,int val){//在指定节点后插入
if(cur==NULL){
return;
}
node *newnode=new node(val,cur->next);
cur->next=newnode;
}
void deletenode(node *&head,int val){//按值删除节点
if (head==NULL){
return;
}
if(head->data==val){
node *temp=head;
head=head->next;
delete temp;
return;
}
node *prev=head;
node *cur=head->next;
while(cur!=NULL&&cur->data!=val){
prev=cur;
cur=cur->next;
}
if(cur!=NULL){
prev->next=cur->next;
delete cur;
}
}
node *find(node *head,int val){//按值查找节点
node *cur=head;
while(cur!=NULL) {
if(cur->data==val){
return cur;
}
cur=cur->next;
}
return NULL;
}
void printList(node *head){//正向遍历打印
node *cur=head;
while(cur!=NULL){
cout<<cur->data<<" -> ";
cur=cur->next;
}
cout<<"NULL\n";
}
void freeList(node *&head){//释放整个链表
while (head!=NULL){
node *temp=head;
head=head->next;
delete temp;
}
}
void reverseList(node *&head){//反转链表
if(head==NULL||head->next==NULL){
return;
}
node *prev=NULL;
node *cur=head;
node *nextnode=NULL;
while (cur!=NULL){
nextnode=cur->next;
cur->next=prev;
prev=cur;
cur=nextnode;
}
head=prev;
}
int get_front(node *head){//获取头部元素
if(head==NULL) return -1;
return head->data;
}
int get_back(node *head){//获取尾部元素
if(head==NULL) return -1;
node *cur=head;
while(cur->next!=NULL){
cur=cur->next;
}
return cur->data;
}
};
node *head=NULL;//定义链表
node a;
int main(){
a.push_back(head,10);
a.push_back(head,20);
a.push_back(head,30);
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
a.deletenode(head,10);
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
a.reverseList(head);
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
a.freeList(head);
return 0;
}双向链表
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *prev;
node *next;
node(int val=0,node *p=NULL,node *n=NULL):data(val),prev(p),next(n){}
void insertAfter(node *cur,int val){//在指定节点后插入
if(cur==NULL){
return;
}
node *newnode=new node(val,cur,cur->next);
if(cur->next!=NULL){
cur->next->prev=newnode;
}
cur->next=newnode;
}
void insertBefore(node *cur,int val){//在指定节点前插入
if(cur==NULL){
return;
}
node *newnode=new node(val,cur->prev,cur);
if(cur->prev!=NULL){
cur->prev->next=newnode;
}
cur->prev=newnode;
}
void deletenode(node *cur){//删除指定节点
if(cur==NULL){
return;
}
if(cur->prev!=NULL){
cur->prev->next=cur->next;
}
if(cur->next!=NULL){
cur->next->prev=cur->prev;
}
cur->prev=NULL;
cur->next=NULL;
delete cur;
}
void push_front(node *&head,int val){//从前面插入
if(head==NULL){
head=new node(val);
}else{
insertBefore(head,val);
head=head->prev;
}
}
void push_back(node *&head,int val){//从后面插入
if (head==NULL){
head=new node(val);
return;
}
node *cur=head;
while(cur->next!=NULL){
cur=cur->next;
}
insertAfter(cur,val);
}
node *find(node *head,int val){//按值查找节点
node *cur=head;
while(cur!=NULL){
if(cur->data==val){
return cur;
}
cur=cur->next;
}
return NULL;
}
void printList(node *head){//正向遍历打印
node *cur=head;
while(cur!=NULL){
cout<<cur->data<<" <-> ";
cur=cur->next;
}
cout<<"NULL\n";
}
void freeList(node *head){//释放整个链表
while(head!=NULL){
node *temp=head;
head=head->next;
delete temp;
}
}
int get_front(node *head){//获取头部元素
if(head==NULL){
return -1;
}
return head->data;
}
int get_back(node *head){//获取尾部元素
if(head==NULL){
return -1;
}
node *cur=head;
while(cur->next!=NULL){
cur=cur->next;
}
return cur->data;
}
};
node *head=NULL;//定义链表
node a;
int main(){
a.push_front(head,10);
a.push_front(head,20);
a.push_front(head,30);
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
a.push_back(head,40);
a.push_back(head,50);
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
node *pos=a.find(head,20);
if(pos!=NULL){
a.insertAfter(pos,25);
}
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
node *pos1=a.find(head,40);
if(pos1!=NULL){
a.insertBefore(pos1,35);
}
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
node *pos2=a.find(head,25);
if(pos2!=NULL){
a.deletenode(pos2);
}
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
if(head!=NULL){
node *temp=head;
head=head->next;
a.deletenode(temp);
}
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
node *pos3=a.find(head,50);
if(pos3!=NULL){
a.deletenode(pos3);
}
a.printList(head);
cout<<a.get_front(head)<<endl;
cout<<a.get_back(head)<<endl;
a.freeList(head);
return 0;
}STL 链表
list双向循环链表
#include<bits/stdc++.h>
using namespace std;
list<int> L;//定义list链表
int main(){
L.push_back(10);//尾部插入
L.push_back(20);//尾部插入
L.push_front(5);//头部插入
L.pop_back();//尾部删除
L.pop_front();//头部删除
L.remove(10);//删除链表中所有的指定值
cout<<L.size()<<endl;//输出链表长度
L.push_back(10);//尾部插入
L.push_front(20);//头部插入
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.push_back(30);//尾部插入
L.push_front(40);//头部插入
L.push_front(50);//头部插入
L.push_back(60);//尾部插入
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.sort();//从小到大排序
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.sort(greater<int>());//从大到小排序
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.reverse();//反转链表
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.push_back(10);//尾部插入
L.push_front(10);//头部插入
L.push_back(20);//尾部插入
L.push_back(40);//尾部插入
L.push_front(50);//头部插入
L.push_front(60);//头部插入
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.sort();//从小到大排序
L.unique();//删除连续的重复数字
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
auto it=L.begin();//拿到指向第一个元素的迭代器
advance(it,2);//让迭代器往后走指定步
L.insert(it,99);//在迭代器it的前面插入指定元素
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
L.erase(it);//删掉迭代器指向的那个元素
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
list<int> L2={2,4,6};
L.merge(L2);//转移元素到链表头部
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
list<int> L3={1,3,5};
L.splice(L.end(),L3);//转移元素到链表尾部
cout<<L.front()<<endl;//输出头部元素
cout<<L.back()<<endl;//输出尾部元素
cout<<L.size()<<endl;//输出链表长度
for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素
cout<<*it<<" ";
}
cout<<endl;
return 0;
}队列
普通队列
顺序队列
手写队列
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
int a[1000010],head,tail;
void init(){//初始化
head=1;
tail=0;
}
void clear(){//清空队列
init();
}
void push(int x){//从尾插入
a[++tail]=x;
}
void pop(){//从头弹出
if(head<=tail){
head++;
}
}
int front(){//返回头部元素
if(head>tail){
return -1;
}
return a[head];
}
int back(){//返回尾部元素
if(head>tail){
return -1;
}
return a[tail];
}
int operator[](int x){//返回指定位置元素
if(head+x-1>tail){
return -1;
}
return a[head+x-1];
}
bool empty(){//判空
return head>tail;
}
int size(){//长度
return tail-head+1;
}
void erase(int x){//删除所有指定值元素
int tmp_head=head,tmp_tail=tail;
head=1,tail=0;
for(int i=tmp_head;i<=tmp_tail;i++){
if(a[i]!=x){
push(a[i]);
}
}
}
void reverse(){//反转队列
int l=head,r=tail;
while(l<r){
swap(a[l],a[r]);
l++,r--;
}
}
void print(){//按顺序输出队列元素
for(int i=head;i<=tail;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
};
node q;//定义队列
signed main(){
q.init();
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(40);
q.push(10);
q.push(10);
q.push(30);
cout<<q.front()<<" "<<q.back()<<" "<<q.size()<<endl;
if(q.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
q.pop();
cout<<q[2]<<endl;
cout<<q.front()<<" "<<q.back()<<" "<<q.size()<<endl;
q.print();
q.erase(10);
q.print();
q.reverse();
q.print();
return 0;
}循环队列
手写队列
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
int a[100000],head,tail,cnt;
void init(){//初始化
head=0;
tail=0;
cnt=0;
}
void clear(){//清空队列
init();
}
void push(int x){//从尾插入
a[tail]=x;
tail=(tail+1)%100000;
cnt++;
}
void pop(){//从头弹出
if(cnt>0){
head=(head+1)%100000;
cnt--;
}
}
int front(){//返回头部元素
if(cnt==0){
return -1;
}
return a[head];
}
int back(){//返回尾部元素
if(cnt==0){
return -1;
}
return a[(tail-1+100000)%100000];
}
int operator[](int x){//返回指定位置元素
if(x<1||x>cnt){
return -1;
}
return a[(head+x-1)%100000];
}
bool empty(){//判空
return cnt==0;
}
int size(){//长度
return cnt;
}
void erase(int x){//删除所有指定值元素
int sz=cnt;
int tmp_head=head;
head=0;
tail=0;
cnt=0;
for(int i=0;i<sz;i++){
int val=a[(tmp_head+i)%100000];
if(val!=x){
push(val);
}
}
}
void reverse(){//反转队列
int l=0,r=cnt-1;
while(l<r){
swap(a[(head+l)%100000],a[(head+r)%100000]);
l++;
r--;
}
}
bool find(int x){//查找元素是否存在
for(int i=0;i<cnt;i++){
if(a[(head+i)%100000]==x){
return true;
}
}
return false;
}
int count(int x){//统计元素出现次数
int res=0;
for(int i=0;i<cnt;i++){
if(a[(head+i)%100000]==x){
res++;
}
}
return res;
}
int sum(){//求和
int res=0;
for(int i=0;i<cnt;i++){
res+=a[(head+i)%100000];
}
return res;
}
void q_sort(){//排序
int tmp[100000];
int sz=cnt;
for(int i=0;i<sz;i++){
tmp[i]=a[(head+i)%100000];
}
sort(tmp,tmp+sz);
head=0;
tail=0;
cnt=0;
for(int i=0;i<sz;i++){
push(tmp[i]);
}
}
void unique(){//去连续重复
if(empty()){
return;
}
int sz=cnt;
int tmp_head=head;
head=0;
tail=0;
cnt=0;
int last=a[tmp_head];
push(last);
for(int i=1;i<sz;i++){
int val=a[(tmp_head+i)%100000];
if(val!=last){
push(val);
last=val;
}
}
}
void print(){//按顺序输出队列元素
for(int i=0;i<cnt;i++){
cout<<a[(head+i)%100000]<<" ";
}
cout<<"\n";
}
};
node q;//定义队列
signed main(){
q.init();
q.push(3);
q.push(1);
q.push(4);
q.push(1);
q.push(5);
cout<<q.find(4)<<" "<<q.find(9)<<endl;
cout<<q.count(1)<<" "<<q.count(9)<<endl;
cout<<q.sum()<<endl;
q.reverse();
q.print();
q.q_sort();
q.print();
q.unique();
q.print();
return 0;
}queue循环队列
双端队列
手写双端队列
deque双端队列
单调队列
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,k,s[1000010];
deque<int> a,b;
int main(){
ios::sync_with_stdio(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>s[i];
}
for(int i=1;i<=n;i++){
while(!a.empty()&&s[a.back()]>s[i]){
a.pop_back();
}
a.push_back(i);
if(i>=k){
while(!a.empty()&&a.front()<i-k+1){
a.pop_front();
}
cout<<s[a.front()]<<" ";
}
}
cout<<endl;
for(int i=1;i<=n;i++){
while(!b.empty()&&s[b.back()]<s[i]){
b.pop_back();
}
b.push_back(i);
if(i>=k){
while(!b.empty()&&b.front()<i-k+1){
b.pop_front();
}
cout<<s[b.front()]<<" ";
}
}
cout<<endl;
return 0;
}栈
普通栈
#include<bits/stdc++.h>
using namespace std;
struct node{
int a[100010],cnt=0;
void init(){//初始化
cnt=0;
}
void clear(){//清空栈
cnt=0;
}
void push(int x){//栈顶压入元素
a[++cnt]=x;
}
void pop(){//栈顶弹出元素
if(cnt!=0){
cnt--;
}
}
int top(){//返回栈顶元素
if(cnt==0){
return -1;
}
return a[cnt];
}
int operator[](int x){//返回指定位置元素
if(cnt==0||x>cnt){
return -1;
}
return a[x];
}
bool empty(){//判空
return cnt==0;
}
int size(){//返回长度
return cnt;
}
void erase(int x){//删除所有指定值元素
int tmp=cnt;
cnt=0;
for(int i=1;i<=tmp;i++){
if(a[i]!=x){
push(a[i]);
}
}
}
void reverse(){//反转栈内元素
int l=1,r=cnt;
while(l<r){
swap(a[l],a[r]);
l++,r--;
}
}
void print(){//顺序输出栈内元素
for(int i=1;i<=cnt;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
};
node st;//定义栈
int main(){
st.init();
st.push(10);
st.push(20);
st.push(30);
st.push(40);
st.push(40);
st.push(10);
st.push(10);
st.push(30);
cout<<st.top()<<" "<<st.size()<<endl;
if(st.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
st.pop();
cout<<st[2]<<endl;
cout<<st.top()<<" "<<st.size()<<endl;
st.print();
st.erase(10);
st.print();
st.reverse();
st.print();
st.clear();
if(st.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
return 0;
} 单调栈
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,s[100010],ans[100010];
int main(){
ios::sync_with_stdio(0);
stack<int> a;
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
}
for(int i=n;i>=1;i--){
while(!a.empty()&&s[a.top()]<=s[i]){
a.pop();
}
if(a.empty()){
ans[i]=0;
}else{
ans[i]=a.top();
}
a.push(i);
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<endl;
}
return 0;
}