📄 queue.c
字号:
/* Demo of simplest queue data structure. * * Written by Cyril Hu (cyrilhu@gmail.com), public domain. */#include<my.h>#define MAX 10typedef struct q_node { int data; struct q_node *next;} Q_NODE, *q_node_ptr;typedef struct queue { q_node_ptr front; q_node_ptr rear;} QUEUE;void init_queue(QUEUE *p){ p->front = p->rear = (q_node_ptr)mem('m', sizeof(Q_NODE)); p->front->next = NULL;}void en_queue(QUEUE *p, const q_node_ptr q){ q_node_ptr t = (q_node_ptr)mem('m', sizeof(Q_NODE)); t->data = q->data; t->next = NULL; p->rear->next = t; p->rear = t;}int queue_empty(QUEUE *p){ return p->front != p->rear ? 0 : 1;}void de_queue(QUEUE *p, q_node_ptr q){ q_node_ptr t; if(! queue_empty(p)) { if( (t = p->front->next) == p->rear) p->rear = p->front; p->front->next = t->next; q->data = t->data; free(t); }}void destroy_queue(QUEUE *p){ while( p->front != NULL) { p->rear = p->front->next; free(p->front); p->front = p->rear; }}int main(){ int i; QUEUE a, *p = &a; Q_NODE b[MAX], c[MAX]; init_queue(p); for(i=0; i<MAX; i++) { b[i].data = i; en_queue(p, &b[i]); } for(i=0; i<MAX; i++) { de_queue(p, &c[i]); printf("%d ", c[i].data); } destroy_queue(p); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -