数据结构模板整合

#include<iostream>
#include<cstdio>
#define maxn 100010

using namespace std;

int n,x;

template<typename type>
inline void read(type &x)
{
    x=0;bool flag(0);char ch=getchar();
    while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    flag?x=-x:0;
}

template<typename type>
inline void write(type x,bool flag=1)
{
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10;while(x);
    while(top) putchar(Stack[top--]|48);
    flag?putchar('\n'):putchar(' ');
}

class stack
{
    public:
        stack(){n=0;}
        void push(int x){a[++n]=x;}
        void pop(){n--;}
        int top(){return a[n];}
        int size(){return n;}
        bool empty(){return !n;}
        void traversal(){for(int i=1;i<=n;i++)write(a[i],0);puts("");}
    private:
        int n;
        int a[maxn];
}s;

signed main()
{
    read(n);
    for(int i=1;i<=n;++i) read(x),s.push(x);
    s.traversal();
    return 0;
}

队列

#include<iostream>
#include<cstdio>
#include<queue>
#define maxn 100010

using namespace std;

int n,x;

template<typename type>
inline void read(type &x)
{
    x=0;bool flag(0);char ch=getchar();
    while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    flag?x=-x:0;
}

template<typename type>
inline void write(type x,bool flag=1)
{
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10;while(x);
    while(top) putchar(Stack[top--]|48);
    flag?putchar('\n'):putchar(' ');
}

class queue
{
    public:
        queue(){head=tail=cnt=0;}
        void push(int x){a[tail]=x,tail=(tail+1)%maxn,cnt++;}
        void pop(){head=(head+1)%maxn,cnt--;}
        int front(){return a[head];}
        int back(){return a[tail];}
        int size(){return cnt;}
        bool empty(){return !cnt;}
        void traversal(){for(int i=0;i<cnt;++i)write(a[(head+i)%(tail+1)],0);puts("");}
    private:
        int head,tail,cnt,a[maxn];
}q;

signed main()
{
    read(n);
    for(int i=1;i<=n;++i) read(x),q.push(x);
    q.traversal();
    return 0;
}

双端队列

#include<iostream>
#include<cstdio>
#include<queue>
#define maxn 100010

using namespace std;

int n,x;

template<typename type>
inline void read(type &x)
{
    x=0;bool flag(0);char ch=getchar();
    while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    flag?x=-x:0;
}

template<typename type>
inline void write(type x,bool flag=1)
{
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10;while(x);
    while(top) putchar(Stack[top--]|48);
    flag?putchar('\n'):putchar(' ');
}

class deque
{
    private:
        int head,tail,n;
    public:
        int a[maxn];
        deque(){head=tail=n=0;}
        void push_front(int x){head=(head?head-1:maxn-1),a[head]=x,n++;}
        void push_back(int x){a[tail]=x,tail=(++tail^maxn?tail:0),n++;}
        void pop_front(){head=(++head^maxn?head:0),n--;}
        void pop_back(){tail=(tail?tail-1:maxn-1),n--;}
        int front(){return a[head];}
        int back(){return a[tail?tail-1:maxn-1];}
        int& operator [](int i){return a[head+i-1-(head+i-1<maxn?0:maxn)];}
        int size(){return n;}
        bool empty(){return !n;}
        void traversal(){for(int i=0;i<n;++i)write(a[head+i-(head+i<maxn?0:maxn)],0);puts("");}
}dq;


signed main()
{
    read(n);
    for(int i=1;i<=n;++i) read(x),dq.push_back(x);
    dq.traversal();
    return 0;
}

链表

#include<iostream>
#include<cstdio>

using namespace std;

int n,x;

template<typename type>
inline void read(type &x)
{
    x=0;bool flag(0);char ch=getchar();
    while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    flag?x=-x:0;
}

template<typename type>
inline void write(type x,bool mode=1)
{
    x<0?x=-x,putchar(' '):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10;while(x);
    while(top) putchar(Stack[top--]|48);
    mode?putchar('\n'):putchar(' ');
}

class node
{
    public:
        int val;
        node *pre=NULL,*next=NULL;
};

class List
{
    public:
        node *head,*tail;
        List(){head=new node(),tail=new node(),head->next=tail,tail->pre=head;}
        ~List(){while(head!=tail)head=head->next,delete head->pre;delete tail;}
        void insert(node *p,int x)//insert x after p
        {
            node *t=new node();
            t->val=x,t->pre=p,t->next=p->next;
            p->next->pre=t,p->next=t;
        }
        void erase(node *p)
        {
            p->pre->next=p->next;
            p->next->pre=p->pre;
            delete p;
        }
}list;

signed main()
{
    read(n);
    for(int i=1;i<=n;++i) read(x),list.insert(list.tail->pre,x);
    for(node *p=list.head->next;p!=list.tail;p=p->next) write(p->val,0);
    return 0;
}

二叉堆

#include<iostream>
#include<cstdio>
#define maxn 100010

using namespace std;
namespace ly
{
    auto swap=[](auto &a,auto &b){auto t=a;a=b,b=t;};
}

int n,x;

template<typename type>
inline void read(type &x)
{
    x=0;bool flag(0);char ch=getchar();
    while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    flag?x=-x:0;
}

template<typename type>
inline void write(type x,bool mode=1)
{
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10;while(x);
    while(top) putchar(Stack[top--]|48);
    mode?putchar('\n'):putchar(' ');
}

class heap
{
    public:
        heap(bool mode=0){type=mode,n=0;}
        void up(int i){while(i^1&&a[i]^a[i>>1]&&type^(a[i]<a[i>>1]))ly::swap(a[i],a[i>>1]),i>>=1;}
        void down(int i)
        {
            i<<=1;
            while(i<=n)
            {
                if(i^n&&a[i]^a[i+1]&&type^(a[i+1]<a[i])) i++;
                if(a[i]^a[i>>1]&&type^(a[i]<a[i>>1])) ly::swap(a[i],a[i>>1]),i<<=1;
                else break;
            }
        }
        void push(int x){a[++n]=x,up(n);}
        void pop(){ly::swap(a[1],a[n--]),down(1);}
        int top(){return a[1];}
        int size(){return n;}
        bool empty(){return !n;}
        void erase(int i){ly::swap(a[i],a[n--]),up(i),down(i);}
        void traversal(){heap t=*this;while(!t.empty())write(t.top(),0),t.pop();puts("");}
    private:
        bool type;//0:min_heap 1:max_heap
        int n;
        int a[maxn];
};

heap q1(0),q2(1);

signed main()
{
    read(n);
    for(int i=1;i<=n;++i) read(x),q1.push(x),q2.push(x);
    q1.traversal(),q2.traversal();
    return 0;
}

整合「封装+模板」

#include<iostream>
#include<cstdio>
#define maxn 1000010

using namespace std;
namespace IO
{
    template<typename type>
    inline void read(type &x)
    {
        x=0;bool flag(0);char ch=getchar();
        while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
        flag?x=-x:0;
    }

    template<typename type>
    inline void write(type x,bool flag=1)
    {
        x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
        do Stack[++top]=x%10,x/=10;while(x);
        while(top) putchar(Stack[top--]|48);
        flag?putchar('\n'):putchar(' ');
    }
}using namespace IO;
namespace ly
{
    auto swap=[](auto &x,auto &y){auto t=x;x=y,y=t;};
    template<typename type>
    class stack
    {
        private:
            int n;
            type a[maxn];
        public:
            stack(){n=0;}
            void push(type x){a[++n]=x;}
            void pop(){n--;}
            type top(){return a[n];}
            int size(){return n;}
            bool empty(){return !n;}
            void traversal(){for(int i=1;i<=n;++i)write(a[i],0);puts("");}
    };
    template<typename type>
    class queue
    {
        public:
            queue(){head=tail=n=0;}
            void push(int x){a[tail]=x,tail=(tail+1)%maxn,n++;}
            void pop(){head=(head+1)%maxn,n--;}
            int front(){return a[head];}
            int back(){return a[(tail-1+maxn)%maxn];}
            int size(){return n;}
            bool empty(){return !n;}
            void traversal(){for(int i=0;i<n;++i)write(a[(head+i)%n],0);puts("");}
        private:
            int head,tail,n;
            type a[maxn];
    };
    template<typename type>
    class deque
    {
        private:
            int head,tail,n;
        public:
            type a[maxn];
            deque(){head=tail=n=0;}
            void push_front(type x){head=(head?head-1:maxn-1),a[head]=x,n++;}
            void push_back(type x){a[tail]=x,tail=(++tail^maxn?tail:0),n++;}
            void pop_front(){head=(++head^maxn?head:0),n--;}
            void pop_back(){tail=(tail?tail-1:maxn-1),n--;}
            type front(){return a[head];}
            type back(){return a[tail?tail-1:maxn-1];}
            type& operator [](int i){return a[head+i-1-(head+i-1<maxn?0:maxn)];}
            int size(){return n;}
            bool empty(){return !n;}
            void traversal(){for(int i=0;i<n;++i)write(a[head+i-(head+i<maxn?0:maxn)],0);puts("");}
    };
    template<typename type>
    class node
    {
        public:
            type val;
            node *pre,*next;
            node(){pre=NULL,next=NULL;}
    };
    template<typename type>
    class list
    {
        public:
            node<type> *head,*tail;
            list(){head=new node<type>(),tail=new node<type>(),head->next=tail,tail->pre=head;}
            ~list(){while(head!=tail)head=head->next,delete head->pre;delete tail;}
            void insert(node<type> *p,type x)
            {
                node<type> *t=new node<type>();
                t->val=x,t->pre=p,t->next=p->next;
                p->next->pre=t,p->next=t;
            }
            void erase(node<type> *p)
            {
                p->next->pre=p->pre;
                p->pre->next=p->next;
                delete p;
            }
            bool empty(){return head->next==tail;}
            void traversal(){for(auto i=head->next;i!=tail;i=i->next)write(i->val,0);puts("");}
    };
    template<typename T>
    class heap
    {
        private:
            bool type;//0:min_heap 1:max_heap
            int n;
            T a[maxn];
        public:
            heap(bool flag=1){type=flag,n=0;}
            void up(int i){while(i^1&&a[i]^a[i>>1]&&type^(a[i]<a[i>>1]))ly::swap(a[i],a[i>>1]),i>>=1;}
            void down(int i)
            {
                i<<=1;
                while(i<=n)
                {
                    if(i^n&&a[i]^a[i+1]&&type^(a[i+1]<a[i])) i++;
                    if(a[i]^a[i>>1]&&type^(a[i]<a[i>>1])) ly::swap(a[i],a[i>>1]),i<<=1;
                    else break;
                }
            }
            void push(T x){a[++n]=x,up(n);}
            void pop(){ly::swap(a[1],a[n--]),down(1);}
            T top(){return a[1];}
            int size(){return n;}
            bool empty(){return !n;}
            void erase(int i){ly::swap(a[i],a[n--]),up(i),down(i);}
            void traversal(){heap t=*this;while(!t.empty())write(t.top(),0),t.pop();puts("");}
    };
}

int n,x;
ly::stack<int>s;
ly::queue<int>q;
ly::deque<int>dq;
ly::list<int>l;
ly::heap<int>h1(0),h2(1);

signed main()
{
    read(n);
    for(int i=1;i<=n;++i) read(x),s.push(x),q.push(x),dq.push_back(x),l.insert(l.tail->pre,x),h1.push(x),h2.push(x);
    s.traversal(),q.traversal(),dq.traversal(),l.traversal(),h1.traversal(),h2.traversal();
    return 0;
}
posted @ 2022-10-29 11:08  凌云_void  阅读(47)  评论(0编辑  收藏  举报