My implementation of single direction queue by linked list
In computer science, a queue (/?kju?/ kew) is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known asenqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.
Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer.
Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers and linked lists.
我的代码实现:
queue.h
#ifndef _QUEUE_H #define _QUEUE_H 1 struct node { struct node* previous; struct node* next; int data; }; #define QUEUE_SIZE 10 #define ARRAY_SIZE 20 #define SUCCESS 1 #define FAILED 0 #include "queue_destory.h" #include "queue_enter.h" #include "queue_init.h" #include "queue_out.h" #include "queue_print.h" #endifqueue_test.c
/***************************************************** code writer : EOF code date : 2014.03.07 e-mail: jasonleaster@gmail.com code purpose: Just a test source file for queue. I would be happy, if my code will help you to know what is hash table. If something wrong with my code, please touch me by e-mail. *****************************************************/ #include <stdio.h> #include "queue.h" int main() { int temp = 0; int array[ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; struct node* p_queue_tail; struct node* p_queue_header; queue_init(&p_queue_header,&p_queue_tail,QUEUE_SIZE); for(temp = 0; temp < ARRAY_SIZE; temp++) { if(SUCCESS != queue_enter(&p_queue_tail,array[temp])) { printf("enter error!\n"); queue_destory(p_queue_tail); return 0; } queue_out(&p_queue_header); } queue_print(p_queue_tail); queue_destory(p_queue_tail); return 0; }
/****************************************************************** code writer : EOF code date : 2014.03.07 e-mail : jasonleaster@gmail.com Attention: Just a implementation of function queue_init. I would be happy, if my code will help you to know what is hash table. we assuem that header node‘s previous ... previous is tail and tail‘s next ... next is header. If something wrong with my code please touch me by e-mail. *******************************************************************/ #include <stdlib.h> #include "queue.h" #include <stdio.h> int queue_init(struct node** pp_queue_header,struct node** pp_queue_tail,int queue_size) { int temp = 0; struct node* p_current_node; struct node* p_next_node; for(temp = 0; temp< queue_size;temp++) { if(temp == 0) { (*pp_queue_header) = (struct node*)malloc(sizeof(struct node)); if((*pp_queue_header) == NULL) { printf("malloc error!\ninitilization failed!\n"); return FAILED; } p_current_node = (*pp_queue_header); p_current_node->data = 0; p_current_node->previous = NULL; p_current_node->next = NULL; p_next_node = p_current_node; (*pp_queue_header) = p_current_node; } else { p_current_node = (struct node*)malloc(sizeof(struct node)); if(p_current_node == NULL) { printf("malloc error!\ninitilization failed\n"); return FAILED; } p_next_node->previous = p_current_node; p_current_node->next = p_next_node; p_current_node->data = 0; p_current_node->previous = NULL; p_next_node = p_current_node; if(temp == queue_size-1) { (*pp_queue_tail) = p_current_node; } } } return SUCCESS; }
/**************************************************************** code writer : EOF code date: 2014.03.07 e-mail: jasonleaster@gmail.com code purpose: Just a implementation of function queue_enter. I would be happy, if my code will help you to know what is hash table. ****************************************************************/ #include <stdlib.h> #include "queue.h" #include <stdio.h> int queue_enter(struct node** pp_queue_tail,int number) { struct node* p_new_node = NULL; struct node* p_temp_node = NULL; p_new_node = (struct node*)malloc(sizeof(struct node)); if(p_new_node == NULL) { printf("malloc error!\nqueue enter failed\n"); return FAILED; } p_temp_node = (*pp_queue_tail); p_new_node->data = number; p_new_node->next = p_temp_node; p_temp_node->previous = p_new_node; (*pp_queue_tail) = p_new_node; return SUCCESS; }
queue_out.c
/*********************************************************************** code writer : EOF code date : 2014.03.07 e-mail: jasonleaster@gmail.com code purpose: Just a implementation of function queue_out. I would be happy, if my code will help you to know what is hash table. If something wrong with my code, please touch me by e-mail. ************************************************************************/ #include "queue.h" #include <stdlib.h> #include <stdio.h> void queue_out(struct node** pp_queue_header) { struct node* p_temp_node = NULL; p_temp_node = (*pp_queue_header); (*pp_queue_header) = (*pp_queue_header)->previous; (*pp_queue_header)->next = NULL; printf("queue out data :%d\n",p_temp_node->data); free(p_temp_node); }
queue_print.c
/****************************************************************************** code writer : EOF code date : 2014.03.07 e-mail: jasonleaster@gmail.com code purpose: Just a implementation of function queue_print. I would be happy, if my code will help you to know what is hash table. If something wrong with my code, please touch me by e-mail. ***********************************************************************************/ #include "queue.h" #include <stdio.h> void queue_print(struct node* p_queue_tail) { struct node* p_temp_node = NULL; p_temp_node = p_queue_tail; printf("queue data at this time:\n"); while(p_temp_node != NULL) { printf("%d ",p_temp_node->data); p_temp_node = p_temp_node->next; } printf("\n"); }
queue_enter.c
/********************************************************************* code writer : EOF code date : 2014.03.07 e-mail: jasonleaster@gmail.com code purpose: Just a implementation of function queue_enter. I would be happy, if my code will help you to know what is hash table. *******************************************************************/ #include <stdlib.h> #include "queue.h" int queue_destory(struct node* p_queue_tail) { struct node* p_temp_node = NULL; p_temp_node = p_queue_tail->next; while(p_temp_node != NULL) { p_queue_tail->next = p_temp_node->next; free(p_temp_node); p_temp_node = p_queue_tail->next; } free(p_queue_tail); }
老规矩简单的header file不一一呈现了,example:
#ifndef _QUEUE_PRINT_H #define _QUEUE_PRINT_H 1 extern void queue_print(struct node* p_queue_tail); #endif
我始终坚持开源精神,希望看过代码的人能挑出我代码中的瑕疵,也希望我的代码能帮助不懂队列的初学者理解队列
EOF
2014.03.08
于XTU 凌晨零点30分
queue implementation 队列的代码实现,布布扣,bubuko.com
原文:http://blog.csdn.net/cinmyheart/article/details/20744617