• QUEUE——队列(procedure)


    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"

    int main()
    {
     int i;
     Type x;
     Type arr[] = {3,1,2,5,7,9};
     QUEUE *q = NULL;

     q = CreateQueue(10);
     if(NULL == q)
      return -1;
     
     for(i = 0; i < sizeof(arr)/sizeof(*arr); i++)
     {
      EnQueue(q, arr + i);
     }
     FrontQueue(q, &x);
     printf("x = %d ", x);

     DisptoryQueue(q);
     return 0;
    }
    ---------------------------------------------------------------

    #ifndef _QUEUE_H__
    #define _QUEUE_H__

    struct node;
    typedef int Type;
    typedef struct node QUEUE;

    QUEUE *CreateQueue(int);
    void QueueMakeEmpty(QUEUE *);
    int QueueIsEmpty(QUEUE *);
    int QueueIsFull(QUEUE *);
    int EnQueue(QUEUE *, const Type *);
    int DeQueue(QUEUE *);
    int FrontQueue(QUEUE *, Type *);
    int FrontAndDeQueue(QUEUE *, Type *);
    void DisptoryQueue(QUEUE *);

    struct node{
     Type *data;
     int capacity;
     int front;
     int rear;
     int size;
    };

    #endif
    -------------------------------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"

    QUEUE *CreateQueue(int size)
    {
     QUEUE *q = malloc(sizeof(*q));
     if(NULL == q)
      return NULL;
     q->data = malloc(sizeof(Type)*size); //队列的长度,队列的成员个数
     if(NULL == q->data)
     {
      free(q);
      return NULL;
     }
     q->capacity = size; //队列容量
     QueueMakeEmpty(q);
     return q;
    }
    void QueueMakeEmpty(QUEUE *q)
    {
     q->size = 0;
     q->front = 1;
     q->rear = 0;
    }
    int QueueIsEmpty(QUEUE *q)
    {
     return q->size == 0;
    }
    int QueueIsFull(QUEUE *q)
    {
     return q->size == q->capacity;
    }
    static int repeat(QUEUE *q, int rear) //队列队尾入队,
    {
     if(++rear == q->capacity)
      rear = 0;
     return rear;
    }
    int EnQueue(QUEUE *q, const Type *x)
    {
     if(QueueIsFull(q))
      return -1;
     q->rear = repeat(q, q->rear); //每次入队成功后,队尾rear置0.
     q->data[q->rear] = *x;
     q->size++;
     return 0;
    }
    int DeQueue(QUEUE *q) //出队
    {
     if(QueueIsEmpty(q))
      return -1;
     q->front = repeat(q, q->front);
     q->size--;
     return 0;
    }
    int FrontQueue(QUEUE *q, Type *x) //查看队首
    {
     if(QueueIsEmpty(q))
      return -1;
     *x = q->data[q->front];
     return 0;
    }
    int FrontAndDeQueue(QUEUE *q, Type *x) //查看队首并出队
    {
     if(FrontQueue(q, x) == 0)
      return DeQueue(q);
     return -1;
    }
    void DisptroyQueue(QUEUE *q)
    {
     free(q->data);
     free(q);
    }

  • 相关阅读:
    确保消息产生前数据库操作已提交
    信息披露和公司简介总结
    1、清空所有,给当前添加/2、清空上一个,给当前添加。
    不能作为判断条件的:
    excel表格 函数功能
    一种ui app写法
    正则中使用变量及数组去重的方法
    鼠标锁定(消失),进入无限滚动状态
    transform 的旋转 ,3d效果,要添加3d效果的父级加上景深perspective。 3d效果的容器加上 transform-style:preserve-3d。
    rem布局,在用户调整手机字体大小/用户调整浏览器字体大小后,布局错乱问题
  • 原文地址:https://www.cnblogs.com/riskyer/p/3356280.html
Copyright © 2020-2023  润新知