队列代码C实例

本文详细介绍了如何通过编写队列代码来掌握编程技巧,包括队列头文件和实现文件的构建过程,以及如何在主函数中应用队列操作。

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

学习编程的秘诀就是多实践,写个队列代码。

queue.h:

#ifndef H_QUEUE
#define H_QUEUE

typedef struct _node{
	char *data;
	struct _node *prev;
	struct _node *next;
} node;

typedef struct _queue{
	node *front;
	node *end;
} queue;

int push(char *, queue *);

char *pop(queue *);

#endif


queue.c:

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

int main(int argc, char *argv[])
{
	queue q;
	q.front = NULL;
	q.end = NULL;

	while (--argc > 0)
		push(*(++argv), &q);

	while (q.front != NULL)
		printf("%s\n", pop(&q));
}

int push(char *data, queue *q)
{
	// Construct a new node
	node *nnode = (node *)malloc(sizeof(node));
	nnode->data = data;
	nnode->prev = NULL;
	nnode->next = NULL;

	// If the queue is not empty
	if (q->end != NULL)
	{
		// Set the new node to be the last one;
		q->end->next = nnode;
		nnode->prev = q->end;
	}
	else
	{
		q->front = nnode;
	}

	q->end = nnode;

	return 0;
}

char *pop(queue *q)
{
	// If the queue is null, return
	if (q->front == NULL)
		return "";

	char *temp;

	if (q->front->next != NULL)
	{
		// Make the second one to be first one
		q->front->next->prev = NULL;
		temp = q->front->data;

		free(q->front);
		q->front = q->front->next;
	}
	else
	{
		temp = q->front->data;

		free(q->front);
		q->front = NULL;
		q->end = NULL;
	}

	return temp;
}


下面是一个简单的环形队列的 C 语言代码示例: ```c #include <stdio.h> #define BUFFER_SIZE 5 typedef struct { int data[BUFFER_SIZE]; int head; int tail; } CircularQueue; void initQueue(CircularQueue* queue) { queue->head = 0; queue->tail = 0; } int isFull(CircularQueue* queue) { return ((queue->tail + 1) % BUFFER_SIZE == queue->head); } int isEmpty(CircularQueue* queue) { return (queue->head == queue->tail); } void enqueue(CircularQueue* queue, int item) { if (isFull(queue)) { printf("Queue is full. Cannot enqueue.\n"); return; } queue->data[queue->tail] = item; queue->tail = (queue->tail + 1) % BUFFER_SIZE; } int dequeue(CircularQueue* queue) { if (isEmpty(queue)) { printf("Queue is empty. Cannot dequeue.\n"); return -1; } int item = queue->data[queue->head]; queue->head = (queue->head + 1) % BUFFER_SIZE; return item; } void printQueue(CircularQueue* queue) { if (isEmpty(queue)) { printf("Queue is empty.\n"); return; } printf("Queue: "); int i = queue->head; while (i != queue->tail) { printf("%d ", queue->data[i]); i = (i + 1) % BUFFER_SIZE; } printf("\n"); } int main() { CircularQueue queue; initQueue(&queue); enqueue(&queue, 1); enqueue(&queue, 2); enqueue(&queue, 3); enqueue(&queue, 4); enqueue(&queue, 5); printQueue(&queue); int item = dequeue(&queue); printf("Dequeued item: %d\n", item); enqueue(&queue, 6); enqueue(&queue, 7); printQueue(&queue); return 0; } ``` 以上代码实现了一个包含初始化、判满、判空、入队、出队和打印队列的环形队列。在 `main` 函数中,我们创建了一个 `CircularQueue` 类型的队列,并进行了一系列的入队和出队操作。最后,通过调用 `printQueue` 函数,打印出队列的当前状态。 需要注意的是,这只是一个简单的示例代码,实际应用中可能需要根据具体需求做一些扩展和优化,比如增加线程安全的操作或动态扩展队列大小等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值