链(单向链)式队列:创建&初始化、入队列、出队列、取队列头数据、计算队列有效长度、清空队列、销毁队列
/*
链式队列
初始化
入队列
出队列
取队列头数据
计算队列有效长度
清空队列
销毁队列
原创代码,用于学习。引用请注明,有问题协商共学习
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ElemType int
typedef struct node
{
ElemType data;
struct node *pNext;
}Node;
typedef struct __listQueueInfo{
Node *front;
Node *rear;
}listQueueInfo;
/* 初始化 */
int initQueue(listQueueInfo **ppQ)
{
if (NULL == ppQ)
{
printf("Queue point address NULL\n");
return -1;
}
listQueueInfo *pNewQueue = (listQueueInfo *)malloc(sizeof(listQueueInfo));
if (NULL == pNewQueue)
{
printf("initQueue malloc error\n");
return -1;
}
Node *pNewNode = (Node *)malloc(sizeof(Node));
if (NULL == pNewNode)
{
printf("init Head Node malloc error\n");
return -1;
}
pNewNode->pNext = NULL;
pNewQueue->front = pNewQueue->rear = pNewNode;
*ppQ = pNewQueue;
return 0;
}
/* 入队列 */
int pushQueue(listQueueInfo *pQ, ElemType val)
{
if (NULL == pQ)
{
printf("Queue is NULL\n");
return -1;
}
Node *pNewNode = (Node *)malloc(sizeof(Node));
if (NULL == pNewNode)
{
printf("Queue Node malloc error\n");
return -1;
}
memcpy(&pNewNode->data, &val, sizeof(Node));
pNewNode->pNext = NULL;
pQ->rear->pNext = pNewNode;
pQ->rear = pNewNode;
return 0;
}
/* 出队列 */
int popQueue(listQueueInfo *pQ, ElemType *val)
{
if ((NULL == pQ) || (NULL == val))
{
printf("Queue NULL or val point NULL\n");
return -1;
}
/* 判空 */
if (pQ->front == pQ->rear)
{
printf("Queue empty\n");
return -1;
}
/* 取头结点(也是空结点)后的第一个数据结点 */
Node *pFirstNode = pQ->front->pNext;
memcpy(val, &pFirstNode->data, sizeof(ElemType));
/* 只有一个数据结点 */
if (pFirstNode == pQ->rear)
{
pQ->rear = pQ->front;
}
pQ->front->pNext = pFirstNode->pNext;
free(pFirstNode);
pFirstNode = NULL;
return 0;
}
/* 取队列头数据 */
int queueFront(listQueueInfo *pQ, ElemType *val)
{
if ((NULL == pQ) || (NULL == val))
{
printf("Queue NULL or ElemType val NULL\n");
return -1;
}
if (pQ->front == pQ->rear)
{
printf("listQueue is empty\n");
return -1;
}
memcpy(val, &pQ->front->pNext->data, sizeof(ElemType));
return 0;
}
/* 计算队列有效数据长度 */
int queueCapacity(listQueueInfo *pQ, unsigned int *size)
{
if ((NULL == pQ) || (NULL == size))
{
printf("Queue is NULL or point of size is NULL\n");
return -1;
}
Node *pTempNode = pQ->front;
unsigned int tempSize = 0;
while (pTempNode != pQ->rear)
{
tempSize++;
pTempNode = pTempNode->pNext;
}
*size = tempSize;
return 0;
}
/* 清空链式队列 */
int emptyQueue(listQueueInfo *pQ)
{
if (NULL == pQ)
{
printf("Queue NULL\n");
return -1;
}
/* 判空 */
if (pQ->front == pQ->rear)
{
printf("Queue empty\n");
return -1;
}
/* 保留头结点,清空所有数据结点 */
Node *pFirstNode = NULL;
while(pQ->front != pQ->rear)
{
pFirstNode = pQ->front->pNext;
if (pFirstNode == pQ->rear)
{
pQ->rear = pQ->front;
}
pQ->front->pNext = pFirstNode->pNext;
free(pFirstNode);
}
pFirstNode = NULL;
return 0;
}
/* 销毁链式队列 */
int destoryQueue(listQueueInfo **ppQ)
{
if (NULL == ppQ)
{
printf("pQenen point address NULL\n");
return -1;
}
if (NULL == *ppQ)
{
printf("pQueue point NULL\n");
return -1;
}
emptyQueue(*ppQ);
free(*ppQ);
*ppQ = NULL;
return 0;
}
/* 测试程序入口 */
int main(void)
{
listQueueInfo *pQueue = NULL;
ElemType printValue = 0;
unsigned int sizeOfQueue = 0;
/* 队列项为10 */
initQueue(&pQueue);
printValue = 0;
sizeOfQueue = 0;
for (int i=0; i<11; i++)
{
pushQueue(pQueue, (ElemType)i);
queueCapacity(pQueue, &sizeOfQueue);
printf("pQueue capacity: %u\n", sizeOfQueue);
queueFront(pQueue, &printValue);
printf("pQueue front value: %d\n", printValue);
}
printValue = 0;
sizeOfQueue = 0;
for (int i=0; i<11; i++)
{
queueCapacity(pQueue, &sizeOfQueue);
printf("pQueue capacity: %u\n", sizeOfQueue);
queueFront(pQueue, &printValue);
printf("pQueue front value: %d\n", printValue);
popQueue(pQueue, &printValue);
printf("pQueue pop value: %d\n", printValue);
}
emptyQueue(pQueue);
destoryQueue(&pQueue);
return 0;
}

浙公网安备 33010602011771号