队列是只允许在一端删除,在另一端插入的线性表,允许删除的一端叫做队头(front),允许插入的一端叫做队尾(rear)。队列遵循先进先出的规则。实现队列可以使用顺序表,也可以使用链表。接下来是关于这两种实现方式的一些简单操作的函数。
顺序队列:
头文件:
Queue.h
#ifndef __QUEUE_H__
#define __QUEUE_H__
#define TRUE 1
#define FALSE 0
#define SIZE 10
typedef int QueueData;
typedef struct _queue
{
QueueData data[SIZE];
int front; // 指向队头的下标
int rear; // 指向队尾的下标
}Queue;
//置空队列
int InitStack (Queue *q);
//判断队空
int QueueEmpty (Queue *q);
//判断队满
int QueueFull (Queue *q);
//入队
int EnQueue (Queue *q,QueueData x);
//出队
int DeQueue (Queue *q,QueueData *x);
//取队头
int GetFront (Queue *q,QueueData *x);
#endif //__QUEUE_H__
源文件:
Queue.c
#include <stdio.h>
#include "Queue.h"
#include "error.h"
//置空队列
int InitQueue (Queue *q)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
q->front = 0;
q->rear = 0;
return TRUE;
}
//判断队空
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;
}
//入队
int EnQueue (Queue *q,QueueData x)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
if (QueueFull (q))
{
errno = FULL_QUEUE;
return FALSE;
}
q->rear = (q->rear+1)%SIZE;
q->data[q->rear] = x;
return TRUE;
}
//出队
int DeQueue (Queue *q,QueueData *x)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
if (QueueEmpty (q))
{
errno = EMPTY_QUEUE;
return FALSE;
}
q->front = (q->front+1)%SIZE;
*x = q->data[q->front];
return TRUE;
}
//取队头
int GetFront (Queue *q,QueueData *x)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
if (QueueEmpty (q))
{
errno = EMPTY_QUEUE;
return FALSE;
}
int index = (q->front+1)%SIZE;
*x = q->data[index];
return TRUE;
}
链式队列:
头文件:
LinkQueue.h
#ifndef __LINKQUEUE_H__
#define __LINKQUEUE_H__
#define TRUE 1
#define FALSE 0
typedef int QueueData;
typedef struct _node
{
QueueData data;
struct _node *next;
}Node;
typedef struct _queue
{
Node *front;
Node *rear;
}Queue;
//创建链式队列
Queue * Create_Stack ();
//判断队空
int QueueEmpty (Queue *q);
// 进队
int EnQueue (Queue *q, QueueData x);
// 出队
int DeQueue (Queue *q, QueueData *x);
// 取队头
int GetFront (Queue *q, QueueData *x);
//销毁队列
int Destroy_Queue (Queue *q);
#endif //__LINKQUEUE_H__
源文件:
LinkQueue.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"
#include "error.h"
//创建链式队列
Queue * Create_Stack ()
{
Queue *q = (Queue *)malloc(sizeof(Queue)/sizeof(char));
if (q == NULL)
{
errno = MALLOC_ERROR;
return FALSE;
}
q->front = NULL;
q->rear = NULL;
return q;
}
//判断队空
int QueueEmpty (Queue *q)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
return q->front == NULL;
}
// 进队
int EnQueue (Queue *q, QueueData x)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
Node * node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
errno = ERROR;
return FALSE;
}
node->data = x;
node->next = NULL;
if (q->front == NULL)
{
q->front = node;
q->rear = node;
}
else
{
q->rear->next = node;
q->rear = node;
}
return TRUE;
}
// 出队
int DeQueue (Queue *q, QueueData *x)
{
if (q == NULL || x == NULL)
{
errno = ERROR;
return FALSE;
}
if (QueueEmpty (q))
{
errno = EMPTY_QUEUE;
return FALSE;
}
Node *p = q->front;
*x = p->data;
q->front = p->next;
free (p);
if (q->front = NULL)
{
q->rear = NULL;
}
return TRUE;
}
// 取队头
int GetFront (Queue *q, QueueData *x)
{
if (q == NULL || x == NULL)
{
errno = ERROR;
return FALSE;
}
if (QueueEmpty (q))
{
errno = EMPTY_QUEUE;
return FALSE;
}
*x = q->front->data;
return TRUE;
}
//销毁队列
int Destroy_Queue (Queue *q)
{
if (q == NULL)
{
errno = ERROR;
return FALSE;
}
int x;
while (QueueEmpty(q) != TRUE)
{
DeQueue(q, &x);
}
free(q);
return TRUE;
}
关于队列的更多的功能,可以大家一起去实现。