数据结构

· · 个人记录

由于我发现数据结构学一边忘一遍,故,第四次忍界大战我在此宣战(bushi)在此记下模板,以便复习。

单链表

#include <bits/stdc++.h>
using namespace std;
const int MAXN=100010;
int head,e[MAXN],ne[MAXN],idx;
void init()
{
    head=-1;
    idx=0;
}
void add(int k,int x)
{
    e[idx]=x;
    ne[idx]=ne[k];
    ne[k]=idx;
    idx++;
}
void add_to_head(int x)
{
    e[idx]=x;
    ne[idx]=head;
    head=idx;
    idx++;
}
void remove(int k)
{
    ne[k]=ne[ne[k]];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    int n;
    cin>>n;
    while(n--)
    {
        char s;
        int k,x;
        cin>>s;
        if(s=='H')
        {
            cin>>x;
            add_to_head(x);
        }
        else if(s=='D')
        {
            cin>>k;
            if(!k)head=ne[head]; 
            remove(k-1);
        }
        else if(s=='I')
        {
            cin>>k>>x;
            add(k-1,x);
        }
    }
    for(int i=head;i!=-1;i=ne[i])
    {
        cout<<e[i]<<" ";
    }
    return 0;
}

双链表

#include <bits/stdc++.h>
using namespace std;
const int MAXN=100010;
int e[MAXN],r[MAXN],l[MAXN],idx;
void init()
{
    r[0]=1;
    l[1]=0;
    idx=2;
}
void add(int k,int x)
{
    e[idx]=x;
    r[idx]=r[k];
    l[idx]=k;
    l[r[k]]=idx;
    r[k]=idx;
    idx++;
}
void remove(int k)
{
    r[l[k]]=r[k];
    l[r[k]]=l[k];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    int n;
    cin>>n;
    while(n--)
    {
    string s;
    int k,x;
    cin>>s;
    if(s=="L")
    {
        cin>>x;
        add(0,x);
    }   
    else if(s=="R")
    {
        cin>>x;
        add(l[1],x);
    }
    else if(s=="D")
    {
        cin>>k;
        remove(k+1);
    }
    else if(s=="IL")
    {
        cin>>k>>x;
        add(l[k+1],x);
    }
    else
    {
        cin>>k>>x;
        add(k+1,x);
    }
    }
    for(int i=r[0];i!=1;i=r[i])
    {
        cout<<e[i]<<" ";
    }
    return 0;
}

模拟栈

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
int stk[MAXN];
int cnt=0;
void push(int x)
{
    stk[++cnt]=x;
} 
void pop()
{
    cnt--;
}
bool empty()
{
    if(cnt<=0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
int query()
{
    return stk[cnt];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m;
    cin>>m;
    while(m--)
    {
        string s;
        cin>>s;
        int x;
        if(s=="push")
        {
            cin>>x;
            push(x);
        }
        else if(s=="pop")
        {
            pop();
        }
        else if(s=="empty")
        {
            if(empty()==0)
            {
                cout<<"YES"<<endl;
            }
            else
            {
                cout<<"NO"<<endl;
            }
        }
        else if(s=="query")
        {
            cout<<query()<<endl;
        }
    }
    return 0;
}

模拟队列

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
int q[MAXN];
int hh,tt=-1;
void push(int x)
{
    q[++tt]=x;
}
void pop()
{
    hh++;
}
bool empty()
{
    if(hh>tt)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
int query()
{
    return q[hh];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m;
    cin>>m;
    while(m--)
    {
        string s;
        cin>>s;
        int x;
        if(s=="push")
        {
            cin>>x;
            push(x);
        }
        else if(s=="pop")
        {
            pop();
        }
        else if(s=="empty")
        {
            if(empty()==0)
            {
                cout<<"YES"<<endl;
            }
            else
            {
                cout<<"NO"<<endl;
            }
        }
        else if(s=="query")
        {
            cout<<query()<<endl;
        }
    }
    return 0;
}

单调栈

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
int stk[MAXN];
int cnt=0;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    int x;
    while(n--)
    {
        cin>>x;
        while(cnt&&stk[cnt]>=x)
        {
            cnt--;
        }
        if(cnt<=0)
        {
            cout<<-1<<" ";
        }
        else
        {
            cout<<stk[cnt]<<" ";
        }
        stk[++cnt]=x;
    }
    return 0;
}

单调队列

#include <bits/stdc++.h>
using namespace std; 
const int MAXN=1000010;
int n,k;
int a[MAXN];
int q[MAXN];
void get_min()
{
    int hh=0;  // 队列头指针(队首)
    int tt=-1; // 队列尾指针(队尾)
    for(int i=0;i<n;i++)  // 遍历数组每个元素(i是当前窗口右边界)
    {
        // 步骤1:移除超出窗口范围的队首元素
        // 窗口左边界 = i - k + 1,若队首下标 < 左边界,说明不在窗口内,弹出
        if(hh<=tt&&i-k+1>q[hh])
        {
            hh++;
        }

        // 步骤2:维护队列单调性(递增)
        // 若队尾元素对应的值 >= 当前值a[i],则队尾元素不可能是后续窗口的最小值,直接弹出
        while(hh<=tt&&a[q[tt]]>=a[i])
        {
            tt--;
        }

        // 步骤3:将当前元素下标加入队尾
        q[++tt]=i;

        // 步骤4:当窗口长度达到k时,输出队首(当前窗口最小值)
        if(i>=k-1)  // i从0开始,i=k-1时第一个窗口形成(0~k-1)
        {
            cout<<a[q[hh]]<<" ";
        }
    }
    cout<<endl;
}
void get_max()
{
    int hh=0;  // 队列头指针(队首,指向当前窗口最大值的下标)
    int tt=-1; // 队列尾指针(队尾,初始为空)
    for(int i=0;i<n;i++)  // i是当前窗口的右边界,遍历数组每个元素
    {
        // 步骤1:剔除超出窗口范围的队首元素
        // 窗口左边界 = i - k + 1;如果队首下标 < 左边界,说明不在窗口内,弹出
        if(hh<=tt&&i-k+1>q[hh])
        {
            hh++;
        }

        // 步骤2:维护队列的单调递减性(核心)
        // 只要队列非空,且队尾下标对应的值 <= 当前值a[i]
        // 说明队尾元素不可能成为后续窗口的最大值,直接弹出队尾
        while(hh<=tt&&a[q[tt]]<=a[i])
        {
            tt--;
        }

        // 步骤3:将当前元素下标加入队尾
        // 此时队尾元素对应的值一定 > a[i],队列保持递减
        q[++tt]=i;

        // 步骤4:当窗口长度达到k时,输出队首(当前窗口的最大值)
        // i从0开始,i >= k-1 时,第一个完整窗口(0~k-1)形成
        if(i>=k-1)
        {
            cout<<a[q[hh]]<<" ";
        }
    }
    cout<<endl;
}
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    get_min();
    get_max();
    return 0;
}