跳到正文

算法·理论

基础数据结构模板

mountain_ZGY约 36 分钟
原文

这是 2026年8月13日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  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";
	}
};
int main(){
    node a;//定义链表
    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";
	}
};
int main(){
	node L;
	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){}
    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;
    }
};
int main(){
	node *head=NULL;//定义链表
	node a;
	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;
}
双向链表
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;
	}
};
int main(){
	node *head=NULL;//定义链表
	node a;
	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;
int main(){
    list<int> L;//定义list链表
    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;
}