zoukankan      html  css  js  c++  java
  • 队列

    //   队列的各种实现

    #include <cstdio>
    #include <cstdlib>
    //#define _OJ_


    typedef struct Lnode
    {
        int data;
        struct Lnode *next;
    } qNode, *Queueptr;

    typedef struct
    {
        Queueptr front;
        Queueptr rear;
    }queue, *Linkqueue;

    void
    creat_queue(Linkqueue q)
    {
        q->front = q->rear = (Queueptr) malloc (sizeof(qNode));
        q->front->next = NULL;
    }


    int
    isempty(Linkqueue q)
    {
        if(q->front == q->rear)
            return 1;
        else
            return 0;
    }


    void
    Enqueue(Linkqueue q)
    {
        printf("%p ", q->rear);
        Queueptr p1;
        p1 = (Queueptr) malloc (sizeof(qNode));
        scanf("%d", &p1->data);
        printf("%d ", p1->data);
        p1->next = NULL;
        q->rear->next = p1;
        q->rear = p1;
            printf("%p ", q->rear);

    }


    int
    Dequeue(Linkqueue q)
    {
        int e;
        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 e;
    }




    int main(int argc, char const *argv[]) {
    #ifndef _OJ_  //ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif

        Linkqueue q;
        creat_queue(q);
        printf("%d ",isempty(q));
        int i, n, x;
        scanf("%d", &n);

        while (n--)
        Enqueue(q);

        while (q->rear != q->front)
        printf("%d ", Dequeue(q));







        return 0;
    }

  • 相关阅读:
    计算器程序
    输入三个整数,输出最大数和最小数
    输入三个数a,b,c,要示按由小到大的顺序输出
    最短路
    luogu P3953 逛公园
    二分图匹配
    luogu P3231 消毒
    [bzoj2120] [洛谷P1903] 数颜色
    [bzoj2038] [洛谷P1494] [2009国家集训队] 小Z的袜子(hose)
    [洛谷P4012] [网络流24题] 深海机器人问题
  • 原文地址:https://www.cnblogs.com/airfand/p/4921919.html
Copyright © 2011-2022 走看看