数据结构:数组队列

来源:互联网 发布:面向对象编程的优势 编辑:程序博客网 时间:2024/06/13 06:20

头文件

#ifndef __SQQUENUE_H__#define __SQQUENUE_H__#define FALSE 0#define TRUE 1#define SIZE 10typedef int Queue_data ;typedef struct _queue{Queue_data data[SIZE];int front;     //定义头int rear;      //定义尾}QUEUE;//清空队列(初始化)int Initqueue(QUEUE * q);//判断队列空否int QueueEmpty(QUEUE * q);//进队int PushQueue(QUEUE *q,Queue_data x);//出队int PopQueue(QUEUE *q,Queue_data *x);//取队头int GetQueue(QUEUE *q,Queue_data *x);#endif

实现函数
#include<stdio.h>#include<stdlib.h>#include"SqQuenue.h"#include"error.h"//清空队列int Initqueue(QUEUE* q){if (q == NULL){errno = ERROR;return FALSE;}q->front = 0;q->rear = 0;}//判断队列空否int QueueEmpty(QUEUE * q){if (q == NULL){errno = ERROR;return FALSE;}return q->front == q->rear ;}//判断队列满否int QueueFull(QUEUE *q){if (q == NULL){errno = ERROR;return FALSE;}return q->front == (q->rear+1)%SIZE;}//进队(从rear进队列)int PushQueue(QUEUE *q,Queue_data x){if (q == NULL){errno = ERROR;return FALSE;}if(QueueFull(q)){errno = QUEUE_FULL;return FALSE;}q->rear = (q->rear+1) % SIZE;q->data[q->rear] = x;return TRUE;}//出队(从front出队列)int PopQueue(QUEUE *q,Queue_data *x){if (q == NULL){errno = ERROR;return FALSE;}if(QueueEmpty(q)){errno = QUEUE_EMPTY;return FALSE;}q->front = (q->front +1) %SIZE;*x = q->data[q->front];return TRUE;}//取队头int GetQueue(QUEUE *q,Queue_data *x){if (q == NULL){errno = ERROR;return FALSE;}if(QueueEmpty(q) != TRUE){errno = QUEUE_EMPTY;return FALSE;}int index = (q->front + 1) % SIZE;*x = q->data[index];return TRUE;}


原创粉丝点击