Advertisement
Guest User

Untitled

a guest
Aug 1st, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. /**
  2.          queueHeader.h header file
  3. */
  4.  
  5. /*
  6.  * the queue structure
  7.  */
  8. typedef struct queue {
  9.         int *que;               /* the actual array of queue elements */
  10.         int head;               /* head index in que of the queue */
  11.         int count;              /* number of elements in queue */
  12.         int size;               /* max number of elements in queue */
  13. } QUEUE;
  14.  
  15. /*
  16.  * the library functions
  17.  */
  18. void queueManage(QUEUE **, int, int);   /* create or delete a queue */
  19. void addToQueue(QUEUE *, int);  /* add to queue */
  20. void removeFromQueue(QUEUE *, int *);   /* remove from queue */
  21.  
  22.  
  23. /**
  24.          queueManager.c source
  25. */
  26.  
  27. #include <stdlib.h>
  28. #include "queueHeader.h"
  29.  
  30. /*
  31.  * create or delete a queue
  32.  *
  33.  * PARAMETERS:  QUEUE **qptr    space for, or pointer to, queue
  34.  *              int flag        1 for create, 0 for delete
  35.  *              int size        max elements in queue
  36.  */
  37. void queueManage(QUEUE **qptr, int flag, int size)
  38. {
  39.         if (flag){
  40.                 /* allocate a new queue */
  41.                 *qptr = malloc(sizeof(QUEUE));
  42.                 (*qptr)->head = (*qptr)->count = 0;
  43.                 (*qptr)->que = malloc(size * sizeof(int));
  44.                 (*qptr)->size = size;
  45.         }
  46.         else{
  47.                 /* delete the current queue */
  48.                 (void) free((*qptr)->que);
  49.                 (void) free(*qptr);
  50.         }
  51. }
  52.  
  53. /*
  54.  * add an element to an existing queue
  55.  *
  56.  * PARAMETERS:  QUEUE *qptr     pointer for queue involved
  57.  *              int n           element to be appended
  58.  */
  59. void addToQueue(QUEUE *qptr, int n)
  60. {
  61.         /* add new element to tail of queue */
  62.         qptr->que[(qptr->head + qptr->count) % qptr->size] = n;
  63.         qptr->count++;
  64.  
  65. }
  66.  
  67. /*
  68.  * take an element off the front of an existing queue
  69.  *
  70.  * PARAMETERS:  QUEUE *qptr     pointer for queue involved
  71.  *              int *n          storage for the return element
  72.  */
  73. void removeFromQueue(QUEUE *qptr, int *n)
  74. {
  75.         /* return the element at the head of the queue */
  76.         *n = qptr->que[qptr->head++];
  77.         qptr->count--;
  78.         qptr->head %= qptr->size;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement