• 循环队列基本操作


    /*
     * 循环队列基本操作。
     * 少用一个元素空间,约定以“队列头指针在队列尾指针的下一个位置”作为队列满的标志。
     * “队列头指针等于队列尾指针”作为队列空的标志。
     */
    #include <stdio.h>
    #include <stdbool.h>
    #include <malloc.h>
    
    #define MAXQSIZE 100
    typedef char ElemType;
    typedef struct
    {
    	ElemType *base;
    	int front;
    	int rear;
    } Queue;
    
    bool InitQueue(Queue *queue);
    int QueueLength(Queue queue);
    bool EnQueue(Queue *queue, ElemType e);
    bool DeQueue(Queue *queue, ElemType *e);
    
    int main()
    {
    	Queue queue;
    	ElemType elem[26],temp;
    	int i,length;
    
    	InitQueue(&queue);
    	for (i = 0; i < 26; i++)
    	{
    		elem[i] = 'A'+i;
    		EnQueue(&queue, elem[i]);
    	}
    	length = QueueLength(queue);
    	printf("length:%d
    ", length);
    	for (i = 0; i < length; i++)
    	{
    		DeQueue(&queue, &temp);
    		putchar(temp);
    	}
    	return 0;
    }
    
    //构造空队列。
    bool InitQueue(Queue *queue)
    {
    	queue->base = (ElemType*)malloc(sizeof(ElemType)*MAXQSIZE);
    	if (!queue->base) return false;
    	queue->front = queue->rear = 0;
    	return true;
    }
    
    //求队列长度。
    int QueueLength(Queue queue)
    {
    	return (queue.rear - queue.front + MAXQSIZE) % MAXQSIZE;
    }
    
    //插入元素e为队列的队尾元素。
    bool EnQueue(Queue *queue, ElemType e)
    {
    	if ((queue->rear + 1) % MAXQSIZE == queue->front)	//队列满
    		return false;
    	queue->base[queue->rear] = e;
    	queue->rear = (queue->rear + 1) % MAXQSIZE;
    	return true;
    }
    
    //删除队列的队头元素,用e返回其值。
    bool DeQueue(Queue *queue, ElemType *e)
    {
    	if (queue->front == queue->rear)	//队列空
    		return false;
    	*e = queue->base[queue->front];
    	queue->front = (queue->front + 1) % MAXQSIZE;
    	return true;
    }
  • 相关阅读:
    Python算术运算符
    Python数据类型转换
    Linux下Tomcat启动设置debug模式启动
    FastJson之JsonObject, JsonString, JavaBean,List快速转换
    Linux 之 ./configure --prefix 命令
    JS中Ajax的同步和异步
    MySql 中 case when then else end 的用法
    Linux中CentOS6.5 64位 系统下安装docker步骤
    Linux常用命令 查找文件
    微信小程序中事件
  • 原文地址:https://www.cnblogs.com/Camilo/p/3896768.html
Copyright © 2020-2023  润新知