标签:
#include <stdio.h> #include <stdlib.h> typedef int queue_element_t; typedef struct queue_node_s { queue_element_t element; struct queue_node_s *restp; }queue_node_t; /*Queue front & rear pointer*/ typedef struct { queue_node_t *frontp, *rearp; int size; }queue_t; void add_to_q(queue_t *qp, queue_element_t ele) { /*Keep queue front pointer*/ if(qp->size == 0) { qp->rearp = (queue_node_t*)malloc(sizeof(queue_node_t)); qp->frontp = qp->rearp; } /*Renew queue rear pointer*/ else { qp->rearp->restp = (queue_node_t*)malloc(sizeof(queue_node_t)); qp->rearp = qp->rearp->restp; } qp->rearp->element = ele; qp->rearp->restp = NULL; ++(qp->size); } queue_element_t remove_from_q(queue_t *qp) { queue_node_t *to_freep; queue_element_t ans; to_freep = qp->frontp; ans = to_freep->element; qp->frontp = to_freep->restp; free(to_freep); --(qp->size); if(qp->size == 0) qp->rearp = NULL; return ans; } int main(void) { queue_t queue = {NULL, NULL, 0}; add_to_q(&queue, 100); add_to_q(&queue, 200); add_to_q(&queue, 300); add_to_q(&queue, 400); int i; int sum = queue.size; for(i = 0; i < sum; i++) printf("%d\n", remove_from_q(&queue)); }
标签:
原文地址:http://www.cnblogs.com/xbon/p/4883048.html