Print item from a queue by removing each item from it

0

I'm doing a program that receives a string with comma-separated words that separates those words and lines them (so far I've been able to do it quietly), then the program has to remove and print the items, but I can not develop the de-working function , I even tried to build on other book codes.

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

    struct fila {
        char nome [30];
        char dado;
        struct fila *proximoPtr;    
    };
    typedef struct fila Fila;
    typedef Fila *PtrFila;
    void imprimeFila (PtrFila atualPtr);
    int ehVazia(PtrFila headPtr );
    //void dequeue(PtrFila *headPtr, PtrFila *tailPtr );
    void enqueue(PtrFila *headPtr, PtrFila *tailPtr, char nome[30]);

    int main(){
            PtrFila headPtr = NULL;
            PtrFila tailPtr = NULL;
        for ( ; ; ){
            char linha[1024];
            puts("Escreva Nomes Separados por Virgula");
            fgets(linha, 1024 , stdin);
            char item;
            char* tok;
            char* tmp = strdup(linha);

            tok=strtok(tmp, ",");
            int cont=0;
            while (tok != NULL) {
                cont++;
                enqueue( &headPtr, &tailPtr, tok);

                tok = strtok (NULL, ",");
            }
            imprimeFila(headPtr);
            //dequeue (&headPtr, &tailPtr); A função que não está funcionando

        }
    }
    /*void dequeue(PtrFila *headPtr, PtrFila *tailPtr ) {
        char aux;
        PtrFila tempPtr;

        aux = ( *headPtr )->aux;
        tempPtr = *headPtr; 
        *headPtr = ( *headPtr )->proximoPtr; 

        if ( *headPtr == NULL ) {
            *tailPtr = NULL; 
        } 

        free( tempPtr );
        imprimeFila(headPtr);
    } */

    int ehVazia( PtrFila headPtr ){
        return headPtr == NULL;
    }

    void imprimeFila( PtrFila atualPtr ){
        PtrFila inicioPtr;
        inicioPtr=atualPtr;
        if ( atualPtr == NULL ) {
            puts( "Fila esta vazia.\n" );
        } 

            atualPtr=inicioPtr;
            while ( atualPtr != NULL ) {
                printf( "%s --> ", atualPtr->nome );
                atualPtr = atualPtr->proximoPtr;
            } 

            puts( "NULL\n" );

        } 

    void enqueue( PtrFila *headPtr, PtrFila *tailPtr, char nome[30]){

        char * novalinha = strchr(nome, '\n');
        if (novalinha)
            *novalinha = '
    #include <stdlib.h>
    #include <stdio.h>

    struct fila {
        char nome [30];
        char dado;
        struct fila *proximoPtr;    
    };
    typedef struct fila Fila;
    typedef Fila *PtrFila;
    void imprimeFila (PtrFila atualPtr);
    int ehVazia(PtrFila headPtr );
    //void dequeue(PtrFila *headPtr, PtrFila *tailPtr );
    void enqueue(PtrFila *headPtr, PtrFila *tailPtr, char nome[30]);

    int main(){
            PtrFila headPtr = NULL;
            PtrFila tailPtr = NULL;
        for ( ; ; ){
            char linha[1024];
            puts("Escreva Nomes Separados por Virgula");
            fgets(linha, 1024 , stdin);
            char item;
            char* tok;
            char* tmp = strdup(linha);

            tok=strtok(tmp, ",");
            int cont=0;
            while (tok != NULL) {
                cont++;
                enqueue( &headPtr, &tailPtr, tok);

                tok = strtok (NULL, ",");
            }
            imprimeFila(headPtr);
            //dequeue (&headPtr, &tailPtr); A função que não está funcionando

        }
    }
    /*void dequeue(PtrFila *headPtr, PtrFila *tailPtr ) {
        char aux;
        PtrFila tempPtr;

        aux = ( *headPtr )->aux;
        tempPtr = *headPtr; 
        *headPtr = ( *headPtr )->proximoPtr; 

        if ( *headPtr == NULL ) {
            *tailPtr = NULL; 
        } 

        free( tempPtr );
        imprimeFila(headPtr);
    } */

    int ehVazia( PtrFila headPtr ){
        return headPtr == NULL;
    }

    void imprimeFila( PtrFila atualPtr ){
        PtrFila inicioPtr;
        inicioPtr=atualPtr;
        if ( atualPtr == NULL ) {
            puts( "Fila esta vazia.\n" );
        } 

            atualPtr=inicioPtr;
            while ( atualPtr != NULL ) {
                printf( "%s --> ", atualPtr->nome );
                atualPtr = atualPtr->proximoPtr;
            } 

            puts( "NULL\n" );

        } 

    void enqueue( PtrFila *headPtr, PtrFila *tailPtr, char nome[30]){

        char * novalinha = strchr(nome, '\n');
        if (novalinha)
            *novalinha = '%pre%';  

        PtrFila newPtr; 
        newPtr=malloc(sizeof(Fila));

        if ( newPtr != NULL ) { 
            newPtr->dado = nome;
            newPtr->proximoPtr = NULL;

            strcpy( newPtr->nome,nome);


            if ( ehVazia( *headPtr ) ) {
                *headPtr = newPtr;
            } 
            else {
                ( *tailPtr )->proximoPtr = newPtr;
            } 

            *tailPtr = newPtr;
        } 
        else {
            printf( "%c nao foi inserido. Memoria nao foi disponibilizada.\n");
        } 
    } 
'; PtrFila newPtr; newPtr=malloc(sizeof(Fila)); if ( newPtr != NULL ) { newPtr->dado = nome; newPtr->proximoPtr = NULL; strcpy( newPtr->nome,nome); if ( ehVazia( *headPtr ) ) { *headPtr = newPtr; } else { ( *tailPtr )->proximoPtr = newPtr; } *tailPtr = newPtr; } else { printf( "%c nao foi inserido. Memoria nao foi disponibilizada.\n"); } }
    
asked by anonymous 10.07.2017 / 05:32

1 answer

0

Normally, for a queue, I would recommend creating a struct containing the two pointers inicioPtr and fimPtr so you do not have to be passing references to your local variables; in fact, problems in dequeue() all have to do with type errors between base types and their pointers. But let's see:

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

First, you forgot to include the <string.h> header to use strdup() and strcpy() . This works because the functions do not use data types defined in the header, but it is a bad habit. You should have taken some warning when compiling complaining about this: please try to read and parse the compiler messages when you compile your program.

struct fila {
    char nome [30];
    char * dado;
    struct fila *proximoPtr;    
};
typedef struct fila Fila;
typedef Fila *PtrFila;

In your queue, you have a nome member of type char[30] and a dado member of type char . Later, in enqueue() , you will get the name passed as a parameter and copy it to the nome and vector mark a reference to it in the dado field, which does not include it it is a char . So I changed it to char * , a pointer to char , although it's unclear why you want a reference if you already copied the name.

void dequeue(PtrFila *headPtr, PtrFila *tailPtr ) {
    PtrFila tempPtr;

    if (*headPtr == NULL) return;
    tempPtr = *headPtr; 
    *headPtr = ( *headPtr )->proximoPtr; 

    if ( *headPtr == NULL ) {
        *tailPtr = NULL; 
    } 

    free( tempPtr );
    imprimeFila(*headPtr);
}

The% wt% itself is simple. You create a dequeue() variable of type aux and assign it a non-existing member char of the queue, which almost certainly is the aux you renamed and forgot to refactor. But also after doing nothing with this variable, then I removed it completely.

Also, at the end, you were passing dado , which is headPtr , instead of ptrFila * , which is *headPtr according to the ptrFila function asks. That's why I personally do not like to synonymous with style imprimeFila() to ptrX . However, it is not an iron rule; there are people who like and do not curl about it.

Finally, in X* we have two problems:

void enqueue( PtrFila *headPtr, PtrFila *tailPtr, char nome[30]){

    char * novalinha = strchr(nome, '\n');
    if (novalinha)
        *novalinha = '
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
'; PtrFila newPtr; newPtr=malloc(sizeof(Fila)); if ( newPtr != NULL ) { newPtr->dado = nome; newPtr->proximoPtr = NULL; strncpy( newPtr->nome,nome,30); if ( ehVazia( *headPtr ) ) { *headPtr = newPtr; } else { ( *tailPtr )->proximoPtr = newPtr; } *tailPtr = newPtr; } else { printf( "%c nao foi inserido. Memoria nao foi disponibilizada.\n"); } }

First, you write enqueue() . But what happens if the user types a name with more than twenty-nine characters? This is a class of bugs that has already caused a lot of damage out there, so-called buffer overflows . Always prefer to use functions, such as strcpy( newPtr->nome,nome); , that receive the size of the output as a parameter. For any function you think of the default library, there is a version with strncpy() in the name that receives an extra parameter with the maximum number of characters to read or write. n , stncpy() , strncmp() , so out there.

Create this habit of checking the amount of characters you are typing, threefold if what you are writing comes from the user. Sooner or later, someone will write something that they should not and their software crashes ...

    
10.07.2017 / 16:31