数据结构:队列的实现
函数声明以及头文件:
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
typedef struct QueueNode
{
DataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;
QueueNode* _tail;
DataType _size;
}Queue;
Queue* QueueInit();//初始化
void QueuePush(Queue* q, DataType x);//尾插
void QueuePop(Queue* q);//头删
DataType QueueFront(Queue* q);//返回队列的头
DataType QueueBack(Queue* q);//返回队列的尾
size_t QueueSize(Queue* q);//队列的大小
int QueueEmpty(Queue* q);//是否为空队列
函数实现:
Queue* QueueInit()
{
Queue* q = (Queue*)malloc(sizeof(Queue));
if (q == NULL)
{
printf("创建失败\n");
return NULL;
}
q->_head = NULL;
q->_tail = NULL;
q->_size = 0;
return q;
}
void QueuePush(Queue* q, DataType x)
{
assert(q);
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
if (newNode == NULL)
{
printf("创建失败\n");
return;
}
newNode->_data = x;
newNode->_next = NULL;
if (q->_size == 0)
{
q->_head = newNode;
q->_tail = newNode;
}
else
{
q->_tail->_next = newNode;
q->_tail = newNode;
}
q->_size++;
}
void QueuePop(Queue* q)
{
assert(q);
QueueNode* cur = q->_head->_next;
if (q->_size == 0)
{
printf("队列为空无法删除\n");
return;
}
else if (q->_size == 1)
{
free(q->_head);
q->_head = NULL;
q->_tail = NULL;
}
else
{
free(q->_head);
q->_head = cur;
}
q->_size--;
}
DataType QueueFront(Queue* q)
{
assert(q);
if (q->_size == 0)
{
printf("队列为空\n");
return -1;
}
else
{
return q->_head->_data;
}
}
DataType QueueBack(Queue* q)
{
assert(q);
if (q->_size == 0)
{
printf("队列为空\n");
return -1;
}
else
{
return q->_tail->_data;
}
}
size_t QueueSize(Queue* q)
{
return (size_t)q->_size;
}
int QueueEmpty(Queue* q)
{
assert(q);
if (q->_size == 0)
{
return 1;
}
else
return 0;
}
void Queueprint(Queue* q)
{
QueueNode*cur = q->_head;
if (q->_size == 0)
{
printf("队列为空\n");
return;
}
while (cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
}