home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / sna / aremote / queue.c < prev    next >
C/C++ Source or Header  |  1997-04-09  |  1KB  |  63 lines

  1. #include <windows.h>
  2. #include "queue.h"
  3.  
  4. qnode *addtoq(queue *q, void *p) {
  5.     qnode *t;
  6.  
  7.     t = (qnode *) LocalAlloc(0, sizeof(qnode));
  8.     if (t == NULL) return NULL;
  9.     t->ptr = p;
  10.     t->next = NULL;
  11.     if (q->head == NULL) {
  12.         t->prev = NULL;
  13.         q->head = t;
  14.         q->tail = t;
  15.     } else {
  16.         t->prev = q->tail;
  17.         q->tail->next = t;
  18.         q->tail = t;
  19.     }
  20.  
  21.     return t;
  22. }
  23.  
  24. void *removeheadfromq(queue *q) {
  25.     qnode *t;
  26.     void *p;
  27.  
  28.     if (q->head == NULL) return NULL;
  29.  
  30.     t = q->head;
  31.     q->head = t->next;
  32.     if (q->head == NULL) q->tail = NULL;
  33.     else q->head->prev = NULL;
  34.     p = t->ptr;
  35.     LocalFree(t);
  36.  
  37.     return p;
  38. }
  39.  
  40. void *removeqnodefromq(queue *q, qnode *t) {
  41.     void *p;
  42.  
  43.     if (q->head == t) q->head = t->next;
  44.     if (q->tail == t) q->tail = t->prev;
  45.     if (t->next != NULL) t->next->prev = t->prev;
  46.     if (t->prev != NULL) t->prev->next = t->next;
  47.     p = t->ptr;
  48.     LocalFree(t);
  49.  
  50.     return p;
  51. }
  52.  
  53. queue *newq(void) {    
  54.     queue *q;
  55.  
  56.     q = (queue *) LocalAlloc(0, sizeof(queue));
  57.     if (q == NULL) return NULL;
  58.     q->head = NULL;
  59.     q->tail = NULL;
  60.  
  61.     return q;
  62. }
  63.