zoukankan      html  css  js  c++  java
  • 数据结构-队列(Queue)

    #include <stdio.h>
    #include <stdlib.h>
    #define LIST_INIT_SIZE 10
    #define LISTINCREMENT 100
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10
    #define TRUE 1
    #define FALSE 0
    #define true 1
    #define false 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    #define OPSETSIZE 7
    #define MAXQSIZE 100
    typedef int Status;
    typedef int ElemType;
    typedef int QElemType;
    typedef struct QNode
    {
       QElemType data;
       struct QNode *next;
    }QNode, *QueuePtr;
    
    typedef struct
    {
        QueuePtr front;
        QueuePtr rear;
    }LinkQueue;
    
    Status InitQueue(LinkQueue *Q);
    Status DestoryQueue(LinkQueue *Q);
    Status Push(LinkQueue *Q, QElemType e);
    Status Pop(LinkQueue *Q, QElemType *e);
    
    int main()
    {
        LinkQueue Q;
        QElemType e;
        InitQueue(&Q);
        Push(&Q, 1);
        Push(&Q, 2);
        Push(&Q, 3);
        Push(&Q, 4);
        while(Pop(&Q, &e))
        {
            printf("%d
    ", e);
        }
        DestoryQueue(&Q);
        return 0;
    }
    
    
    Status InitQueue(LinkQueue *Q)
    {
        Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode));
        if(!Q->front)
            exit(OVERFLOW);
        Q->front->next = NULL;
        return OK;
    }
    
    Status DestoryQueue(LinkQueue *Q)
    {
        while(Q->front)
        {
            Q->rear = Q->front->next;
            free(Q->front);
            Q->front = Q->rear;
        }
        return OK;
    }
    
    Status Push(LinkQueue *Q, QElemType e)
    {
        QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
        if(!p)
            exit(OVERFLOW);
        p->data = e;
        Q->rear->next = p;
        p->next = NULL;
        Q->rear = p;
        return OK;
    }
    
    Status Pop(LinkQueue *Q, QElemType *e)
    {
        if(Q->front == Q->rear)
            return ERROR;
        QueuePtr p = Q->front->next;
        *e = p->data;
        Q->front->next = p->next;
        if(Q->rear == p)
            Q->rear = Q->front;
        free(p);
        return OK;
    }
    
    
    
    

  • 相关阅读:
    Vue项目入门实例
    批量生成删除表数据的SQL语句
    收集的一个可多选日期的日期插件,带日历、农历
    .net core EF,多个dbcontext时,迁移数据方法
    【NET】雪花算法
    URL地址中使用中文作为的参数【转】
    C# 继承 base
    SQL Like
    SecureCRT 8.5 配置自动记录日志
    CentOS 7下Samba服务器的安装与配置
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451404.html
Copyright © 2011-2022 走看看