C实现队列

头文件

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;

}QNode;
typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}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);
///检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* pq);
//个数
int QueueSize(Queue* pq);

功能实现

#include "Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* tmp = cur;
		cur = cur->next;
		free(tmp);
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* tmp = pq->head;
		pq->head = pq->head->next;
	}
	pq->size--;
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL && pq->tail == NULL;//Ϊ1Ϊ0
}
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

运行

#include "Queue.h"
int main()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	printf("%d\n", QueueSize(&q));
	printf("%d\n", QueueEmpty(&q));
	printf("%d\n", QueueFront(&q));
	printf("%d\n", QueueBack(&q));
	QueuePop(&q);
	printf("%d\n", QueueSize(&q));
	printf("%d\n", QueueFront(&q));
	printf("%d\n", QueueBack(&q));
	QueuePush(&q, 5);
	QueuePush(&q, 6);
	QueuePush(&q, 7);
	printf("%d\n", QueueSize(&q));
	printf("%d\n", QueueFront(&q));
	printf("%d\n", QueueBack(&q));
	while(!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	printf("%d\n", QueueSize(&q));
	printf("%d\n", QueueEmpty(&q));
	QueueDestroy(&q);
	return 0;
}

在 C 语言中,实现队列通常有两种方式:基于数组和基于链表。 ### 基于链表实现队列 队列是一种先进先出(FIFO)的线性数据结构,在任务调度、消息队列等场景广泛应用[^2]。可以定义一个包含头指针、尾指针和队列长度的结构体来表示队列,同时定义节点结构体用于存储数据和指向下一个节点的指针。 以下是一个示例代码: ```c // 定义队列节点结构体 typedef struct QNode { int data; struct QNode* next; } QNode; // 定义队列结构体 typedef struct Queue { QNode* head; QNode* tail; int size; // 用来记录队列长度 } Queue; // 初始化队列 void QueueInit(Queue* pq) { pq->head = pq->tail = NULL; pq->size = 0; } // 创建新节点 QNode* createNode(int data) { QNode* newNode = (QNode*)malloc(sizeof(QNode)); newNode->data = data; newNode->next = NULL; return newNode; } // 入队操作 void enqueue(Queue* pq, int data) { QNode* newNode = createNode(data); if (pq->tail == NULL) { pq->head = pq->tail = newNode; } else { pq->tail->next = newNode; pq->tail = newNode; } pq->size++; } // 出队操作 int dequeue(Queue* pq) { if (pq->head == NULL) { return -1; // 队列为空 } QNode* temp = pq->head; int data = temp->data; pq->head = pq->head->next; if (pq->head == NULL) { pq->tail = NULL; } free(temp); pq->size--; return data; } // 判断队列是否为空 int queue_empty(Queue* pq) { return pq->size == 0; } // 队列的销毁 void QueueDestroy(Queue* pq) { while (!queue_empty(pq)) { dequeue(pq); } } ``` 可以使用以下测试代码对上述功能进行测试: ```c #include <stdio.h> #include <stdlib.h> #include <assert.h> // 上述队列实现代码 int main(int argc, const char *argv[]) { Queue lq; QueueInit(&lq); char input[10]; printf("please Num or Char\n"); while (1) { scanf("%s", input); if (input[0] >= '0' && input[0] <= '9') { enqueue(&lq, atoi(input)); } else { if (!queue_empty(&lq)) { printf("dequeue:%d\n", dequeue(&lq)); } } } QueueDestroy(&lq); return 0; } ``` ### 基于数组实现队列 也可以使用数组来实现队列,通过维护队头和队尾的索引来实现入队和出队操作。 ```c #define MAX_SIZE 100 typedef struct { int data[MAX_SIZE]; int front; int rear; } ArrayQueue; // 初始化队列 void ArrayQueueInit(ArrayQueue* aq) { aq->front = aq->rear = 0; } // 入队操作 void ArrayEnqueue(ArrayQueue* aq, int data) { if ((aq->rear + 1) % MAX_SIZE == aq->front) { printf("Queue is full\n"); return; } aq->data[aq->rear] = data; aq->rear = (aq->rear + 1) % MAX_SIZE; } // 出队操作 int ArrayDequeue(ArrayQueue* aq) { if (aq->front == aq->rear) { printf("Queue is empty\n"); return -1; } int data = aq->data[aq->front]; aq->front = (aq->front + 1) % MAX_SIZE; return data; } // 判断队列是否为空 int ArrayQueueEmpty(ArrayQueue* aq) { return aq->front == aq->rear; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值