队列基于链表实现,存储头节点和尾节点
定义队列结构体
typedef int QDataType;
typedef struct QueueNode
{
int val;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
创立队列的节点
QNode* QuBuyNode(QDataType x)
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
入队列
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = QuBuyNode(x);
if (pq->ptail)//队列不为空
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
else
{
pq->phead = pq->ptail = newnode;
}
pq->size++;
}
出队列
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->phead != NULL);
if (pq->phead->next == NULL)//一个节点
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else//多个节点
{
QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
取队头元素
//队头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead != NULL);
return pq->phead->val;
}
取队尾元素
//队尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->ptail != NULL);
return pq->ptail->val;
}
队列大小
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
队列的销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* pucr = pq->phead;
while (pucr)
{
QNode* next = pucr->next;
free(pucr);
pucr = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
队列是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
int val;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
QNode* QuBuyNode(QDataType x);
void QueuePush(Queue* pq, QDataType x);
bool QueueEmpty(QNode* pq);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* pucr = pq->phead;
while (pucr)
{
QNode* next = pucr->next;
free(pucr);
pucr = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
QNode* QuBuyNode(QDataType x)
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = QuBuyNode(x);
if (pq->ptail)//队列不为空
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
else
{
pq->phead = pq->ptail = newnode;
}
pq->size++;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->phead != NULL);
if (pq->phead->next == NULL)//一个节点
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else//多个节点
{
QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
//队头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead != NULL);
return pq->phead->val;
}
//队尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->ptail != NULL);
return pq->ptail->val;
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
while (!QueueEmpty(&q))
{
int ret = QueueFront(&q);
printf("%d ", ret);
QueuePop(&q);
}
QueueDestroy(&q);
return 0;
}