1 /* struct::tree - critcl - support - stack/queue of nodes. 2 * definitions. 3 */ 4 5 #include "tcl.h" 6 #include <util.h> 7 8 static NL* nlq_newitem (void* n); 9 10 11 /* Initialize queue data structure. 12 */ 13 14 void nlq_init(NLQ * q)15nlq_init (NLQ* q) 16 { 17 q->start = q->end = NULL; 18 } 19 20 /* Add item to end of the list 21 */ 22 23 void nlq_append(NLQ * q,void * n)24nlq_append (NLQ* q, void* n) 25 { 26 NL* qi = nlq_newitem (n); 27 28 if (!q->end) { 29 q->start = q->end = qi; 30 } else { 31 q->end->next = qi; 32 q->end = qi; 33 } 34 } 35 36 /* Add item to the front of the list 37 */ 38 39 void nlq_push(NLQ * q,void * n)40nlq_push (NLQ* q, void* n) 41 { 42 NL* qi = nlq_newitem (n); 43 44 if (!q->end) { 45 q->start = q->end = qi; 46 } else { 47 qi->next = q->start; 48 q->start = qi; 49 } 50 } 51 52 /* Return item at front of the list. 53 */ 54 55 void* nlq_pop(NLQ * q)56nlq_pop (NLQ* q) 57 { 58 NL* qi = NULL; 59 void* n = NULL; 60 61 if (!q->start) { 62 return NULL; 63 } 64 65 qi = q->start; 66 n = qi->n; 67 68 q->start = qi->next; 69 if (q->end == qi) { 70 q->end = NULL; 71 } 72 73 ckfree ((char*) qi); 74 return n; 75 } 76 77 /* Delete all items in the list. 78 */ 79 80 void nlq_clear(NLQ * q)81nlq_clear (NLQ* q) 82 { 83 NL* next; 84 NL* qi = q->start; 85 86 while (qi) { 87 next = qi->next; 88 ckfree ((char*) qi); 89 qi = next; 90 } 91 q->start = NULL; 92 q->end = NULL; 93 } 94 95 /* INTERNAL - Create new item to put into the list. 96 */ 97 98 static NL* nlq_newitem(void * n)99nlq_newitem (void* n) 100 { 101 NL* qi = (NL*) ckalloc (sizeof (NL)); 102 103 qi->n = n; 104 qi->next = NULL; 105 106 return qi; 107 } 108 109 /* 110 * Local Variables: 111 * mode: c 112 * c-basic-offset: 4 113 * fill-column: 78 114 * End: 115 */ 116