LinkQueue by c

 1 #pragma once
 2 
 3 typedef char *Type;
 4 typedef enum {false, true} bool;
 5 
 6 typedef struct LQNode {
 7     Type data;
 8     struct LQNode *next;
 9 } LQNode, *LQNodePtr;
10 
11 typedef struct LQueue {
12     LQNodePtr front;
13     LQNodePtr rear;
14 } LQueue, *LQueuePtr;
15 
16 void InitLQueue(LQueuePtr p);
17 bool EnLQueue(LQueuePtr p, Type d);
18 bool DeLQueue(LQueuePtr p, Type *d);
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "LinkQueue.h"
 4 
 5 void InitLQueue(LQueuePtr p)
 6 {
 7    p->front = NULL;
 8    p->rear = NULL;
 9 }
10 
11 static bool IsEmpty(LQueuePtr p)
12 {
13     return NULL == p->front;
14 }
15 
16 bool EnLQueue(LQueuePtr p, Type d)
17 {
18     LQNodePtr pNode;
19 
20     pNode = (LQNodePtr)malloc(sizeof(*pNode));
21     if (NULL == pNode)
22     return false;
23 
24     pNode->data = d;
25 
26     if (p->front == NULL)
27     p->front = pNode;
28     else
29     p->rear->next = pNode;
30 
31     p->rear = pNode;
32 
33     return true;
34 }
35 
36 bool DeLQueue(LQueuePtr p, Type *d)
37 {
38     LQNodePtr pNode;
39 
40     if (IsEmpty(p))
41     return false;
42 
43     pNode = p->front;
44     p->front = pNode->next;
45     *d = pNode->data;
46     free(pNode);
47 
48     return true;
49 }
 1 #include <stdio.h>
 2 #include "LinkQueue.h"
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int i;
 7     LQueue p;
 8     Type d;
 9     Type data[] = {"Create", "LinkQueue", "Success", "!"};
10 
11     InitLQueue(&p);
12 
13     for (i = 0; i < 4; i++)
14     EnLQueue(&p, data[i]);
15 
16     while(DeLQueue(&p, &d))
17     printf("%s ", d);
18 
19     putchar('\n');
20 
21     return 0;
22 }

posted on 2012-09-02 16:10  robin.he  阅读(223)  评论(0)    收藏  举报

导航