Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define nmax 10
- //Structuri
- typedef struct mesaj{
- char s[100];
- int id,p;
- }mesaj;
- typedef struct coada
- {
- mesaj elem[10];
- int citire;
- int scriere;
- }coada;
- typedef struct Nod {
- coada c;
- int p;
- struct Nod *urmator;
- }Nod;
- Nod *inceput;
- //Functii pentru coada circulara
- int vid(coada c)
- {
- if (c.citire == (c.scriere + 1) % nmax) return 1;
- return 0;
- }
- int plina(coada c)
- {
- if (c.citire == (c.scriere + 2) % nmax)
- return 1;
- else return 0;
- }
- coada adauga(coada c, mesaj s)
- {
- if (!plina(c))
- {
- c.scriere = (c.scriere + 1) % nmax;
- c.elem[c.scriere]=s;
- }
- return c;
- }
- mesaj scoate(coada *c)
- {
- mesaj element;
- if (!vid(*c))
- {
- element = c->elem[c->citire];
- c->citire = ((c->citire) + 1) % nmax;
- }
- return element;
- }
- // adaugare categorie
- void adaugC(int pp){
- Nod *q,*x;
- q=(Nod*)malloc(sizeof(Nod));
- q->p=pp;
- q->c.citire=0;
- q->c.scriere=9;
- x=inceput;
- if(x==NULL)
- {
- q->urmator=NULL;
- inceput=q;
- }
- else
- {
- while(x->urmator!=NULL&&(x->urmator->p)>pp)
- {
- x=x->urmator;
- }
- q->urmator=x->urmator;
- x->urmator=q;
- }
- }
- //adaugare mesaj
- void adaugM(mesaj k)
- {
- Nod *x;
- if(inceput!=NULL){
- x=inceput;
- while((x->p)!=k.p&&x!=NULL)
- x=x->urmator;
- if(x==NULL)
- adaugC(k.p);
- x=inceput;
- while((x->p)!=k.p&&x!=NULL)
- x=x->urmator;
- x->c=adauga(x->c,k);
- }
- else
- {
- adaugC(k.p);
- inceput->c=adauga(inceput->c,k);
- }
- }
- //deservire mesaj
- mesaj desM()
- {
- Nod *x;
- mesaj k;
- if(inceput!=NULL)
- {
- x=inceput;
- while(vid(x->c))
- x=x->urmator;
- k=scoate(&(x->c));
- return k;
- }
- }
- int main()
- {
- mesaj m,n;
- strcpy(m.s,"abc");
- m.id=1;
- m.p=1;
- adaugC(1);
- adaugC(2);
- adaugM(m);
- printf("\n\n%s\n\n",inceput->c.elem[0].s);
- n=desM();
- printf("%s",n.s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement