2
I made a function using the concept of static queue to insert an element, however, it is not running when I call it in main. I do not know if the problem is in her, in the main function or in the two rs... follows insertion function:
#include<stdio.h>
#include<stdlib.h>
#define tamanho_fila 5
int menu(){
int opc;
printf("[1]QUEUE\t");
printf("[2]DEQUEUE\t");
printf("[3]FREE QUEUE\t");
printf("[4]SHOW FIRST ELEMENT\t");
printf("[5]SHOW ALL ELEMENTS\t\n");
printf("\nPLEASE, CHOOSE ANY OPTION: ");
scanf("%i", &opc);
return opc;
}
struct queue{
int inicio;
int qtd;
int elemento[tamanho_fila];
};
int fila_vazia(struct queue *q){
if(q->qtd==0)
return 1;
return 0;
}
void inserir_fim_fila(struct queue *q, int vlr){
int fim;
if(q->qtd==tamanho_fila){
printf("CAPACIDADE MAXIMA DA FILA ESTOUROU\n");
exit(0);
}else{
fim=(q->inicio+q->qtd)%tamanho_fila;
q->elemento[fim]=vlr;
q->qtd++;
}
}
int remover_inicio_fila(struct queue *q){
int aux;
if(fila_vazia(q)){
printf("IMPOSSIVEL REMOVER, FILA VAZIA");
return 0;
}else{
aux=q->elemento[q->inicio];
q->inicio=(q->inicio+1)%tamanho_fila;
q->qtd--;
return 1;
}
}
void mostrar_fila(struct queue *q){
int i;
for(i=0;i<q->qtd;i++){
printf("%i", q->elemento);
}
}
void free_queue(struct queue* q){
free(q);
}
int main(){
int opc, vlr;
struct queue *q;
while(opc==menu()){
switch(opc){
case 1:
printf("INSERT ELEMENT: ");
scanf("%i", &vlr);
inserir_fim_fila(&q, vlr);
break;
case 3:
free_queue(&q);
break;
};
system("pause");
}
return 0;
}
I hope you can help!
Hello Camilla! You can post your full code, or something close to explained here, please? =]
– Daniel
@Blogger, yes of course.. I edited my question..
– Sbim
Camilla - someone will have to answer this better here (someone who understands!), but for you to keep working, I think there’s one more like it:
while(opc==menu()){}
. Probably should bewhile(opc=menu()){}
.– Daniel