Note: The main purpose of this article is to record your learning process and facilitate your communication. For reprint, please indicate from:
Http://blog.csdn.net/ab198604
The queue implementation method is similar to the stack implementation method, and is also based on the linked list operation. Therefore, the specific code implementation of the queue is further encapsulated based on the original one-way linked list. The data storage and retrieval of queues are opposite to the stack. It advocates first-in-first-out, similar to buying tickets in our real-life queues. The first is to buy tickets first, and the second is to buy tickets first, the third ......
I. Queue structure and Interface Definition
Similar to the stack structure, the queue structure is the same as the linked list structure, so rename it with typedef. The Code is as follows:
/** Filename: queue. H * Author: zhm * Date: 2013-01-05 */# ifndef queue_h # define queue_h # include <stdlib. h> # include "list. H "typedef list queue; // rename it as queue
For linked list related definition interface and operation can log on to this address to view the relevant content: http://blog.csdn.net/ab198604/article/details/8253405
The following table lists some queue-related operation interface declarations:
/*public interface */#define queue_init list_init#define queue_destroy list_destroyint queue_enqueue(Queue *queue, const void *data);int queue_dequeue(Queue *queue, void **data);#define queue_peek(queue) ((queue)->head == NULL ? NULL : (queue)->head->data)#define queue_size list_size#endif
Similar to stack operations, a stack has queue initialization, queue destruction, column entry, and column-out operations. The initialization and destruction of the queue are the redefinition of the linked list operation interface by generic typedef statements. Queue_peek this macro is used to obtain the data field of the queue Header element. queue_size this macro is used to obtain the number of elements in the queue.
Ii. Queue interface Implementation Details
Because the initialization and destruction operations of queues are mainly the redefinition of chain table initialization and destruction, you can refer to the one-way chain table for Data Structure Learning. Here, we mainly implement the columns and columns of the queue. The essence of the implementation is to repackage the linked list operation. The code is simple, as follows:
/* * filename:queue.c * author: zhm * date: 2013-01-05 */#include <stdlib.h>#include <string.h>#include "list.h"#include "queue.h"/* queue_enqueue */int queue_enqueue(Queue *queue, const void *data){ return list_ins_next(queue, list_tail(queue), data);}/* queue_dequeue */int queue_dequeue(Queue *queue, void **data){ return list_rem_next(queue, NULL, data);}
The column operation actually calls the insert operation function interface of the linked list. The column operation calls the delete operation function interface of the linked list.
Iii. Application Examples of queues
In this example, the main logic is: first initialize the queue and then perform the column import operation. The sequence is: 0-> 1-> 2-> 3-> 4, then perform the column-out operation in sequence. The column-out sequence should be consistent with the column-in sequence. The following is the code. If you don't talk about it, you will know it after reading it:
/* * filename: main.c * author:zhm * date: 2013-01-06 */#include <string.h>#include <stdlib.h>#include <stdio.h>#include "list.h"#include "queue.h"/* destroy */void destroy(void *data){ printf("in destroy\n"); free(data); return;}/* main */int main(int argc, char **argv){ Queue queue; int *int_ptr = NULL; int ret; int i; //init queue queue_init(&queue, destroy); //enqueue; for(i = 0; i < 5; i++ ) { int_ptr = NULL; int_ptr = (int *)malloc(sizeof(int)); if( int_ptr == NULL ) return -1; printf("enqueue: data = %d\n",i); *int_ptr = i; ret = queue_enqueue(&queue, (void *)int_ptr); if( ret != 0 ) return -1; } printf("the size of the queue: %d\n", queue_size(&queue)); for(i = queue_size(&queue); i > 0 ; i-- ) { int_ptr = NULL; ret = queue_dequeue(&queue, (void **)&int_ptr); if( ret != 0 ) return -1; printf("i = %d, dequque data = %d\n",i, *int_ptr); } printf("after dequeue the size of the queue: %d\n",queue_size(&queue)); return 0;}
The preceding code is compiled and executed. The result is as follows:
The results show that the execution sequence of the queue columns is consistent with that of the columns. Finished !~~~