C语言队列的入队出队

queue.c

void InitQueue(S_Queue *q)
{
	q->front = 0;
	q->rear = 0;
	q->num = 0;
}
// insert a node
bool EnQueue(S_Queue *q, U_QueueNode *p,u16 queueSize)
{
	U_QueueNode *pos;
	
	if(QueIsFull(q,queueSize)) 
	{
		return FALSE;
	}
	pos = q->node + q->rear;
	q->curIndex = q->rear;
	memcpy(pos,p,sizeof(U_QueueNode));

	q->rear = (q->rear+1) % queueSize;
	q->num++;
	return TRUE;
}

// delete a node and return the node 
U_QueueNode *DeQueue(S_Queue *q,u16 queueSize)
{
	U_QueueNode *pos;
	
	if(QueIsEmpty(q)) 
	{
		return NULL;
	}
	
	pos = q->node + q->front;
	q->front = (q->front+1) % queueSize;
	q->num--;

	return pos;
}


// return queue head node
U_QueueNode *QueHeadNode(S_Queue *q)
{
	U_QueueNode *p;
	
	p = QueIsEmpty(q) ?  NULL :  (q->node + q->front);
		
	return p;
}

// if queue is empty return true
bool QueIsEmpty(S_Queue *q)
{
	return (bool)(0 == q->num);
}

// if queue is full return true
bool QueIsFull(S_Queue *q,u16 queueSize)
{
	return (bool)(queueSize <= q->num);
}

queue.h

typedef union
{
	BaliseParame_S BaliseParame;
	u8 buf[140];
	u16 Word[68];
}U_QueueNode;

typedef struct
{
	u16 front;  	// queue head, outlet							
	u16 rear;			// queue tail inlet
	u16 num;			// node number
	u16 curIndex;

	U_QueueNode node[BTM_QUEUE_SIZE];
}S_Queue;


void InitQueue(S_Queue *q);
bool EnQueue(S_Queue *q,U_QueueNode *p,u16 queueSize);// insert a node
U_QueueNode *DeQueue(S_Queue *q,u16 queueSize);// delete a node and return the node 
U_QueueNode *QueHeadNode(S_Queue *q);// return queue head node
bool QueIsEmpty(S_Queue *q); // if queue is empty return true
bool QueIsFull(S_Queue *q,u16 queueSize); // if queue is full return true

<think>嗯,用户想了解队列入队出队操作在C语言中的函数实现。首先,我需要确认他们对队列的基本概念是否清楚。队列是先进先出的数据结构,对吧?所以入队(enqueue)是在队尾添加元素,出队(dequeue)是从队头移除元素。接下来,可能要考虑用什么样的数据结构来实现,比如数组还是链表。数组实现的话,可能会有循环队列来处理假溢出的问题,而链表则更灵活,不需要担心容量限制。 用户可能对两种实现方式都感兴趣,或者只想知道其中种。但为了全面性,应该都介绍下。然后,具体到函数,比如用结构体定义队列,包含元素、队头、队尾等信息。数组实现需要固定大小,而链表则需要动态分配节点。 然后,入队操作需要考虑队列是否已满(数组)或内存分配是否成功(链表)。出队操作则要检查队列是否为空,避免下溢。代码示例要清晰,注释也要用中文,方便理解。可能还需要提到错误处理,比如返回特定值表示成功或失败。 另外,用户可能在实际应用中遇到问题,比如多线程环境下的队列操作是否需要锁,但这个问题可能超出了基本介绍的范畴。不过还是先专注于基础实现。需要确保代码示例正确,语法无误,特别是指针操作数组索引的处理,比如循环队列的队尾计算。 最后,总结两种实现的优缺点,帮助用户根据实际需求选择。比如数组实现更简单,但大小固定;链表动态扩展,但需要更多内存管理。确保回答结构清晰,分步骤讲解,让用户能步步理解实现。</think>队列种先进先出(FIFO)的线性数据结构。以下是用C语言实现队列的详细步骤函数示例: 队列的核心操作 1. 入队(Enqueue):在队列尾部添加元素 2. 出队(Dequeue):从队列头部移除元素 二、数组实现(固定大小队列) ```c #define MAX_SIZE 100 typedef struct { int items[MAX_SIZE]; int front; // 队头索引 int rear; // 队尾索引 } ArrayQueue; // 初始化队列 void initQueue(ArrayQueue *q) { q->front = -1; q->rear = -1; } // 入队函数 int enqueue(ArrayQueue *q, int value) { if (q->rear == MAX_SIZE - 1) { printf("队列已满\n"); return 0; // 失败 } if (q->front == -1) { q->front = 0; // 初始化队头 } q->rear++; q->items[q->rear] = value; return 1; // 成功 } // 出队函数 int dequeue(ArrayQueue *q) { if (q->front == -1 || q->front > q->rear) { printf("队列为空\n"); return -1; // 错误值 } int value = q->items[q->front]; q->front++; if (q->front > q->rear) { // 重置队列为空状态 q->front = q->rear = -1; } return value; } ``` 三、链表实现(动态大小队列) ```c typedef struct Node { int data; struct Node *next; } QueueNode; typedef struct { QueueNode *front; // 队头指针 QueueNode *rear; // 队尾指针 } LinkedQueue; // 初始化队列 void initLinkedQueue(LinkedQueue *q) { q->front = q->rear = NULL; } // 入队函数 void enqueueLinked(LinkedQueue *q, int value) { QueueNode *newNode = (QueueNode*)malloc(sizeof(QueueNode)); newNode->data = value; newNode->next = NULL; if (q->rear == NULL) { // 空队列 q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } // 出队函数 int dequeueLinked(LinkedQueue *q) { if (q->front == NULL) { printf("队列为空\n"); return -1; // 错误值 } QueueNode *temp = q->front; int value = temp->data; q->front = q->front->next; if (q->front == NULL) { q->rear = NULL; // 队列置空 } free(temp); return value; } ``` 四、实现要点分析 1. 数组实现特点: - 时间复杂度:$O(1)$ 的入队/出队操作 - 需要预先分配固定空间 - 需要处理循环队列逻辑(上述示例未实现循环,可添加取模运算改进) 2. 链表实现特点: - 动态内存管理 - 无需预先分配空间 - 需要处理指针操作 五、使用示例 ```c int main() { // 数组队列测试 ArrayQueue aq; initQueue(&aq); enqueue(&aq, 10); enqueue(&aq, 20); printf("出队元素:%d\n", dequeue(&aq)); // 输出10 // 链表队列测试 LinkedQueue lq; initLinkedQueue(&lq); enqueueLinked(&lq, 30); enqueueLinked(&lq, 40); printf("出队元素:%d\n", dequeueLinked(&lq)); // 输出30 return 0; } ``` 六、注意事项 1. 数组实现应考虑循环队列优化,避免假溢出 2. 链表实现每次出队后必须释放内存 3. 多线程环境下需要添加同步机制 4. 实际应用中建议封装错误处理机制 两种实现方式的选择取决于具体应用场景:需要固定大小高性能时选数组实现,需要动态扩展时选链表实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值