C队列实现:
#include <stdio.h>
//节点
struct Node{
int data;//节点数据
Node *next;//下一节点
};
//队列
struct Queue{
Node *front;//队头
Node *end;//队尾
};
//创建节点
struct Node* createNode(int _data){
//创建节点内存
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = _data;//节点数据
newNode->next = NULL;//下一节点
return newNode;//返回节点
};
//创建队列
struct Queue* createQueue(){
//分配队列内存
struct Queue *newQueue = (struct Queue*)malloc(sizeof(struct Queue));
newQueue->front = newQueue->end = NULL;//默认队头队尾为空
return newQueue;//返回队列
};
//空队判断
bool isEmpty(struct Queue *q){
return q->front == NULL;
}
//取队头数据
int front(struct Queue *q){
if (isEmpty(q)) {
printf("队列为空");
return -1;
}
return q->front->data;
}
//队列大小
int size(struct Queue *q){
int count = 0;
struct Node *cur = q->front;