一、Sysutil.h//系统头文件
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdbool.h>
#include<windows.h>
#include<vld.h> //内存泄漏工具的头文件
二、单链表实现队列函数
#include"Sysutil.h"
#define QueueElemType int
链队列
typedef struct LinkQueueNode
{
QueueElemType data;
struct LinkQueueNode *next;
}LinkQueueNode;
typedef struct LinkQueue
{
LinkQueueNode *head;
LinkQueueNode *tail; //指向队尾节点
}LinkQueue;
void LinkQueueInit(LinkQueue *pq);
void LinkQueueDestroy(LinkQueue *pq);
void LinkQueueEn(LinkQueue *pq, QueueElemType x);
void LinkQueueDe(LinkQueue *pq);
void LinkQueueShow(LinkQueue *pq);
QueueElemType LinkQueueBack(LinkQueue *pq);
QueueElemType LinkQueuefront(LinkQueue *pq);
void LinkQueueInit(LinkQueue *pq){
assert(pq);
pq->head = pq->tail = NULL;
}
void LinkQueueEn(LinkQueue *pq, QueueElemType x){
assert(pq);
LinkQueueNode *p = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
assert(p);
p->data = x;
p->next = NULL;
if (pq->head == NULL){
pq->head = pq->tail = p;
}
else{
pq->tail->next = p;
pq->tail = p;
}
}
void LinkQueueDe(LinkQueue *pq){
assert(pq);
if (pq->head != NULL){
LinkQueueNode *p = pq->head;
pq->head = p->next;
if (pq->head == NULL){
pq->tail = NULL;
}
free(p);
}
}
QueueElemType LinkQueueBack(LinkQueue *pq){
assert(pq&&pq->head);
return pq->tail->data;
}
QueueElemType LinkQueueFront(LinkQueue *pq){
assert(pq&&pq->head);
return pq->head->data;
}
void LinkQueueShow(LinkQueue *pq){
assert(pq);
LinkQueueNode *p = pq->head;
while (p != NULL){
printf("%d<--", p->data);
p = p->next;
}
printf("Nilr..\n");
}
void LinkQueueDestroy(LinkQueue *pq){
assert(pq);
while (pq->head != NULL){
LinkQueueNode *p = pq->head;
pq->head = p->next;
free(p);
}
pq->head = pq->tail = NULL;
}
三、单链表实现队列主函数
#include“Queue.h”
void main()
{
LinkQueue Q;
LinkQueueInit(&Q);
LinkQueueEn(&Q, 1);
LinkQueueEn(&Q, 2);
LinkQueueEn(&Q, 3);
LinkQueueEn(&Q, 4);
LinkQueueEn(&Q, 5);
LinkQueueEn(&Q, 6);
LinkQueueShow(&Q);
printf("==============\n");
QueueElemType val = LinkQueueFront(&Q);
LinkQueueDe(&Q);
printf("%d 出队.\n", val);
LinkQueueDestroy(&Q);
system("pause");
return 0;
}
四、顺序表实现队列(循环队列)函数
//顺序队列-->循环队列
#define QUEUE_DEFAULT_SIZE 8
typedef struct SeqQueue
{
QueueElemType *base;
size_t capacity;
int front;
int rear;
}SeqQueue;
void SeqQueueInit(SeqQueue *pq, int sz);
void SeqQueueDestroy(SeqQueue *pq);
void SeqQueueEn(SeqQueue *pq, QueueElemType x);
void SeqQueueDe(SeqQueue *pq);
QueueElemType SeqQueueBack(SeqQueue *pq);
QueueElemType SeqQueueFront(SeqQueue *pq);
void SeqQueueShow(SeqQueue *pq);
void SeqQueueInit(SeqQueue *pq, int sz)
{
assert(pq);
pq->capacity = sz > QUEUE_DEFAULT_SIZE ? sz : QUEUE_DEFAULT_SIZE;
pq->base = (QueueElemType*)malloc(sizeof(QueueElemType) * (pq->capacity + 1));
assert(pq->base != NULL);
pq->front = pq->rear = 0;
}
void SeqQueueDestroy(SeqQueue *pq)
{
assert(pq);
free(pq->base);
pq->base = NULL;
pq->capacity = pq->front = pq->rear = 0;
}
void SeqQueueEn(SeqQueue *pq, QueueElemType x)
{
assert(pq);
if ((pq->rear + 1) % (pq->capacity + 1) == pq->front)
{
printf("队列已满, %d 不能入队.\n", x);
return;
}
pq->base[pq->rear] = x;
pq->rear = (pq->rear + 1) % (pq->capacity + 1);
}
void SeqQueueDe(SeqQueue *pq)
{
assert(pq);
if (pq->front == pq->rear)
{
printf("队列已空,不能出队.\n");
return;
}
pq->front = (pq->front + 1) % (pq->capacity + 1);
}
QueueElemType SeqQueueBack(SeqQueue *pq)
{
assert(pq && (pq->front != pq->rear));
return pq->base[(pq->rear - 1 + pq->capacity + 1) % (pq->capacity + 1)];
}
QueueElemType SeqQueueFront(SeqQueue *pq)
{
assert(pq && (pq->front != pq->rear));
return pq->base[pq->front];
}
void SeqQueueShow(SeqQueue *pq)
{
assert(pq);
for (int i = pq->front; i != pq->rear; )
{
printf("%d<--", pq->base[i]);
i = (i + 1) % (pq->capacity + 1);
}
printf("Nil.\n");
}
五、顺序表实现队列(循环队列)主函数
#include"Queue.h"
void main()
{
SeqQueue Q;
SeqQueueInit(&Q, 0);
SeqQueueEn(&Q, 1);
SeqQueueEn(&Q, 3);
SeqQueueEn(&Q, 5);
SeqQueueEn(&Q, 4);
SeqQueueEn(&Q, 2);
SeqQueueEn(&Q, 6);
SeqQueueEn(&Q, 7);
SeqQueueEn(&Q, 8);
SeqQueueEn(&Q, 20);
SeqQueueDe(&Q);
SeqQueueEn(&Q, 20);
SeqQueueDe(&Q);
SeqQueueEn(&Q, 30);
SeqQueueShow(&Q);
system("pause");
return 0;
}