How to create QUEUE by passing STRUCT

Asked

Viewed 674 times

0

I am trying to create a code in which I will insert 10 candidate names in a queue using a C structure. But I don’t have much knowledge in C data structure.

I did it this way:

#define TAM 5

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>


    typedef struct
    {

        int final;
        int inicio;
        int tamanho;   
        int contador;

        char *vetorFila[TAM];

    }  pretendentes;


    void ListarNomesDosPretendentes();
    void removerNomesDosPretendentes();
    void receberNomesDosPretendentes();
    void armazenarNomesDosPretendentes();


    int main(void)
    {
        setlocale(LC_ALL, "");        


        int menu = 0;
        int operacao = 0;


            do
            {
                printf("1 - Inserir nome\n");
                printf("2 - Remover nome\n");
                printf("3 - Exibir fila\n");
                scanf("%i", &menu);

            } while(menu < 0 && menu > 3);


            switch (operacao)
            {
                case 1:
                {
                    receberNomesDosPretendentes();
                }
                case 2:
                {
                    removerNomesDosPretendentes();
                }
                case 3:
                {
                    ListarNomesDosPretendentes();
                }
            }

        return 0;
    }



    void receberNomesDosPretendentes()
    {
        static char nomes[10], *ptr;

        printf("Digite o nome ou aperte ENTER para sair.");
        do
        {
            printf("\n\nDigite o nome do pretendente: ");
            gets(nomes);

            ptr = (char*) malloc(strlen(nomes));
            strcpy(ptr, nomes);

            armazenarNomesDosPretendentes(ptr);

        } while(*nomes);
    }



    void armazenarNomesDosPretendentes(pretendentes *Pretendente, char *ptr)
    {
        if(Pretendente -> inicio == TAM)
        {
            printf("A fila está cheia.");
            return 0;
        }

        Pretendente -> vetorFila[Pretendente->inicio] = ptr;
        Pretendente -> inicio++;

    }

    void removerNomesDosPretendentes(pretendentes *Pretendente)
    {
        if(Pretendente -> inicio == Pretendente -> final)
        {
            printf("A fila está vazia.");
            return 0;
        }

        Pretendente -> final--;

    }

    void ListarNomesDosPretendentes(pretendentes *Pretendente)
    {
        int i;

        for(i = Pretendente -> final; i < TAM; i++)
        {
            printf("Pretendente %d: %s", i+1, Pretendente -> vetorFila[i]);
        }
    }

But just when I turn is giving an error, someone helps me identify the error? It runs but soon breaks...

  • "int pinicial" should be global, like this. You are always inserted at position 0. You can use "Static int pinicial"

  • Probably the worst C book ever written.

  • Interestingly, his code matches his of this question. And I can’t resist asking the same question I did. The function qstore is supposed to do what ?

  • @Isac The function of qstore is to store queued names entered by the user.

  • @Maniero Which language book would you refer me to? I need solid references, I turned to books.

  • https://answall.com/tags/c/info

  • There is tutorial on queue on the internet, gave a google and found this. I have not read it, I do not know if it is good, but since it is an extremely basic subject, any tutorial should serve

Show 2 more comments
No answers

Browser other questions tagged

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