Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- #include<malloc.h>
- typedef struct estrutura{
- int valor;
- estrutura *prox;
- }NO;
- typedef struct
- {
- NO* inicio;
- NO* fim;
- }FILA;
- void inicializar(FILA *f)
- {
- f->inicio = NULL;
- f->fim = NULL;
- }
- bool filavazia(FILA *f)
- {
- if(!f->inicio) return true;
- return false;
- }
- void inserir(FILA *f, int valor)
- {
- NO* novo =(NO*) malloc(sizeof(NO));
- novo->valor = valor;
- novo->prox = NULL;
- if(f->fim !=NULL){
- f->fim->prox = novo; // anterior
- }
- else
- {
- f->inicio = novo;
- }
- f->fim = novo;
- }
- int retirar(FILA *f){
- if(filavazia(f)) return -1;
- int resp = f->inicio->valor;
- NO* aux = f->inicio;
- f->inicio = f->inicio->prox;
- free(aux);
- if(!f->inicio) f->fim =NULL;
- return resp;
- }
- void imprimir(FILA *f)
- {
- NO* p = f->inicio;
- while(p)
- {
- printf("%i\t", p->valor);
- p = p->prox;
- }
- printf("\n");
- }
- main()
- {
- FILA f;
- inicializar(&f);
- for(int i=0; i<10; i++)
- {
- inserir(&f,i);
- imprimir(&f);
- }
- imprimir(&f);
- for(int i=0; i<10; i++)
- {
- retirar(&f);
- imprimir(&f);
- }
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment