QUEUE - Implement a first in, first out linked list
/* * File: queue.h * Purpose: Implement a first in, first out linked list * * Notes: */ #ifndef _QUEUE_H_ #define _QUEUE_H_ /********************************************************************/ /* * Individual queue node */ typedef struct NODE { struct NODE *next; } QNODE; /* * Queue Struture - linked list of qentry items */ typedef struct { QNODE *head; QNODE *tail; } QUEUE; /* * Functions provided by queue.c */ void queue_init( QUEUE * ); int queue_isempty( QUEUE * ); void queue_add( QUEUE *, QNODE * ); QNODE* queue_remove( QUEUE * ); QNODE* queue_peek( QUEUE * ); void queue_move( QUEUE *, QUEUE * ); /********************************************************************/ #endif /* _QUEUE_H_ */ /* * File: queue.c * Purpose: Implement a first in, first out linked list * * Notes: */ #include "queue.h" /********************************************************************/ /* * Initialize the specified queue to an empty state * * Parameters: * q Pointer to queue structure */ void queue_init( QUEUE *q ) { q->head = NULL; } /********************************************************************/ /* * Check for an empty queue * * Parameters: * q Pointer to queue structure * * Return Value: * 1 if Queue is empty * 0 otherwise */ int queue_isempty( QUEUE *q ) { return ( q->head == NULL ); } /********************************************************************/ /* * Add an item to the end of the queue * * Parameters: * q Pointer to queue structure * node New node to add to the queue */ void queue_add( QUEUE *q, QNODE *node ) { if ( queue_isempty( q ) ) { q->head = q->tail = node; } else { q->tail->next = node; q->tail = node; } node->next = NULL; } /********************************************************************/ /* * Remove and return first (oldest) entry from the specified queue * * Parameters: * q Pointer to queue structure * * Return Value: * Node at head of queue - NULL if queue is empty */ QNODE* queue_remove( QUEUE *q ) { QNODE *oldest; if ( queue_isempty( q ) ) return NULL; oldest = q->head; q->head = oldest->next; return oldest; } /********************************************************************/ /* * Peek into the queue and return pointer to first (oldest) entry. * The queue is not modified * * Parameters: * q Pointer to queue structure * * Return Value: * Node at head of queue - NULL if queue is empty */ QNODE* queue_peek( QUEUE *q ) { return q->head; } /********************************************************************/ /* * Move entire contents of one queue to the other * * Parameters: * src Pointer to source queue * dst Pointer to destination queue */ void queue_move( QUEUE *dst, QUEUE *src ) { if ( queue_isempty( src ) ) return; if ( queue_isempty( dst ) ) dst->head = src->head; else dst->tail->next = src->head; dst->tail = src->tail; src->head = NULL; return; } /********************************************************************/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本