c 队列模拟

本文介绍了一个简单的队列数据结构的实现及其基本操作,包括初始化、判断队列是否为空或已满、获取队列中元素数量、入队和出队等,并提供了一个简单的测试程序来演示这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件 queue.h

#ifndef _QUEUE_H_
#define _QUEUE_H_

#include <stdbool.h>

typedef int Item;
#define MAXQUEUE 10

typedef struct node
{
	Item item;
	struct node* next;
} Node;

typedef struct queue
{
	Node* front;
	Node* rear;
	int items;
} Queue;

/**
* 操作: 初始化队列
* 前置条件: pq指向一个队列
* 后置条件: 队列被初始化为空
*/
void InitializeQueue(Queue* pq);

/**
* 操作: 检查队列是否已满
* 前置条件: pq指向一个队列
* 后置条件: 如果队列已满,返回true,否则返回false
*/
bool QueueIsFull(const Queue* pq);


/**
* 操作: 检查队列是否为空
* 前置条件: pq指向一个队列
* 后置条件: 如果队列为空,返回true,否则返回false
*/
bool QueueIsEmpty(const Queue* pq);

/**
* 操作: 确定队列中的项数
* 前置条件: pq指向一个队列
* 后置条件: 返回队列中的项数
*/
int QueueItemCount(const Queue* pq);

/**
* 操作: 在队列末尾添加项
* 前置条件: pq指向一个队列, item是将被添加在队列的末尾
* 后置条件: 添加成功返回true,否则返回false
*/
bool EnQueue(Item item, Queue* pq);

/**
* 操作: 从队列开头删除项
* 前置条件: pq指向一个队列, 队列首端的item将被拷贝到*pitem中并被删除
* 后置条件: 删除成功返回true,否则返回false
*/
bool DeQueue(Item* pitem, Queue* pq);

/**
* 操作: 清空队列
* 前置条件: pq指向一个队列
* 后置条件: 队列被清空
*/
void EmptyTheQueue(Queue* pq);

#endif 

实现文件 queue.c

#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

static void CopyToNode(Item item, Node* node);
static void CopyToItem(Node* pn, Item* pi);

void InitializeQueue(Queue* pq)
{
	pq->front = pq->rear = NULL;
	pq->items = 0;
}

bool QueueIsFull(const Queue* pq)
{
	return pq->items == MAXQUEUE;
}

bool QueueIsEmpty(const Queue* pq)
{
	return pq->items == 0;
}

int QueueItemCount(const Queue* pq) 
{
	return pq->items;
}

bool EnQueue(Item item, Queue* pq)
{
	Node* pnew;

	if (QueueIsFull(pq)) {
		return false;
	}

	pnew = (Node*)malloc(sizeof(Node));
	if (pnew == NULL) {
		fprintf(stderr, "Unable to allocate memory!\n");
		exit(1);
	}

	CopyToNode(item, pnew);
	pnew->next = NULL;

	if (QueueIsEmpty(pq)) {
		pq->front = pnew;
	}
	else {
		pq->rear->next = pnew;
	}

	pq->rear = pnew;
	pq->items++;

	return true;
}

bool DeQueue(Item* pitem, Queue* pq)
{
	Node* pt;
	
	if (QueueIsEmpty(pq)) {
		return false;
	}

	CopyToItem(pq->front, pitem);
	pt = pq->front;
	pq->front = pq->front->next;
	free(pt);
	pq->items--;
	if (pq->items == 0) {
		pq->rear = NULL;
	}

	return true;
}

void EmptyTheQueue(Queue* pq)
{
	Item dummy;
	while (!QueueIsEmpty(pq)) {
		DeQueue(&dummy, pq);
	}
}

static void CopyToNode(Item item, Node* node)
{
	node->item = item;
}

static void CopyToItem(Node* pn, Item* pi)
{
	*pi = pn->item;
}

测试 main.c

#include <stdio.h>

#include "queue.h"

int main(void)
{
	Queue line;
	Item temp;
	char ch;

	InitializeQueue(&line);

	puts("Testing the Queue interface, Type a to add a value.\n ");
	puts("Type d to delete a value, and type q to quit.\n");

	while ((ch = getchar()) != 'q') {
		if (ch != 'a' && ch != 'd') {
			continue;
		}
		if (ch == 'a') {
			printf("Integer to add: ");
			scanf_s("%d", &temp);
			if (!QueueIsFull(&line)) {
				printf("Putting %d into queue\n", temp);
				EnQueue(temp, &line);
			}
			else {
				puts("Queue is full\n");
			}
		}
		else {
			if (QueueIsEmpty(&line)) {
				puts("Nothing to delete.\n");
			}
			else {
				DeQueue(&temp, &line);
				printf("Removing %d from queue.\n", temp);
			}
		}
		printf("%d items in queue\n", QueueItemCount(&line));
		puts("Type a to add, d to delete, q to quit.\n");
	}

	EmptyTheQueue(&line);
	puts("Done\n");

	return 0;
}

来自《c primer plus》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值