1、头文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "stdbool.h"
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QUeueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
2、接口函数
#include "Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
QueueNode* cur = pq->head;
assert(pq);
while(cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->data = x;
newnode->next = NULL;
if(!pq->head)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)
{
QueueNode* next = pq->head->next;
assert(pq);
assert(pq->head != NULL);
free(pq->head);
pq->head = next;
if(!pq->head)
pq->tail = NULL;
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head != NULL);
return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->tail != NULL);
return pq->tail->data;
}
int QUeueSize(Queue* pq)
{
int size = 0;
QueueNode* cur = pq->head;
while(cur)
{
++size;
cur = cur->next;
}
return size;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
int main()
{
Queue q;
QueueInit(&q);
return 0;
}