跳到正文

算法·理论

基础数据结构模板

mountain_ZGY约 65 分钟
原文

这是 2026年10月4日 存档的第 4 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月10日
收录
2026年8月13日
更新
2026年11月19日
篇幅
约 65 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1

基础数据结构模板

链表
静态链表
单向链表
C++
#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;
}
双向链表
C++
#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;
} 
动态链表
单向链表
C++
#include<bits/stdc++.h>
using namespace std;
struct Node{
    int data;
    Node *next;
    Node(int val=0, Node *nxt=NULL):data(val),next(nxt){}
};
struct LinkedList{
    Node *head;
    LinkedList():head(NULL){}
    void push_front(int val){//从前面插入
        head=new Node(val,head);
    }
    void push_back(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(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(int val){//按值查找节点
        Node *cur=head;
        while(cur!=NULL){
            if(cur->data==val){
                return cur;
            }
            cur=cur->next;
        }
        return NULL;
    }
    void printList(){//正向遍历打印
        Node *cur=head;
        while(cur!=NULL){
            cout<<cur->data<<" -> ";
            cur=cur->next;
        }
        cout<<"NULL\n";
    }
    void freeList(){//释放整个链表
        while(head!=NULL){
            Node *temp=head;
            head=head->next;
            delete temp;
        }
    }
    void reverseList(){//反转链表
        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(){//获取头部元素
        if(head==NULL) return -1;
        return head->data;
    }
    int get_back(){//获取尾部元素
        if(head==NULL) return -1;
        Node *cur=head;
        while(cur->next!=NULL){
            cur=cur->next;
        }
        return cur->data;
    }
};
int main(){
    LinkedList a;
    a.push_back(10);
    a.push_back(20);
    a.push_back(30);
    a.printList();
    cout<<a.get_front()<<endl;
    cout<<a.get_back()<<endl;
    a.deletenode(10);
    a.printList();
    cout<<a.get_front()<<endl;
    cout<<a.get_back()<<endl;
    a.reverseList();
    a.printList();
    cout<<a.get_front()<<endl;
    cout<<a.get_back()<<endl;
    a.freeList();
    return 0;
}
双向链表
C++
#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双向循环链表
C++
#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;
}
队列
普通队列
顺序队列
手写队列
C++
#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;
}
循环队列
手写队列
C++
#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循环队列
C++
#include<bits/stdc++.h>
using namespace std;
queue<int> q,q2;//定义队列
int main(){
    q.push(10);//尾部插入
    q.push(20);//尾部插入
    q.push(30);//尾部插入
    cout<<q.front()<<endl;//输出队首元素
    cout<<q.back()<<endl;//输出队尾元素
    cout<<q.size()<<endl;//输出队列长度
    q.pop();//头部删除
    cout<<q.front()<<endl;//输出队首元素
    cout<<q.back()<<endl;//输出队尾元素
    cout<<q.size()<<endl;//输出队列长度
    if(q.empty()){//判空
        cout<<"Empty"<<endl;
    }else{
        cout<<"Not Empty"<<endl;
    }
    q2.push(100);//尾部插入
    q2.push(200);//尾部插入
    q.swap(q2);//交换两个队列
    cout<<q.front()<<endl;//输出交换后q的队首元素
    cout<<q2.front()<<endl;//输出交换后q2的队首元素
    while(!q.empty()){//清空队列
        q.pop();
    }
    cout<<q.size()<<endl;//输出清空后的队列长度
    return 0;
}
双端队列
手写双端队列
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
    int a[1000010];
    int head,tail;
    void init(){//初始化
        head=500000;
        tail=500000;
    }
    void clear(){//清空队列
        init();
    }
    void push_front(int x){//从头部插入
        a[--head]=x;
    }
    void push_back(int x){//从尾部插入
        a[tail++]=x;
    }
    void pop_front(){//从头部弹出
        if(head<tail){
            head++;
        }
    }
    void pop_back(){//从尾部弹出
        if(head<tail){
            tail--;
        }
    }
    int front(){//返回头部元素
        if(head>=tail){
            return -1;
        }
        return a[head];
    }
    int back(){//返回尾部元素
        if(head>=tail){
            return -1;
        }
        return a[tail-1];
    }
    int operator[](int x){//返回指定位置元素(从1开始)
        if(head+x-1>=tail){
            return -1;
        }
        return a[head+x-1];
    }
    bool empty(){//判空
        return head>=tail;
    }
    int size(){//返回长度
        return tail-head;
    }
    void print(){//按顺序输出元素
        for(int i=head;i<tail;i++){
            cout<<a[i]<<" ";
        }
        cout<<"\n";
    }
};
node dq;//定义双端队列
signed main(){
    dq.init();
    dq.push_back(10);
    dq.push_back(20);
    dq.push_front(5);
    dq.push_front(1);
    cout<<dq.front()<<endl;
    cout<<dq.back()<<endl;
    cout<<dq.size()<<endl;
    dq.print();
    dq.pop_front();
    dq.pop_back();
    cout<<dq.front()<<endl;
    cout<<dq.back()<<endl;
    cout<<dq.size()<<endl;
    dq.print();
    if(dq.empty()){
        cout<<"Empty"<<endl;
    }else{
        cout<<"Not Empty"<<endl;
    }
    cout<<dq[1]<<endl;
    dq.clear();
    cout<<dq.size()<<endl;
    return 0;
}
deque双端队列
C++
#include<bits/stdc++.h>
using namespace std;
deque<int> dq;//定义双端队列
int main(){
    dq.push_back(10);//尾部插入
    dq.push_back(20);//尾部插入
    dq.push_front(5);//头部插入
    dq.push_front(1);//头部插入
    cout<<dq.front()<<endl;//输出队首元素
    cout<<dq.back()<<endl;//输出队尾元素
    cout<<dq.size()<<endl;//输出队列长度
    dq.pop_front();//头部删除
    dq.pop_back();//尾部删除
    cout<<dq.front()<<endl;//输出删除后的队首元素
    cout<<dq.back()<<endl;//输出删除后的队尾元素
    cout<<dq.size()<<endl;//输出队列长度
    if(dq.empty()){//判空
        cout<<"Empty"<<endl;
    }else{
        cout<<"Not Empty"<<endl;
    }
    cout<<dq[0]<<endl;//按下标访问(从0开始)
    dq.insert(dq.begin()+1,99);//在指定位置插入
    cout<<dq[1]<<endl;//输出插入后的元素
    dq.erase(dq.begin()+1);//删除指定位置元素
    cout<<dq[1]<<endl;//输出删除后的元素
    dq.clear();//清空队列
    cout<<dq.size()<<endl;//输出清空后的队列长度
    return 0;
}
单调队列
C++
#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;
}
栈
普通栈
手写栈
C++
#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;
} 
stack栈
C++
#include<bits/stdc++.h>
using namespace std;
stack<int> st,st2;//定义栈
int main(){
    st.push(10);//栈顶压入元素
    st.push(20);//栈顶压入元素
    st.push(30);//栈顶压入元素
    cout<<st.top()<<endl;//输出栈顶元素
    cout<<st.size()<<endl;//输出栈内元素个数
    st.pop();//弹出栈顶元素
    cout<<st.top()<<endl;//输出弹出后的栈顶元素
    cout<<st.size()<<endl;//输出栈内元素个数
    if(st.empty()){//判空
        cout<<"Empty"<<endl;
    }else{
        cout<<"Not Empty"<<endl;
    }
    st2.push(100);
    st2.push(200);
    st.swap(st2);//交换两个栈
    cout<<st.top()<<endl;//输出交换后st的栈顶元素
    cout<<st2.top()<<endl;//输出交换后st2的栈顶元素
    while(!st.empty()){//清空栈
        st.pop();
    }
    cout<<st.size()<<endl;//输出清空后的栈大小
    return 0;
}
单调栈
C++
#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;
}
二叉树和哈夫曼树
二叉树
二叉树的遍历
深度优先搜索
先序遍历
C++
#include<bits/stdc++.h>
using namespace std;
struct into{
	int l,r;
}a[1030];
int n,t;
void p1(int p){
    printf("%d ",p);
	if(a[p].l!=0){
		p1(a[p].l);
	}
	if(a[p].r!=0){
		p1(a[p].r);
	}
}
int main(){
	ios::sync_with_stdio(0);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].l,&a[i].r);
	}
	t=0;
	p1(1);
    return 0;
}
中序遍历
C++
#include<bits/stdc++.h>
using namespace std;
struct into{
	int l,r;
}a[1030];
int n,t;
void p2(int p){
	if(a[p].l!=0){
		p2(a[p].l);
	}
    printf("%d ",p);
	if(a[p].r!=0){
		p2(a[p].r);
	}
}
int main(){
	ios::sync_with_stdio(0);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].l,&a[i].r);
	}
	t=0;
	p2(1);
    return 0;
}
后序遍历
C++
#include<bits/stdc++.h>
using namespace std;
struct into{
	int l,r;
}a[1030];
int n,t;
void p3(int p){
	if(a[p].l!=0){
		p3(a[p].l);
	}
	if(a[p].r!=0){
		p3(a[p].r);
	}
	printf("%d ",p);
}
int main(){
	ios::sync_with_stdio(0);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].l,&a[i].r);
	}
	t=0;
	p3(1);
    return 0;
}
先序中序转后序
C++
#include<bits/stdc++.h>
using namespace std;
int n,a[1030],b[1030];
void dfs(int l1,int r1,int l2,int r2){
	if(l1>r1){
		return;
	}
	if(l1==r1){
		printf("%d ",a[l1]);
		return;
	}
	int x;
	for(int i=l2;i<=r2;i++){
		if(b[i]==a[l1]){
			x=i;
			break;
		}
	}
	dfs(l1+1,l1+x-l2,l2,x-1);
	dfs(l1+x-l2+1,r1,x+1,r2);
	printf("%d ",a[l1]);
}
int main(){
	ios::sync_with_stdio(0);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&b[i]);
	}
	dfs(1,n,1,n);
	return 0;
}
中序后序转先序
C++
#include<bits/stdc++.h>
using namespace std;
int n,a[1030],b[1030];
void dfs(int l1,int r1,int l2,int r2){
	if(l1>r1){
		return;
	}
	if(l1==r1){
		printf("%d ",a[l1]);
		return;
	}
	printf("%d ",b[r2]);
	int x=0;
	for(int i=l1;i<=r1;i++){
		if(a[i]==b[r2]){
			x=i;
			break;
		}
	}
	dfs(l1,x-1,l2,l2+x-1-l1);
	dfs(x+1,r1,l2+x-l1,r2-1);
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	dfs(1,n,1,n);
	return 0;
}
广度优先搜索
C++
#include<bits/stdc++.h>
using namespace std;
struct node{
	int l,r;
}a[1025];
int n;
void bfs(){
	queue<int> q;
	q.push(1);
	while(!q.empty()){
		int t=q.front();
		q.pop();
		cout<<t<<" ";
		if(a[t].l!=0){
			q.push(a[t].l);
		}
		if(a[t].r!=0){
			q.push(a[t].r);
		}
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].l>>a[i].r;
	}
	bfs();
	return 0;
}
哈夫曼树