数据结构--循环队列
2017-05-03 16:33
267 查看
head为空值
#define QUEUEMAX 15
#include<stdio.h>
typedef struct
{
DATA data[QUEUEMAX];
int head;
int tail;
}CycQueue;
CycQueue *CycQueueInit()
{
CycQueue *q;
if(q = (CycQueue *)malloc(sizeof(CycQueue)))
{
q->head = 0;
q->tail = 0;
return q;
}
else
{
return NULL;
}
}
void CycQueueFree(CycQueue *q)
{
if(q != NULL)
free(q);
}
int CycQueueIsEmpty(CycQueue *q)
{
return (q->head == q->tail);
}
int CycQueueIsFull(CycQueue *q)
{
return ((q->tail+1)%QUEUEMAX == q->head);
}
int CycQueueIn(CycQueue *q,DATA data)
{
if((q->tail+1)%QUEUEMAX == q->head)
{
printf("queue is full\n");
return 0;
}
else
{
q->tail = (q->tail+1)%QUEUEMAX;
q->data[q->tail] = data;
return 1;
}
}
DATA *CycQueueOut(CycQueue *q)
{
if(q->head == q->tail)
{
printf("queue is empty\n");
return NULL;
}
else
{
q->head = (q->head+1)%QUEUEMAX;
return &(q->data[q->head]);
}
}
int CycQueueLen(CycQueue *q)
{
int n;
n = q->tail - q->head;
if(n < 0)
n = QUEUEMAX + n;
return n;
}
DATA *CycQueuePeek(CycQueue *q)
{
if(q->head == q->tail)
{
printf("queue is empty\n");
return NULL;
}
else
{
return &(q->data[(q->head+1)%QUEUEMAX]);
}
}
#define QUEUEMAX 15
#include<stdio.h>
typedef struct
{
DATA data[QUEUEMAX];
int head;
int tail;
}CycQueue;
CycQueue *CycQueueInit()
{
CycQueue *q;
if(q = (CycQueue *)malloc(sizeof(CycQueue)))
{
q->head = 0;
q->tail = 0;
return q;
}
else
{
return NULL;
}
}
void CycQueueFree(CycQueue *q)
{
if(q != NULL)
free(q);
}
int CycQueueIsEmpty(CycQueue *q)
{
return (q->head == q->tail);
}
int CycQueueIsFull(CycQueue *q)
{
return ((q->tail+1)%QUEUEMAX == q->head);
}
int CycQueueIn(CycQueue *q,DATA data)
{
if((q->tail+1)%QUEUEMAX == q->head)
{
printf("queue is full\n");
return 0;
}
else
{
q->tail = (q->tail+1)%QUEUEMAX;
q->data[q->tail] = data;
return 1;
}
}
DATA *CycQueueOut(CycQueue *q)
{
if(q->head == q->tail)
{
printf("queue is empty\n");
return NULL;
}
else
{
q->head = (q->head+1)%QUEUEMAX;
return &(q->data[q->head]);
}
}
int CycQueueLen(CycQueue *q)
{
int n;
n = q->tail - q->head;
if(n < 0)
n = QUEUEMAX + n;
return n;
}
DATA *CycQueuePeek(CycQueue *q)
{
if(q->head == q->tail)
{
printf("queue is empty\n");
return NULL;
}
else
{
return &(q->data[(q->head+1)%QUEUEMAX]);
}
}
相关文章推荐
- 数据结构 学习笔记之:线性表——顺序表、静态链表、动态链表(单链表、双链表、单循环链表、双循环链表,链式栈、链式队列)——基础概念大扫盲!
- 数据结构:循环队列--Java实现
- 数据结构 一 (栈 & 循环队列 & 链式队列)
- 数据结构:双向链表实现队列与循环链表
- 数据结构:循环队列(C语言实现)
- [数据结构]队列实现、循环队列的不同实现
- 数据结构 -- 队列 & 循环队列 -- 数组实现
- 基本数据结构,循环队列的顺序实现。
- 数据结构-【队列】链式队列和循环队列
- 数据结构-- 队列 循环与顺序
- Java数据结构之循环队列简单定义与用法示例
- 【数据结构】静态循环队列的操作
- 数据结构之循环队列
- 算法与数据结构【四】——C语言实现循环队列
- 用c++编写的数据结构循环队列
- 数据结构学习----顺序循环队列(Java实现)
- 第三章 队列【数据结构】【链队列】【循环队列】
- 循环队列 C语言数据结构
- 数据结构,循环队列各功能的实现
- 循环队列的表示和实现(数据结构)