mihainan

Nan Mihai - Structuri de date

Apr 1st, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.37 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. /** Structura implementeaza o coada de int folosind un tablou alocat dinamic.
  6.  
  7.  * Pentru a folosi mai bine memoria, zona de date se considera circulara (vezi breviar
  8.  
  9.  * laborator).
  10.  
  11.  */
  12.  
  13.  
  14.  
  15. struct queue_struct {
  16.  
  17.     /** Un pointer catre zona de date */
  18.  
  19.     int* data;
  20.  
  21.     /** Capacitatea zonei de date */
  22.  
  23.     int capacity;
  24.  
  25.     /** Pozitia primului element din coada */
  26.  
  27.     int front;
  28.  
  29.     /** Numarul de elemente din coada */
  30.  
  31.     int size;
  32.  
  33. };
  34.  
  35.  
  36.  
  37. typedef struct queue_struct* Queue;
  38.  
  39. #define QUEUE_INITIAL_CAPACITY 10
  40.  
  41.  
  42.  
  43. /**1.  Returneaza un pointer la o coada nou alocata*/
  44.  
  45. Queue newQueue()
  46.  
  47. {
  48.  
  49.     Queue nou;
  50.  
  51.     nou = (Queue) malloc(sizeof(struct queue_struct));
  52.  
  53.     nou->data = (int *) malloc(QUEUE_INITIAL_CAPACITY*sizeof(int));
  54.  
  55.     nou->front = 0;
  56.  
  57.     nou->size = 0;
  58.  
  59.     nou->capacity = QUEUE_INITIAL_CAPACITY;
  60.  
  61.     return nou;
  62.  
  63. }
  64.  
  65.  
  66.  
  67. /** 2. Elibereaza intreaga memorie alocata cozii*/
  68.  
  69. Queue deleteQueue(Queue q)
  70.  
  71. {
  72.  
  73.     if(q != NULL)
  74.  
  75.     {
  76.  
  77.         free(q->data);
  78.  
  79.         free(q);
  80.  
  81.     }
  82.  
  83.     return NULL;
  84.  
  85. }
  86.  
  87.  
  88.  
  89. /** 3. Returneaza True daca este goala */
  90.  
  91. int isEmpty(Queue q)
  92.  
  93. {
  94.  
  95.     if(q->size == 0)
  96.  
  97.     {
  98.  
  99.         return 1;
  100.  
  101.     }
  102.  
  103.     else
  104.  
  105.     {
  106.  
  107.         return 0;
  108.  
  109.     }
  110.  
  111. }
  112.  
  113.  
  114.  
  115. /** 4. Returneaza numarul de element din coada */
  116.  
  117. int size(Queue q)
  118.  
  119. {
  120.  
  121.     if(q != NULL)
  122.  
  123.         return q->size;
  124.  
  125.     else
  126.  
  127.         return 0;
  128.  
  129. }
  130.  
  131.  
  132.  
  133. /** 5. Se asigura ca respectiva coada are capacitatea cel putin minCapacity*/
  134.  
  135. Queue ensureCapacity(Queue q, int minCapacity)
  136.  
  137. {
  138.  
  139.         if(q->capacity < minCapacity)
  140.  
  141.         {
  142.  
  143.             q->capacity = minCapacity;
  144.  
  145.             q->data = (int *) realloc(q, minCapacity*sizeof(int));
  146.  
  147.             return q;
  148.  
  149.         }
  150.  
  151.         else
  152.  
  153.             return q;
  154.  
  155. }
  156.  
  157.  
  158.  
  159. /** 6. Adauga un element nou in coada*/
  160.  
  161. Queue enqueue(Queue q, int value)
  162.  
  163. {
  164.  
  165.     if(q->capacity == q->size)
  166.  
  167.         q = ensureCapacity(q, 2*q->capacity);
  168.  
  169.     q->data[(q->front+q->size)%q->capacity] = value;
  170.  
  171.     q->size = q->size + 1;
  172.  
  173.     q->capacity = q->capacity + 1;
  174.  
  175.     return q;
  176.  
  177. }
  178.  
  179.  
  180.  
  181. /** 7. Intoarce valoarea primului element din coada sau -1 daca coada este goala (Ar trebui sa returneze o eroare.
  182.  
  183.  * Cum facem in C??)
  184.  
  185.  * */
  186.  
  187. int front(Queue q)
  188.  
  189. {
  190.  
  191.     if(q->size == 0)
  192.  
  193.         return -1;
  194.  
  195.     else
  196.  
  197.     {
  198.  
  199.         return q->data[q->front];
  200.  
  201.     }
  202.  
  203. }
  204.  
  205.  
  206.  
  207. /** 8. Scoate din coada primul element si il returneaza*/
  208.  
  209. int dequeue(Queue q)
  210.  
  211. {
  212.  
  213.     int primul;
  214.  
  215.     if(q->size == 0)
  216.  
  217.     {
  218.  
  219.         return -1;
  220.  
  221.     }
  222.  
  223.     else
  224.  
  225.     {
  226.  
  227.         primul = front(q);
  228.  
  229.         q->front = (q->front+1)%q->capacity;
  230.  
  231.         q->size = q->size-1;
  232.  
  233.         return primul;
  234.  
  235.     }
  236.  
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /** Adauga intervalul [start, end) */
  244.  
  245. void addSome(Queue q, int start, int end) {
  246.  
  247.     int i;
  248.  
  249.     for (i = start; i < end; ++i) {
  250.  
  251.         enqueue(q, i);
  252.  
  253.     }
  254.  
  255. }
  256.  
  257.  
  258.  
  259. /** Testeaza ca poate extrage [start, end) din coada*/
  260.  
  261. int testDequeue(Queue q, int start, int end) {
  262.  
  263.     int ok = 1;
  264.  
  265.     int i;
  266.  
  267.     for (i = start; i < end; ++i) {
  268.  
  269.         int got = dequeue(q);
  270.  
  271.         if (got != i && ok) {
  272.  
  273.             ok = 0;
  274.  
  275.             printf("Dequeue, expected %5d, got %5d\n", i, got);
  276.  
  277.         }
  278.  
  279.     }
  280.  
  281.     if (!ok) {
  282.  
  283.         printf("Dequeue FAILED!\n");
  284.  
  285.     }
  286.  
  287.     return ok;
  288.  
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295. int main(int argc, char** argv) {
  296.  
  297.     Queue q;
  298.  
  299.     int i, start = 0, end = 100;
  300.  
  301.     int ok = 1;
  302.  
  303.     //Test 1 - Empty list:
  304.  
  305.     q = newQueue();
  306.  
  307.     printf("front on empty,   expected: %5d got: %5d\n", -1, front(q));
  308.  
  309.     printf("dequeue on empty, expected: %5d got: %5d\n", -1, dequeue(q));
  310.  
  311.     printf("isEmpty on empty, expected: %5d got: %5d\n",  1, isEmpty(q));
  312.  
  313.     printf("size on empty,    expected: %5d got: %5d\n",  0, size(q));
  314.  
  315.  
  316.  
  317.     deleteQueue(q);
  318.  
  319.  
  320.  
  321.     q = newQueue();
  322.  
  323.  
  324.  
  325.     //Test 2 - enqueue, and dequeue
  326.  
  327.     addSome(q, 0, 5);       //q = [0, 5)
  328.  
  329.     printf("front on some,   expected: %5d got: %5d\n", 0, front(q));
  330.  
  331.     printf("isEmpty on some, expected: %5d got: %5d\n", 0, isEmpty(q));
  332.  
  333.     printf("size on some,    expected: %5d got: %5d\n", 5, size(q));
  334.  
  335.  
  336.  
  337.  
  338.  
  339.     testDequeue(q, 0, 3);   //q = [3, 5)
  340.  
  341.     addSome(q, 5, 12);          //q = [3, 12)
  342.  
  343.  
  344.  
  345.     printf("front on some (1),   expected: %5d got: %5d\n", 3, front(q));
  346.  
  347.     printf("isEmpty on some (1), expected: %5d got: %5d\n", 0, isEmpty(q));
  348.  
  349.     printf("size on some (1),    expected: %5d got: %5d\n", 9, size(q));
  350.  
  351.  
  352.  
  353.  
  354.  
  355.     addSome(q, 12, 200);        // q = [3, 200)
  356.  
  357.     testDequeue(q, 3, 200); // q = empty
  358.  
  359.     printf("isEmpty on empty, expected: %5d got: %5d\n",  1, isEmpty(q));
  360.  
  361.  
  362.  
  363.     deleteQueue(q);
  364.  
  365.  
  366.  
  367.     return 0;
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment