Problem inserting elements in a row

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 it, in the main function or in the two rs ... follow 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;
}

I hope you can help!

    
asked by anonymous 24.06.2017 / 19:11

1 answer

1

Well I rewrote his code with some changes because the way it was, the logic was a bit confusing. 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 since 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 to go first", then the variable qtd will mark the end then each time you add an element you will increment it.

In the removal you will always remove from the beginning of the queue so you will have the start variable marking which is the first element to leave the queue.

Continue your implementation from this code that I think will work. Sorry I joined the community now and I do not 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;
}
    
24.06.2017 / 19:55