fork download
  1. /*
  2.  • *Создать очередь на основе двусвязного списка.
  3.  • **Создать стек на основе двусвязного списка.
  4.  */
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <stdlib.h>
  10.  
  11. #define T int
  12. #define QMAX 100
  13.  
  14.  
  15. typedef struct Node {
  16. T payload;
  17. struct Node *prev;
  18. struct Node *next;
  19.  
  20. } Node;
  21.  
  22. Node *newNode() {
  23. Node *n = (Node*)malloc(sizeof(Node));
  24. if (n == NULL) {
  25. puts("Out of memory!");
  26. return n;
  27. }
  28. n->prev = NULL;
  29. n->next = NULL;
  30.  
  31. return n;
  32. }
  33.  
  34.  
  35. typedef struct Queue {
  36. Node *recent;
  37. Node *oldest;
  38. int size;
  39.  
  40. } Queue;
  41.  
  42. Queue newQueue() {
  43. Queue *q = (Queue*)malloc(sizeof(Queue));
  44. if (q == NULL) {
  45. puts("Out of memory!");
  46. return *q;
  47. }
  48. q->recent = NULL;
  49. q->oldest = NULL;
  50. q->size = 0;
  51. return *q;
  52. }
  53.  
  54. void qPush(T val, Queue *q){
  55. if (q->size >= QMAX){
  56. puts("Queue is full! Do some cleanup first!");
  57. return;
  58. }
  59. Node *tmp = newNode();
  60. tmp->payload = val;
  61.  
  62. if (q->oldest == NULL)
  63. q->oldest = tmp;
  64. else if (q->recent == NULL) {
  65. tmp->prev = q->oldest;
  66. q->oldest->next = tmp;
  67. q->recent = tmp;
  68. } else {
  69. tmp->prev = q->recent;
  70. q->recent->next = tmp;
  71. q->recent = tmp;
  72.  
  73. }
  74. q->size++;
  75. }
  76. T qPop(Queue *q){
  77. if (q->oldest == NULL) {
  78. puts("Queue is empty");
  79. return 0;
  80. }
  81.  
  82. Node *tmp = q->oldest;
  83. T val = tmp->payload;
  84. if (&q->oldest == &q->recent || q->recent == NULL) {
  85. free(q->recent);
  86. free(q->oldest);
  87. } else {
  88. q->oldest = q->oldest->next;
  89. }
  90.  
  91. q->size--;
  92. free(tmp);
  93. return val;
  94. };
  95.  
  96. int bilen(long n) { //calculates length of a binary representation of a dec number
  97. return (int)(log(n)/0.693147 + 1); //0.693147 == log(2);
  98. }
  99.  
  100. void goida(Node *n) {
  101. if(!n){
  102. puts("It's empty!");
  103. return;
  104. }
  105. printf("Paylod is %2d! GOIDA!\n", n->payload);
  106. if (n->next != NULL) goida(n->next);
  107.  
  108. }
  109.  
  110.  
  111. void l7(){
  112. printf("binary representation of 512 have length of %d\n", bilen(512));
  113. printf("binary representation of 511 have length of %d\n", bilen(511));
  114. Queue q = newQueue();
  115. int i =0;
  116. for(i = 0; i < 105; i++) qPush(i, &q);
  117. Queue q2 = newQueue();
  118. goida(q2.oldest);
  119. goida(q.oldest);
  120. for (i = 0; i < 101; i++ ) printf("Goida! %d!\n", qPop(&q));
  121.  
  122. goida(q.oldest);
  123.  
  124. printf("size of q: %d\n", q.size);
  125. printf("size of q: %d\n", q.size);
  126.  
  127. }
  128.  
  129. int main(int argc, const char * argv[]) {
  130.  
  131. l7();
  132.  
  133. puts("");
  134. return 0;
  135. }
  136.  
Success #stdin #stdout 0s 10304KB
stdin
Standard input is empty
stdout
binary representation of 512 have length of 10
binary representation of 511 have length of 9
Queue is full! Do some cleanup first!
Queue is full! Do some cleanup first!
Queue is full! Do some cleanup first!
Queue is full! Do some cleanup first!
Queue is full! Do some cleanup first!
It's empty!
Paylod is  0! GOIDA!
Paylod is  1! GOIDA!
Paylod is  2! GOIDA!
Paylod is  3! GOIDA!
Paylod is  4! GOIDA!
Paylod is  5! GOIDA!
Paylod is  6! GOIDA!
Paylod is  7! GOIDA!
Paylod is  8! GOIDA!
Paylod is  9! GOIDA!
Paylod is 10! GOIDA!
Paylod is 11! GOIDA!
Paylod is 12! GOIDA!
Paylod is 13! GOIDA!
Paylod is 14! GOIDA!
Paylod is 15! GOIDA!
Paylod is 16! GOIDA!
Paylod is 17! GOIDA!
Paylod is 18! GOIDA!
Paylod is 19! GOIDA!
Paylod is 20! GOIDA!
Paylod is 21! GOIDA!
Paylod is 22! GOIDA!
Paylod is 23! GOIDA!
Paylod is 24! GOIDA!
Paylod is 25! GOIDA!
Paylod is 26! GOIDA!
Paylod is 27! GOIDA!
Paylod is 28! GOIDA!
Paylod is 29! GOIDA!
Paylod is 30! GOIDA!
Paylod is 31! GOIDA!
Paylod is 32! GOIDA!
Paylod is 33! GOIDA!
Paylod is 34! GOIDA!
Paylod is 35! GOIDA!
Paylod is 36! GOIDA!
Paylod is 37! GOIDA!
Paylod is 38! GOIDA!
Paylod is 39! GOIDA!
Paylod is 40! GOIDA!
Paylod is 41! GOIDA!
Paylod is 42! GOIDA!
Paylod is 43! GOIDA!
Paylod is 44! GOIDA!
Paylod is 45! GOIDA!
Paylod is 46! GOIDA!
Paylod is 47! GOIDA!
Paylod is 48! GOIDA!
Paylod is 49! GOIDA!
Paylod is 50! GOIDA!
Paylod is 51! GOIDA!
Paylod is 52! GOIDA!
Paylod is 53! GOIDA!
Paylod is 54! GOIDA!
Paylod is 55! GOIDA!
Paylod is 56! GOIDA!
Paylod is 57! GOIDA!
Paylod is 58! GOIDA!
Paylod is 59! GOIDA!
Paylod is 60! GOIDA!
Paylod is 61! GOIDA!
Paylod is 62! GOIDA!
Paylod is 63! GOIDA!
Paylod is 64! GOIDA!
Paylod is 65! GOIDA!
Paylod is 66! GOIDA!
Paylod is 67! GOIDA!
Paylod is 68! GOIDA!
Paylod is 69! GOIDA!
Paylod is 70! GOIDA!
Paylod is 71! GOIDA!
Paylod is 72! GOIDA!
Paylod is 73! GOIDA!
Paylod is 74! GOIDA!
Paylod is 75! GOIDA!
Paylod is 76! GOIDA!
Paylod is 77! GOIDA!
Paylod is 78! GOIDA!
Paylod is 79! GOIDA!
Paylod is 80! GOIDA!
Paylod is 81! GOIDA!
Paylod is 82! GOIDA!
Paylod is 83! GOIDA!
Paylod is 84! GOIDA!
Paylod is 85! GOIDA!
Paylod is 86! GOIDA!
Paylod is 87! GOIDA!
Paylod is 88! GOIDA!
Paylod is 89! GOIDA!
Paylod is 90! GOIDA!
Paylod is 91! GOIDA!
Paylod is 92! GOIDA!
Paylod is 93! GOIDA!
Paylod is 94! GOIDA!
Paylod is 95! GOIDA!
Paylod is 96! GOIDA!
Paylod is 97! GOIDA!
Paylod is 98! GOIDA!
Paylod is 99! GOIDA!
Goida! 0!
Goida! 1!
Goida! 2!
Goida! 3!
Goida! 4!
Goida! 5!
Goida! 6!
Goida! 7!
Goida! 8!
Goida! 9!
Goida! 10!
Goida! 11!
Goida! 12!
Goida! 13!
Goida! 14!
Goida! 15!
Goida! 16!
Goida! 17!
Goida! 18!
Goida! 19!
Goida! 20!
Goida! 21!
Goida! 22!
Goida! 23!
Goida! 24!
Goida! 25!
Goida! 26!
Goida! 27!
Goida! 28!
Goida! 29!
Goida! 30!
Goida! 31!
Goida! 32!
Goida! 33!
Goida! 34!
Goida! 35!
Goida! 36!
Goida! 37!
Goida! 38!
Goida! 39!
Goida! 40!
Goida! 41!
Goida! 42!
Goida! 43!
Goida! 44!
Goida! 45!
Goida! 46!
Goida! 47!
Goida! 48!
Goida! 49!
Goida! 50!
Goida! 51!
Goida! 52!
Goida! 53!
Goida! 54!
Goida! 55!
Goida! 56!
Goida! 57!
Goida! 58!
Goida! 59!
Goida! 60!
Goida! 61!
Goida! 62!
Goida! 63!
Goida! 64!
Goida! 65!
Goida! 66!
Goida! 67!
Goida! 68!
Goida! 69!
Goida! 70!
Goida! 71!
Goida! 72!
Goida! 73!
Goida! 74!
Goida! 75!
Goida! 76!
Goida! 77!
Goida! 78!
Goida! 79!
Goida! 80!
Goida! 81!
Goida! 82!
Goida! 83!
Goida! 84!
Goida! 85!
Goida! 86!
Goida! 87!
Goida! 88!
Goida! 89!
Goida! 90!
Goida! 91!
Goida! 92!
Goida! 93!
Goida! 94!
Goida! 95!
Goida! 96!
Goida! 97!
Goida! 98!
Goida! 99!
Queue is empty
Goida! 0!
It's empty!
size of q: 0
size of q: 0