Problem inserting elements into a queue

I made a function using the concept of static queue to insert an element, however, it is not executing when I call it in main. I don't know if the problem is in it, the main function or the two rs... following Insert 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;
}

Hope you can help!

 2
Author: Sbim, 2017-06-24

1 answers

Good I rewrote his code with some changes because the way he was the logic was a little confused. Sorry for my edited answer.

Your code at first in main was only with the struct declared with pointer, but actually with static queue you work through reference.

I have created a function to initialize the queue, which is essential as you need your queue to start with values.

In the Insert Function you will always always add the element at the end of the queue since the queue goal is the "first in first out" so the variable qtd will mark the end of the queue so every time you add an element you will increment it.

In removal you will always remove from the beginning of the queue then you will have the variable Start checking which element first to exit the queue.

Continue your implementation from this code which I think will work out. Sorry I joined the community now and don't have much practice teaching through posts.

#include<stdio.h>
#include<stdlib.h>
#define tamanho_fila 5

struct queue{
   int inicio;
   int qtd;
   int elemento[tamanho_fila];
};

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;
}

void inicFila(struct queue *q){
    q->qtd=-1;
    q->inicio = 0;
}

int fila_vazia(struct queue *q){
   if(q->qtd<q->inicio)
          return 1;
   return 0;
}

int inserir_fim_fila(struct queue *q, int vlr){
   int fim;
   if(q->qtd == tamanho_fila-1){
          printf("CAPACIDADE MAXIMA DA FILA ESTOUROU\n");
          return 1;
   }else
        return q->elemento[++(q->qtd)] = vlr;
}

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++;
          return aux;
   }
}

void mostrar_fila(struct queue *q){
   int i;
   if(!fila_vazia(&q)){
       for(i=0;i<q->qtd;i++){
              printf("%i", q->elemento[i]);
       }
   }
   else printf("A fila não tem nenhum elemento.");
}

void free_queue(struct queue* q){
   free(q);
}

int main(){
    int opc, vlr;
    struct queue q;
    inicFila(&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;
}
 2
Author: xGlover, 2017-06-24 22:07:17