Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include "queue.h"
  2.  
  3. Queue* createQueue(int capacity)
  4. {
  5.     Queue* queue=(Queue*)malloc(sizeof(Queue));
  6.     queue->capacity=capacity;
  7.     queue->head=0;
  8.     queue->tail=0;
  9.     queue->data=(char**)malloc(queue->capacity*(sizeof(char*)));
  10.     for(int i=0;i<queue->capacity;i++)
  11.         queue->data[i]=(char*)malloc(sizeof(char)*640);
  12.     return queue;
  13. }
  14. Queue* createQueueClients(int capacity)
  15. {
  16.     Queue* queue=(Queue*)calloc(1, sizeof(Queue));
  17.     queue->capacity=capacity;
  18.     queue->head=0;
  19.     queue->tail=0;
  20.     queue->CurrentSize=0;
  21.     queue->MaxElement=capacity;
  22.     queue->Tab = (int*)calloc(capacity, sizeof(int));
  23.     return queue;
  24. }
  25.  
  26. unsigned int size(Queue* queue)
  27. {
  28.     return queue->head-queue->tail;
  29. }
  30. unsigned int capacity(Queue * queue)
  31. {
  32.     return queue->capacity;
  33. }
  34. void push(Queue* queue,char* data)
  35. {
  36.     unsigned int index = queue->head % queue->capacity;
  37.  
  38.     if(size(queue) == capacity(queue))
  39.         queue->tail++;
  40.     queue->head++;
  41.  
  42.     queue->data[index]=data;
  43. }
  44. char *pop(Queue* queue)
  45. {
  46.     unsigned int index = queue->tail % queue->capacity;
  47.     queue->tail++;
  48.     return queue->data[index];
  49. }
  50. void pushClients(Queue* queue, int x)
  51. {
  52.     queue->Tab[queue->tail] = x;
  53.     queue->tail = (queue->tail+1) % queue->MaxElement;
  54.  
  55.     queue->CurrentSize++;
  56. }
  57. int popClients(Queue* queue)
  58. {
  59.     int x = queue->Tab[queue->head];
  60.     queue->Tab[queue->head] = '\0';
  61.     queue->head = (queue->head+1) % queue->MaxElement;
  62.     queue->CurrentSize--;
  63.  
  64.     return x;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement