算法-队列
队列的介绍
队列(Queue),是一种线性存储结构。它有以下几个特点:
(01) 队列中数据是按照"先进先出(FIFO, First-In-First-Out)"方式进出队列的。
(02) 队列只允许在"队首"进行删除操作,而在"队尾"进行插入操作。
队列通常包括的两种操作:入队列 和 出队列。
1. 队列的示意图
队列中有10,20,30共3个数据。
2. 出队列
出队列前:队首是10,队尾是30。
出队列后:出队列(队首)之后。队首是20,队尾是30。
3. 入队列
入队列前:队首是20,队尾是30。
入队列后:40入队列(队尾)之后。队首是20,队尾是40。
用c语言实现一个可以容纳任何数据的队列实现
1 /* 2 * name :queue.c 3 * description: queue 4 * single list 5 *@istta 6 * Email: hcu5555@hotmail.com 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include "queue.h" 12 13 static struct queue_info * creat_node(void * data) 14 { 15 struct queue_info * new_node = NULL; 16 new_node = (struct queue_info *)malloc(sizeof(struct queue_info)); 17 if(new_node ==NULL){ 18 printf("%s malloc error\n", __FUNCTION__); 19 return NULL; 20 } 21 else{ 22 new_node->data = data; 23 new_node->next = NULL; 24 } 25 26 return new_node; 27 } 28 29 int queue_init() 30 { 31 queue_single_list = creat_node(NULL); 32 if(queue_single_list == NULL) 33 { 34 printf("creat queue is error\n"); 35 return -1; 36 } 37 queue_count = 0; 38 printf("init queue\n"); 39 return 0; 40 } 41 42 43 int queue_push(void * val) 44 { 45 if(queue_count == 0) 46 { 47 printf("queue is empty\n"); 48 queue_single_list = creat_node(val); 49 if(queue_single_list == NULL) 50 { 51 printf("creat node head fail \n"); 52 return -1; 53 } 54 55 queue_count++; 56 return 0; 57 } 58 59 if(queue_count >=queue_max) 60 { 61 printf("queue is ful\n"); 62 return 0; 63 } 64 65 struct queue_info *p_node = creat_node(val); 66 struct queue_info *p_end = queue_single_list; 67 68 while(p_end->next) 69 p_end= p_end->next; 70 71 p_end->next = p_node; 72 queue_count++; 73 74 return 0; 75 } 76 77 void * queue_pop() 78 { 79 void * ret; 80 ret = queue_single_list->data; 81 82 struct queue_info * p_node; 83 p_node = queue_single_list; 84 queue_single_list = p_node->next; 85 free(p_node); 86 87 queue_count--; 88 return ret; 89 } 90 91 int queue_is_empty() 92 { 93 return queue_count; 94 } 95 96 int queue_size() 97 { 98 int count= 0; 99 struct queue_info * p_node = queue_single_list; 100 while(p_node) 101 { 102 count++; 103 p_node=p_node->next; 104 } 105 106 return count; 107 } 108 109 void * queue_front() 110 { 111 return queue_single_list->data; 112 }
/* * name :queue.h * description: queue * single list *@istta * Email: hcu5555@hotmail.com */ #ifndef __QUEUE_H__ #define __QUEUE_H__ #ifdef cplusplus extern "C" { #endif struct queue_info { void * data; struct queue_info *next; }; struct queue_info *queue_single_list; int queue_count; int queue_max; int queue_init(); int queue_push(void *pval); void *queue_pop(); int queue_size(); int queue_is_empty(); void * queue_front(); #ifdef cplusplus } #endif #endif
测试代码:
#include <stdio.h> #include <stdlib.h> #include "queue.h" struct end_joined { unsigned int device_id; }; struct end_leave { unsigned int device_id; }; struct check { unsigned int coord_id; unsigned int endd_id; }; int main(void) { struct end_joined a; struct end_leave b; struct check c; a.device_id = 50004; b.device_id = 50004; c.coord_id = 50004; c.endd_id = 3; queue_max = 3; queue_init(); printf("queue is %s\n", ((queue_is_empty()) == 0 )? "is empty" : "is not empty"); if(queue_push(&a) !=0) printf("queue push is error\n"); if(queue_push(&b) !=0) printf("queue push is error\n"); printf("queue is %s\n", ((queue_is_empty()) == 0 )? "is empty" : "is not empty"); printf("queue size is %d\n", queue_size()); struct end_joined *tmp = NULL; tmp = (struct end_joined *)queue_pop(); printf("device_id %d\n", tmp->device_id); if(queue_push(&c) !=0) printf("queue push is error\n"); struct end_leave *tmp2 = NULL; tmp2 = (struct end_leave *)queue_pop(); printf("device_id %d\n", tmp->device_id); printf("queue size is %d\n", queue_size()); struct check *tmp3; tmp3 = (struct check *)queue_pop(); printf("device_id %d, port %d\n", tmp3->coord_id ,tmp3->endd_id); printf("queue size is %d\n", queue_size()); // queue_max = 3; // int i =0 ; queue_push(&a); queue_push(&a); queue_push(&a); queue_push(&a); queue_push(&b); queue_push(&c); printf("queue size is %d\n", queue_size()); // struct check *d, *f, * h, *g; // queue_push(d); // queue_push(f); // queue_push(h); // queue_push(g); return 0; }
作者:issta hu
出处:http://www.cnblogs.com/hcu5555/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。