队列

/*
* @Author: qin_peng
* @Date:   2018-08-25 09:10:10
* @Last Modified by:   qin_peng
* @Last Modified time: 2018-08-29 11:08:38
*/
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct que
{
    int *head,*tail;
};
bool empty(que *q){return q->head==q->tail;}
int size(que *q){return q->tail-q->head;}
void build(que *q,int n)
{
    q->head=(int *)malloc(sizeof(int));
    q->tail=q->head;
}
void push(que *q,int val){*q->tail++=val;}
int pop(que *q)
{
    if(empty(q))return -1;
    return *q->head++;
}
int front(que *q)
{
    if(empty(q))return -1;
    return *q->head;
}
int main()
{
    que q;
    build(&q,5);
    for(int i=0;i<5;i++)
    {
        int x;cin>>x;
        push(&q,x);
    }
    while(!empty(&q))cout< 
  

队列2

#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Status int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define INFEASIBLE  -1
#define OVERFLOW  -2
typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;
struct QUEUE
{
    Status InitQueue(LinkQueue &Q)
    {
        Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
        if(!Q.front)exit(OVERFLOW);
        return OK;
    }
    Status DestroyQueue(LinkQueue &Q)
    {
        while(Q.front)
        {
            Q.rear=Q.front->next;
            free(Q.front);
            Q.front=Q.rear;
        }return OK;
    }
    Status EnQueue(LinkQueue &Q,int e)
    {
        QueuePtr p;
        p=(QueuePtr)malloc(sizeof(QNode));
        if(!p)exit(OVERFLOW);
        p->data=e;p->next=NULL;
        Q.rear->next=p;
        Q.rear=p;return OK;
    }
    Status DeQueue(LinkQueue &Q,int &e)
    {
        if(Q.front==Q.rear)return ERROR;
        QueuePtr p;
        p=Q.front->next;
        e=p->data;
        Q.front->next=p->next;
        if(Q.rear==p)Q.rear=Q.front;
        free(p);
        return OK;
    }
    Status Empty(LinkQueue Q)
    {
        return Q.rear==Q.front;
    }
};
int main()
{
    QUEUE qu;
    LinkQueue Q;
    qu.InitQueue(Q);
    for(int i=0,e;i<5;i++)
    {
        cin>>e;
        qu.EnQueue(Q,e);
    }
    while(!qu.Empty(Q))
    {
        int e;qu.DeQueue(Q,e);
        cout< 
  

循环队列

#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Status int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define INFEASIBLE  -1
#define OVERFLOW  -2
#define MAXSIZE 100
typedef struct
{
    int *base;
    int front ,rear;
}SqQueue;
struct QUEUE
{
    Status InitQueue(SqQueue &Q)
    {
        Q.base=(int *)malloc(MAXSIZE*sizeof(int));
        if(!Q.base)exit(OVERFLOW);
        Q.front=Q.rear=0;
        return OK;
    }
    Status QueueLength(SqQueue &Q)
    {
        return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
    }
    Status EnQueue(SqQueue &Q,int r)
    {
        if((Q.rear+1)%MAXSIZE==Q.front)return ERROR;
        Q.base[Q.rear]=r;
        Q.rear=(Q.rear+1)%MAXSIZE;
        return OK;
    }
    Status DeQueue(SqQueue &Q,int &e)
    {
        if(Q.front==Q.rear)return ERROR;
        e=Q.base[Q.front];
        Q.front=(Q.front+1)%MAXSIZE;
        return OK;
    }
    Status QueueTravel(SqQueue Q)
    {
        for(int i=0;i>e;
        qu.EnQueue(Q,e);
   } 
   qu.QueueTravel(Q);
}

单调队列

#include
#include
#include
#include
#include
using namespace std;
#define N 1000005
int n,k;
int minn[N],maxx[N];
int a[N];
struct qin
{
    int num,index;
};
qin Q[N],P[N];
void getmax()
{
    int head=1,tail=0;
    for(int i=0;i=k-1)
        {
            while(Q[head].index<=i-k)head++;
            maxx[i-k+1]=Q[head].num;
        }
    }
}
void getmin()
{
    int head=1,tail=0;
    for(int i=0;ia[i])tail--;
        tail++;P[tail].num=a[i];P[tail].index=i;
        if(i>=k-1)
        {
            while(P[head].index<=i-k)head++;
            minn[i-k+1]=P[head].num;
        }
    }
}
int main()
{
    while(cin>>n>>k)
    {
        for(int i=0;i
#include 
#include 
#include 
using namespace std;
typedef pair P;
#define maxn 1000000 + 10
deque

 Q1; deque

 Q2; int n, k; int Min[maxn], Max[maxn]; int main() {     while(~scanf("%d%d", &n, &k))     {         while(!Q1.empty()) Q1.pop_back();         while(!Q2.empty()) Q2.pop_back();         int x;         for(int i=1; i<=n; i++)         {             scanf("%d", &x);             while(!Q1.empty() && Q1.back().first >= x) Q1.pop_back();             Q1.push_back(P(x, i));             if(i >= k)                 {                     while(!Q1.empty() && Q1.front().second <= i-k) Q1.pop_front();                     Min[i] = Q1.front().first;                 }             while(!Q2.empty() && Q2.back().first <= x) Q2.pop_back();             Q2.push_back(P(x, i));             if(i >= k)                 {                     while(!Q2.empty() && Q2.front().second <= i-k) Q2.pop_front();                     Max[i] = Q2.front().first;                 }         }         for(int i=k; i<=n; i++)             i == n ? printf("%d\n", Min[i]) : printf("%d ", Min[i]);         for(int i=k; i<=n; i++)             i == n ? printf("%d\n", Max[i]) : printf("%d ", Max[i]);     }     return 0; } *////