| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "linkqueue.h"
-
- linkqueue_t pkt_creat_linkqueue(void)
- {
- linkqueue_t q;
- if ((q = (linkqueue_t)malloc(sizeof(struct linkqueue))) == NULL) /* malloc queue */
- {
- printf("malloc filed!\n");
- return NULL;
- }
-
- queue_node_t h;
- if ((h = (queue_node_t)malloc(sizeof(struct queue_node))) == NULL) /* malloc head node */
- {
- printf("malloc filed!\n");
- return NULL;
- }
- h->next = NULL;
- q->front = q->rear = h;
- return q;
- }
-
- int pkt_empty_linkqueue(linkqueue_t q)
- {
- return q->front->next == NULL;
- }
-
- int pkt_put_message_linkqueue(linkqueue_t q, message_t m)
- {
- /* linkqueue not have sapce limit */
- queue_node_t n;
- if ((n = (queue_node_t)malloc(sizeof(struct queue_node))) == NULL)
- {
- printf("malloc filed!\n");
- return -1;
- }
-
- n->msg = *m;
- n->next = q->rear->next;
- q->rear->next = n;
- q->rear = n; /* move q->rear to new node, is tail */
-
- // printf("put queue node = %p data = %p\n", q->rear->next, n->msg.data);
- // ptk_show_linkqueue(q);
-
- return 0;
- }
-
- int pkt_take_message_linkqueue(linkqueue_t q, message_t m)
- {
- if (pkt_empty_linkqueue(q))
- {
- printf("linkqueue is empty!\n");
- return -1;
- }
- // printf("take queue node = %p\n", q->front->next);
- *m = q->front->next->msg;
- queue_node_t del = q->front->next;
- q->front->next = del->next; /* q->front is alwasy in head, did not move */
- free(del);
- return 0;
- }
-
- void ptk_show_linkqueue(linkqueue_t q)
- {
- queue_node_t m;
-
- for (m = q->front->next; m != NULL; m = m->next)
- {
- printf("linkqueue = %p\n", m->msg.data);
- }
- }
- #if 0
- int main(int argc, char *argv[])
- {
- linkqueue_t q;
- struct message m;
-
- q = pkt_creat_linkqueue();
-
- for (int i = 1; i <= 10; i++)
- {
- m.data = i;
- printf("put = %d\n", m.data);
- pkt_put_message_linkqueue(q, &m);
- }
-
- ptk_show_linkqueue(q);
-
- while (!pkt_empty_linkqueue(q))
- {
- pkt_take_message_linkqueue(q, &m);
- printf("take = %d\n", m.data);
- }
-
- pkt_take_message_linkqueue(q, &m);
- return 0;
- }
- #endif
|