C语言队列

这篇博客介绍了使用C++实现先进先出(FIFO)队列的数据结构,包括初始化、检查队列状态、添加元素、删除元素和清空队列等操作。代码中展示了如何创建和管理队列,并通过一个简单的命令行应用程序演示了队列的使用,如添加和删除元素。此外,博客还包含了队列满和空的判断条件。
摘要由CSDN通过智能技术生成

 队列为先进先出。实例:手机应用商店应用更新队列,但这个可以强制调换队列顺序。

代码注释已经很详细了,不在解释。

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;

//队列初始化为空
void InitializeQueue(Queue* pq);

//检查队列是否以满
bool QueueIsFull(const Queue* pq);

//检查队列是否为空
bool QueueIsEmtpy(const Queue* pq);

//确定队列中的项数
int QueueItemCount(const Queue* pq);

//在队列末尾添加项
bool EnQueue(Item item, Queue* pq);

//从队列的开头删除项
bool DeQueue(Item* pitem, Queue* pq);

//清空队列
void EmptyTheQueue(Queue* pq);

#endif

Queue.cpp

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

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

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

bool QueueIsFull(const Queue* pq)  //判断是否已满  10
{
	return pq->items == MAXQUEUE;
}

bool QueueIsEmtpy(const Queue* pq)  //检查队列是否为空
{
	return pq->items == 0;
}

int QueueItemCount(const Queue* pq)  //确定队列中的项数
{
	return pq->items;
}

bool EnQueue(Item item, Queue* pq)  //在末尾添加项  item=1;
{
	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 (QueueIsEmtpy(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 (QueueIsEmtpy(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 (!QueueIsEmtpy(pq))
		DeQueue(&dummy, pq);
}

void CopyToNode(Item item, Node* pn)  //拷贝到节点
{
	pn->item = item;
}

void CopyToItem(Node* pn, Item* pi)    //拷贝到项目
{
	*pi = pn->item;
}

UseQueue.cpp

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

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

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

	while ((ch = getchar()) != 'q')
	{
		if (ch != 'a' && ch != 'd')
		{
			printf("incorrect input\n");
			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!");
		}
		else
		{
			if (QueueIsEmtpy(&line))
				puts("Noting to delete!");
			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:");
	}

	EmptyTheQueue(&line);
	puts("bye!");

	return 0;
}

运行结果

 Success is going from failure to failure without losing enthusiasm.

成功是从一次失败走向另一次失败,却不失热情。

 

#include #include #include //队列最大长度 #define MAX_QUEUE 1024 //偷懒,就用静态队列了 static int mQueue[MAX_QUEUE]; //队列插入 void InsertData(int **Front, int **Rear) { if (*Rear + 1 == *Front && (*Rear + 1 - MAX_QUEUE != *Front)) { //当队列数据已满,返回 puts("Queue Size Overflow!\n"); return; } else if (*Rear - mQueue > MAX_QUEUE) { //实现的是类似循环队列,但由于是静态线性队列(数组) //而不是用链表来实现的,所以到静态队列(数组)尾部,尾指针自动指向(数组)头部 *Rear = mQueue; } puts("Input Data:"); scanf("%d", *Rear); //输入数据后,尾指针后移 *Rear += 1; } //从头指针删除一个队列中的数据 void DeleteData(int **Front, int **Rear) { if (*Front == *Rear) { //头指针尾指针重合,队列空,不能删除,返回 puts("Queue Empty!\n"); return; } else if (*Front - mQueue > MAX_QUEUE) { //参考 Rear *Front = mQueue; } //从头指针删除一个数据 *Front += 1; } //显示队列数据 void ShowData(int **Front, int **Rear) { int *temp; for (temp=*Front; temp!=*Rear; temp++) { printf("%d --> ", *temp); } puts("\n"); } void usage(void) { puts("1. Insert Data"); puts("2. Delete Data"); puts("3. Show Data"); } int main(int argc, char **argv) { //头指针,尾指针 //队列的一个特性 First in first out FIFO int *pFront, *pRear; int op_code; //初始化队列,头指针和尾指针此时指向的地址相同 pFront = pRear = mQueue; while (1) { usage(); scanf("%d", &op_code); switch (op_code) { case 1: printf("%p\n", pFront); printf("%d\n", *pFront); InsertData(&pFront, &pRear); break; case 2: DeleteData(&pFront, &pRear); break; case 3: ShowData(&pFront, &pRear); break; default: break; } } return 0; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值