Advertisement
Guest User

Code

a guest
Jul 30th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "queueHeader.h"
  5.  
  6. int main() {
  7. QUEUE *pointerToQ;
  8. queueManage(&pointerToQ, 1, 10);
  9. int i = 0;
  10. for(i = 0; i < 10; i++){
  11. addToQueue(pointerToQ, i);
  12. }
  13. printf("Pointer is pointing to %d", pointerToQ -> count);
  14.  
  15. int *val = 0;
  16. removeFromQueue(pointerToQ, val);
  17. printf("The value of val is %d", val);
  18.  
  19. }
  20.  
  21.  
  22. /*
  23. * line Manager
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "queueHeader.h"
  28.  
  29. /*
  30. * create or delete a queue
  31. *
  32. * PARAMETERS: QUEUE **qptr space for, or pointer to, queue
  33. * int flag 1 for create, 0 for delete
  34. * int size max elements in queue
  35. */
  36. void queueManage(QUEUE **qptr, int flag, int size)
  37. {
  38. if (flag){
  39. /* allocate a new queue */
  40. *qptr = malloc(sizeof(QUEUE));
  41. (*qptr)->head = (*qptr)->count = 0;
  42. (*qptr)->que = malloc(size * sizeof(int));
  43. (*qptr)->size = size;
  44. }
  45. else{
  46. /* delete the current queue */
  47. (void) free((*qptr)->que);
  48. (void) free(*qptr);
  49. }
  50. }
  51.  
  52. /*
  53. * add an element to an existing queue
  54. *
  55. * PARAMETERS: QUEUE *qptr pointer for queue involved
  56. * int n element to be appended
  57. */
  58. void addToQueue(QUEUE *qptr, int n)
  59. {
  60. /* add new element to tail of queue */
  61. qptr->que[(qptr->head + qptr->count) % qptr->size] = n;
  62. qptr->count++;
  63.  
  64. }
  65.  
  66. /*
  67. * take an element off the front of an existing queue
  68. *
  69. * PARAMETERS: QUEUE *qptr pointer for queue involved
  70. * int *n storage for the return element
  71. */
  72. void removeFromQueue(QUEUE *qptr, int *n)
  73. {
  74. /* return the element at the head of the queue */
  75. *n = qptr->que[qptr->head++];
  76. qptr->count--;
  77. qptr->head %= qptr->size;
  78. }
  79.  
  80. /*
  81. * line Manager Header file
  82. */
  83.  
  84.  
  85. /*
  86. * the queue structure
  87. */
  88. typedef struct queue {
  89. int *que; /* the actual array of queue elements */
  90. int head; /* head index in que of the queue */
  91. int count; /* number of elements in queue */
  92. int size; /* max number of elements in queue */
  93. } QUEUE;
  94.  
  95. /*
  96. * the library functions
  97. */
  98. void queueManage(QUEUE **, int, int); /* create or delete a queue */
  99. void addToQueue(QUEUE *, int); /* add to queue */
  100. void removeFromQueue(QUEUE *, int *); /* remove from queue */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement