Problem inserting elements in a queue

Asked

Viewed 282 times

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? =]

  • 1

    @Blogger, yes of course.. I edited my question..

  • 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 be while(opc=menu()){}.

1 answer

2


Well I rewrote your code with some changes because the way it was the logic was a little confused. Sorry for my edited reply.

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

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

In the insertion function you will always add the element at the end of the queue since the Queue goal is the "first to enter the first to exit" then the variable Qtd will mark the end of the Queue so every time you add an element you will increment it.

In the removal you will always remove from the top of the queue then you will have the variable beginning marking the first element to exit the queue.

Continue your implementation from that code that I think will work. Sorry I joined the community now and I 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;
}
  • Hi Luiz, welcome to Stack Overflow! Usually the answers given here "solve" the problem - for questions, use the comment field (which you can use soon, when you have more reputation). For the time being, I suggest that you answer only clear questions that can give an accurate solution.

  • Hello! The %queue_size is to indicate where the end of the queue is located in the array, for this I need to add the number of items + index of the beginning % queue_size.. example: 2+4/5 = means the end of the queue is at index 1.. Anyway, I run the system and it gets to run, but it does not ask wait I insert the element and it closes... maybe the problem is in the main

  • Hello guys, Thanks for welcoming @Blogger understand your tips, I just entered the community I hope I can help other people with the codes. I edited my reply you can check now. Thank you for understanding. See more.

  • Good! You can use websites with repl it. to demonstrate the code in operation (in addition, of course, to putting it in the text) - usually adds value to the answer! ;)

  • @Luizeduardoc. Thank you so much for your help!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.