队列(内核为单链表).c
2017-03-13 20:04
148 查看
#define NULL ((void *)0) #define MALLOC(dataType, n) malloc( sizeof(dataType)*n ) #define SCANF(pVal, val) scanf("%d", &val) #define FREE(Point) free(Point) typedef struct Node{ int data; struct Node * pNext; }NODE_T, *PNODE_T; typedef struct Queue_List{ PNODE_T pFront; PNODE_T pRear; }QUEUE_LIST_T, *PQUEUE_LIST_T; void Init_Queue_List(PQUEUE_LIST_T pQueue_List); void Traverse_Queue_List(PQUEUE_LIST_T pQueue_List); bool En_Queue_List(PQUEUE_LIST_T pQueue_List, int val); bool Out_Queue_List(PQUEUE_LIST_T pQueue_List, int * val); int main(void) { int val; QUEUE_LIST_T queue_list; Init_Queue_List(&queue_list); //En_Queue_List(&queue_list, 33); En_Queue_List(&queue_list, -90); //En_Queue_List(&queue_list, 21); En_Queue_List(&queue_list, 68); //En_Queue_List(&queue_list, 5); //En_Queue_List(&queue_list, 133); Traverse_Queue_List(&queue_list); if(Out_Queue_List(&queue_list, &val)){ printf("Out Queue success,val = %d\n", val); }else{ printf("Out Queue Fail!\n"); } if(Out_Queue_List(&queue_list, &val)){ printf("Out Queue success,val = %d\n", val); }else{ printf("Out Queue Fail!\n"); } if(Out_Queue_List(&queue_list, &val)){ printf("Out Queue success,val = %d\n", val); }else{ printf("Out Queue Fail!\n"); } Traverse_Queue_List(&queue_list); return 0; } void Init_Queue_List(PQUEUE_LIST_T pQueue_List) { PNODE_T pBase = (PNODE_T)MALLOC(NODE_T, 1); if(NULL == pBase){ printf("Allocation Fail!\n"); }else{ pBase->pNext = NULL; pBase->data = 0; pQueue_List->pFront = pBase; pQueue_List->pRear = pQueue_List->pFront; } } bool En_Queue_List(PQUEUE_LIST_T pQueue_List, int val) { PNODE_T pNew = (PNODE_T)MALLOC(NODE_T, 1); if(NULL == pNew){ printf("Allocation Fail!\n"); return false; }else{ pNew->pNext = NULL; pNew->data = 0; pQueue_List->pRear->data = val; pQueue_List->pRear->pNext = pNew; pQueue_List->pRear = pNew; return true; } } void Traverse_Queue_List(PQUEUE_LIST_T pQueue_List) { PNODE_T p = pQueue_List->pFront; while(p != pQueue_List->pRear) { printf("%xH: %d\n", p,p->data); p = p->pNext; } return; } bool Is_Empty(PQUEUE_LIST_T pQueue_List) { if(pQueue_List->pFront == pQueue_List->pRear) return true; else return false; } bool Out_Queue_List(PQUEUE_LIST_T pQueue_List, int * val) { if( Is_Empty(pQueue_List) ){ printf("Queue List is empty!\n"); return false; }else{ (*val) = pQueue_List->pFront->data; pQueue_List->pFront = pQueue_List->pFront->pNext; return true; } }
相关文章推荐
- 看完内核中的链表操作部分,写了个通用队列
- 队列的C语言实现(通过内核链表)
- 《Linux内核设计与实现》读书笔记(六)- 内核数据结构之链表与队列
- 内核数据结构:链表,队列,映射二叉树
- 双链表实现的消息队列,可支持优先级读取
- 数据结构---链表、堆栈、队列详解
- 基于数组和链表的队列实现
- linux 内核分析之等待队列
- 链表,栈,队列代码
- 数组,栈,队列,链表的区别
- 队列用链表实现
- 4--消息队列(报文队列)实践到内核--消息队列的控制
- 链表,队列,堆栈的区别
- 队列用链表实现(建立,插入新元素,删除元素,读取元素,全部删除,全部读出,判断是否为空,清空)
- Linux设备驱动工程师之路——内核链表的使用
- Delphi链表问题(队列)
- C++实现队列--数组实现和链表实现
- 《数据结构与算法分析:C语言描述》复习——第三章“线性表、栈和队列”——单向链表
- 详解Linux内核之双向循环链表
- 队列(链表实现)