Advertisement
Guest User

PI Pratica 30-03

a guest
Mar 30th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // [ , , , , ] MAX
  4. // [1, , , , ]
  5.  
  6. // [1,4,17, , ] front = 0, length = 3
  7. // dequeue -> tirei o 1 da fila
  8.  
  9. // [1,4,17, , ] front = 1, length = 2
  10.  
  11. // dequeue -> tirei o 4 da fila
  12. // [1,4,17, , ] front = 2, length = 1
  13.  
  14. // inseri 23 e 55
  15.  
  16. // [1,4,17,23,55] front = 2, length = 3
  17.  
  18. // inserir 47
  19. // [47,4,17,23,55] front = 2, length = 4
  20.  
  21. //inserir 39
  22. // [47,39,17,23,55] front = 2, length = 5
  23.  
  24.  
  25. //[38,4,17,23,55] front = 4, length = 2;
  26.  
  27.  
  28.  
  29. #define MAX 5
  30.  
  31. struct staticqueue{
  32. int front;
  33. int length;
  34. int values[MAX];
  35. };
  36.  
  37. typedef struct staticqueue *SQueue;
  38.  
  39. void SinitSQueue(SQueue q){
  40. q->front = 0;
  41. q->length = 0;
  42. }
  43.  
  44. int Senqueue(SQueue q, int x){
  45. if(q->length == MAX) return -1;
  46.  
  47. q->values[(q->length + q->front)%MAX] = x;
  48. q->length++;
  49.  
  50. return 0;
  51. }
  52.  
  53. int Sdequeue(SQueue q, int *x){
  54. if(q->length == 0) return -1;
  55.  
  56. *x = q->values[q->front];
  57.  
  58. q->length--;
  59. q->front = (q->front+1)%MAX;
  60. return 0;
  61. }
  62.  
  63. int Sfront(SQueue, int *x){
  64. if(q->length == 0) return -1;
  65.  
  66. *x = q->values[q->front];
  67. return 0;
  68. }
  69.  
  70. int main() {
  71. struct staticqueue s1;
  72. SQueue s = &s1;
  73. int x;
  74.  
  75. SinitSQueue(s);
  76. Senqueue(s,29);
  77. Senqueue(s,67);
  78. Senqueue(s,7);
  79. Senqueue(s,35);
  80. Senqueue(s,9);
  81.  
  82. Sdequeue(s,&x);
  83. Sdequeue(s,&x);
  84. Sdequeue(s,&x);
  85. Sdequeue(s,&x);
  86.  
  87. Senqueue(s,50);
  88. Sdequeue(s,&x);
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement