Created
March 20, 2020 02:04
-
-
Save TopRoupi/3da12cfe812f4e0505b8183fb18cd898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) { | |
typedef struct { | |
char vet[3]; | |
int front,rear; | |
} tfila; | |
void create(tfila *f){ | |
f->front = 0; | |
f->rear = 0; | |
} | |
int next(int n){ | |
if(n < 3) | |
return n + 1; | |
return 0; | |
} | |
int isempty(tfila f){ | |
return f.front == f.rear; | |
} | |
int isfull(tfila f){ | |
return next(f.rear) == f.front; | |
} | |
void store(tfila *f, char elem){ | |
if(isfull(*f)) | |
printf("\noverflow\n"); | |
else{ | |
f->vet[f->rear] = elem; | |
f->rear = next(f->rear); | |
} | |
} | |
char retrieve(tfila *f){ | |
if(isempty(*f)) | |
printf("\nunderflow\n"); | |
else{ | |
char aux = f->vet[f->front]; | |
f->front = next(f->front); | |
return aux; | |
} | |
} | |
typedef struct { | |
tfila f1, f2, f3, f4; | |
} tfilaprior; | |
void init(tfilaprior *fp){ | |
create(&(fp->f1)); | |
create(&(fp->f2)); | |
create(&(fp->f3)); | |
create(&(fp->f4)); | |
} | |
void inclui(tfilaprior *fp, int prior, char elem){ | |
switch(prior) { | |
case 1 : | |
store(&(fp->f1), elem); | |
break; | |
case 2 : | |
store(&(fp->f2), elem); | |
break; | |
case 3 : | |
store(&(fp->f3), elem); | |
break; | |
case 4 : | |
store(&(fp->f4), elem); | |
break; | |
} | |
} | |
char atende(tfilaprior *fp){ | |
if(!isempty(fp->f1)) | |
return retrieve(&fp->f1); | |
if(!isempty(fp->f2)) | |
return retrieve(&fp->f2); | |
if(!isempty(fp->f3)) | |
return retrieve(&fp->f3); | |
return retrieve(&fp->f4); | |
} | |
tfilaprior filaprior; | |
init(&filaprior); | |
inclui(&filaprior, 1, 'a'); | |
inclui(&filaprior, 2, 'b'); | |
inclui(&filaprior, 2, 'c'); | |
inclui(&filaprior, 3, 'd'); | |
inclui(&filaprior, 4, 'e'); | |
printf("%c\n", atende(&filaprior)); | |
printf("%c\n", atende(&filaprior)); | |
printf("%c\n", atende(&filaprior)); | |
printf("%c\n", atende(&filaprior)); | |
printf("%c\n", atende(&filaprior)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment