Advertisement
andreibalu

Untitled

Nov 30th, 2020
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define nmax 10
  5.  
  6. //Structuri
  7. typedef struct mesaj{
  8. char s[100];
  9. int id,p;
  10. }mesaj;
  11.  
  12. typedef struct coada
  13. {
  14.  mesaj elem[10];
  15.  int citire;
  16.  int scriere;
  17. }coada;
  18.  
  19. typedef struct Nod {
  20.   coada c;
  21.   int p;
  22.   struct Nod *urmator;
  23. }Nod;
  24. Nod *inceput;
  25.  
  26. //Functii pentru coada circulara
  27. int vid(coada c)
  28. {
  29.   if (c.citire == (c.scriere + 1) % nmax) return 1;
  30.   return 0;
  31. }
  32. int plina(coada c)
  33. {
  34.    if (c.citire == (c.scriere + 2) % nmax)
  35.   return 1;
  36.  else return 0;
  37. }
  38. coada adauga(coada c, mesaj s)
  39. {
  40.    if (!plina(c))
  41.    {
  42.       c.scriere = (c.scriere + 1) % nmax;
  43.       c.elem[c.scriere]=s;
  44.    }
  45.    return c;
  46. }
  47. mesaj scoate(coada *c)
  48. {
  49.    mesaj element;
  50.    if (!vid(*c))
  51.   {
  52.      element = c->elem[c->citire];
  53.      c->citire = ((c->citire) + 1) % nmax;
  54.  
  55.   }
  56.  return element;
  57. }
  58.  
  59.  
  60. // adaugare categorie
  61. void adaugC(int pp){
  62. Nod *q,*x;
  63. q=(Nod*)malloc(sizeof(Nod));
  64. q->p=pp;
  65. q->c.citire=0;
  66. q->c.scriere=9;
  67. x=inceput;
  68. if(x==NULL)
  69. {
  70.     q->urmator=NULL;
  71.     inceput=q;
  72. }
  73. else
  74. {
  75. while(x->urmator!=NULL&&(x->urmator->p)>pp)
  76. {
  77.     x=x->urmator;
  78. }
  79. q->urmator=x->urmator;
  80. x->urmator=q;
  81. }
  82. }
  83. //adaugare mesaj
  84. void adaugM(mesaj k)
  85. {
  86.     Nod *x;
  87.     if(inceput!=NULL){
  88.     x=inceput;
  89.     while((x->p)!=k.p&&x!=NULL)
  90.         x=x->urmator;
  91.     if(x==NULL)
  92.         adaugC(k.p);
  93.     x=inceput;
  94.     while((x->p)!=k.p&&x!=NULL)
  95.         x=x->urmator;
  96.     x->c=adauga(x->c,k);
  97.     }
  98.     else
  99.     {
  100.         adaugC(k.p);
  101.         inceput->c=adauga(inceput->c,k);
  102.     }
  103. }
  104. //deservire mesaj
  105. mesaj desM()
  106. {
  107.     Nod *x;
  108.     mesaj k;
  109.     if(inceput!=NULL)
  110.         {
  111.     x=inceput;
  112.     while(vid(x->c))
  113.         x=x->urmator;
  114.     k=scoate(&(x->c));
  115.     return k;
  116.     }
  117. }
  118.  
  119. int main()
  120. {
  121. mesaj m,n;
  122. strcpy(m.s,"abc");
  123.  
  124. m.id=1;
  125. m.p=1;
  126. adaugC(1);
  127. adaugC(2);
  128. adaugM(m);
  129. printf("\n\n%s\n\n",inceput->c.elem[0].s);
  130. n=desM();
  131. printf("%s",n.s);
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement