Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define MAX 3
- typedef struct No {
- int x,v[MAX];
- struct No *prox,*inicio,*fim;
- }fila;
- fila *fila_cria(void)
- {
- /* funcao que cria uma fila vazia */
- fila *f = (fila *) malloc(sizeof(fila));
- if (f != NULL) /* se nC#o houver nenhuim erro de alocacao */
- f->inicio = NULL;
- f->prox = NULL;
- f->fim = NULL;
- return f; /* retorne a fila */
- }
- bool isEmpty(fila *f)
- {
- return(f->fim == NULL);
- }
- bool enfileirar(fila *f,int v)
- {
- fila *no = (fila *) malloc(sizeof(fila)); /* aloque dinamicamente */
- fila *tmp;
- if (no == NULL)
- fprintf(stderr,"erro \n"); /* fprintf(2...); envia um fluxo para uma saida */
- f->x =v;
- f->prox = NULL;
- if (isEmpty(f)){
- f->inicio = no;
- f->x =v;
- }else{
- //tmp = no->prox;
- while(tmp != NULL){
- tmp = tmp->prox;
- f->x =v;
- f->fim->prox = no;
- }
- f->fim->prox = no;
- }
- return true;
- }
- void imprime(fila *f)
- {
- fila *pointer;
- pointer = f;
- int i;
- for(; pointer != NULL; pointer = pointer->prox)
- printf(" [%d] \n",pointer->x);
- }
- int main()
- {
- fila *f= fila_cria();
- int opc,num;
- for(;;){
- printf("1---inserir \n");
- printf("2--- imprime \n");
- scanf("%d ",&opc);
- switch(opc){
- case 1:
- scanf("%d",&num);
- enfileirar(f,num);
- break;
- case 2:
- imprime(f);
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement