Queue.h
Queue.c
Queue.h
Queue.c
1、循环队列
Queue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_SIZE 8
typedef int DataType;
typedef struct queue
{
DataType _array[MAX_SIZE];
DataType _front;
DataType _back;
int count;
}Queue ,*PQueue;
void QueueInit(PQueue s);
void QueuePush(PQueue s, DataType data);
void QueuePop(PQueue s);
int QueueSize(PQueue s);
int QueueEmpty(PQueue s);
DataType Queuefront(PQueue s);
DataType Queueback(PQueue s);
void TestQueue();
Queue.c
#include "Queue.h"
void QueueInit(PQueue s)
{
assert(s);
s->_front = s->_back = 0;
s->count = 0;
}
void QueuePush(PQueue s, DataType data)
{
assert(s);
if (MAX_SIZE == s->count)
{
printf("队列已满!\n");
return;
}
s->_array[s->_back++] = data;
if (s->_back == MAX_SIZE)
s->_back = 0;
s->count++;
}
void QueuePop(PQueue s)
{
assert(s);
if (0 == s->count)
return;
s->_front++;
if (s->_front == MAX_SIZE)
s->_front = 0;
s->count--;
}
int QueueSize(PQueue s)
{
assert(s);
return s->count;
}
int QueueEmpty(PQueue s)
{
if (0 == s->count)
return 1;
return 0;
}
DataType Queuefront(PQueue s)
{
assert(s);
if (0 == s->count)
exit(EXIT_FAILURE);
return s->_array[s->_front];
}
DataType Queueback(PQueue s)
{
assert(s);
if (0 == s->count)
exit(EXIT_FAILURE);
if (0 != s->_back)
{
return s->_array[s->_back - 1];
}
return s->_array[MAX_SIZE - 1];
}
void TestQueue()
{
Queue s;
QueueInit(&s);
QueuePush(&s, 1);
QueuePush(&s, 2);
QueuePush(&s, 3);
QueuePush(&s, 4);
QueuePush(&s, 5);
QueuePush(&s, 6);
QueuePush(&s, 7);
QueuePush(&s, 8);
QueuePop(&s);
int size = QueueSize(&s);
int ret = QueueEmpty(&s);
DataType a =Queuefront(&s);
DataType b = Queueback(&s);
}
2、动态队列
Queue.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Node
{
struct Node *_PNext;
DataType _data;
}Node, *PNode;
typedef struct Queue
{
PNode front;
PNode rear;
}Queue, *PQueue;
void QueueInit(PQueue q);
void QueuePush(PQueue q, DataType data);
void QueuePop(PQueue q);
PNode BuyNode(DataType data);
int QueueEmpty(PQueue q);
DataType QueueFrontData(PQueue q);
DataType QueueRearData(PQueue q);
int QueueSize(PQueue q);
void QueueDestory(PQueue q);
Queue.c
#include "Queue.h"
void QueueInit(PQueue q)
{
assert(q);
q->front = NULL;
q->rear = NULL;
}
void QueuePush(PQueue q, DataType data)
{
assert(q);
if (NULL == q->front)
{
q->front = q->rear = BuyNode(data);
}
else
{
q->rear->_PNext = BuyNode(data);
q->rear = q->rear->_PNext;
}
}
void QueuePop(PQueue q)
{
PNode pDel = NULL;
assert(q);
if (NULL == q->front)
{
return;
}
else
{
pDel = q->front;
q->front = q->front->_PNext;
free(pDel);
pDel = NULL;
}
}
PNode BuyNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
{
exit(EXIT_FAILURE);
}
pNewNode->_data = data;
pNewNode->_PNext = NULL;
return pNewNode;
}
int QueueEmpty(PQueue q)
{
assert(q);
if (NULL == q->front)
return 1;
return 0;
}
DataType QueueFrontData(PQueue q)
{
assert(q);
if (NULL == q->front)
exit(EXIT_FAILURE);
return q->front->_data;
}
DataType QueueRearData(PQueue q)
{
assert(q);
if (NULL == q->front)
exit(EXIT_FAILURE);
return q->rear->_data;
}
int QueueSize(PQueue q)
{
int count = 0;
PNode pCur = NULL;
assert(q);
if (NULL == q->front)
{
return 0;
}
else
{
pCur = q->front;
while (pCur)
{
count++;
pCur = pCur->_PNext;
}
}
return count;
}
void QueueDestory(PQueue q)
{
PNode pDel = NULL;
PNode pCur = NULL;
assert(q);
if (NULL == q->front)
{
return;
}
else
{
pCur = q->front;
q->front = NULL;
while (pCur)
{
pDel = pCur;
pCur = pCur->_PNext;
free(pDel);
}
}
}