Implementação de uma estrutura de tipo Fila usando TAD
arquivo.h
typedef struct sNO{
int num;
struct sNO *prox;
}NO;
typedef NO *FILA;
void iniciar(FILA *f);
void inserir(FILA *f, int num);
int remover(FILA *f);
void inicio(FILA *f);
void fim(FILA *f);
void exibir(FILA *f);
arquivo.c
#include"FilaDin.h"
#include<stdio.h>
#include<stdlib.h>
NO *memoria(){
return (FILA) malloc(sizeof(NO));
}
void iniciar(FILA *f){
*f = NULL;
}
void inserir(FILA *f, int num){
FILA temp = memoria();
temp->num = num;
temp->prox = NULL;
if(temp == NULL){
puts("Não foi possivel alocar memoria!");
}
if(*f == NULL){
*f = temp;
} else {
FILA fila = *f;
while(fila->prox != NULL){
fila = fila->prox;
}
fila->prox = temp;
}
}
int remover(FILA *f){
int num;
if(*f != NULL){
FILA temp = *f;
num = temp->num;
*f = temp->prox;
free(temp);
return num;
} else {
puts("fila vazia!");
}
}
void fim(FILA *f){
puts("Fim Fila:");
FILA temp = *f;
while(temp->prox != NULL){
temp = temp->prox;
}
printf("%d\n\n", temp->num);
}
void inicio(FILA *f){
puts("Inicio Fila:");
FILA temp = *f;
printf("%d\n\n", temp->num);
}
void exibir(FILA *f){
if(*f != NULL){
puts("Fila:");
FILA temp = *f;
while(temp != NULL){
printf("%d\n", temp->num);
temp = temp->prox;
}
puts("");
} else {
puts("fila vazia!");
}
}
Nenhum comentário:
Postar um comentário