Thanks in advance for any help I can get. This is a que I need to add in to another program. It is almost straight out of the book, and a lot like my working stack implementation. It steps through correctly when I go line by line, but I'm not getting the correct values when I print data at line 29 in main.

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

int main()
{
    char infix[256] = {0};
    QUE *que;
    char *data;
    char token;
    int i = 0;

    que = createQue();
    data = (char*)malloc(sizeof(char));

    printf("enter an infix: ");
    scanf("%s", infix);
    /* printf("%s\n", infix); */
    for (i = 0; i< strlen(infix); i++)
    {
        token = infix[i];
        *data = token;
        enque(que, data);
    }
    while (que->qCount != 0)
    {
        data = (char*)deque(que);

        printf("%c", data);
    }
    destroyQue(que);
    //  printf("%d", (*(int*)data));

    return 0;
}


/********************que.c***************************/
#include "que.h"
#include <stdlib.h>
#include <stdio.h>

QUE *createQue()
{
    QUE *que;
    que = (QUE*)malloc(sizeof(QUE));
    if (que)
    {
        que->head = que->tail = NULL;
        que->qCount = 0;
    } /* end if */
    return que;
} /* end function createQueue */

QUE *destroyQue(QUE *que)
{
    QNODE *nodeToDelete;
    if (que)
    {
        while (que->head != NULL)
        {
            free(que->head->data);
            nodeToDelete = que->head;
            que->head = que->head->next;
            free(nodeToDelete);
        } /* end while */
        free (que);
    }/* end if */
    return NULL;
}/* end destroyQueue */

/*********************************************************/
bool enque(QUE *que, void *data)
{
    QNODE *newNode;
    newNode = (QNODE*)malloc(sizeof(QNODE));
    if (!newNode)
    {
        return false;
    }
    newNode->data = data;
    newNode->next = NULL;

    if (que->qCount == 0)
    {
        que->head = newNode;
    }
    else
    {
        que->tail->next = newNode;
    }
    que->qCount ++;
    que->tail = newNode;
    return true;
}
/*********************************************************/
void *deque(QUE *que)
{
    QNODE *nodeToDelete;
    void *dataOut;
    if (!que->qCount)
    {
        return NULL;
    }
    dataOut = que->head->data;
    nodeToDelete = que->head;
    if (que->qCount == 1)
    {
        que->tail = que->head = NULL;
    }
    else
    {
        que->head = que->head->next;
    }
    que->qCount--;
    free(nodeToDelete);

    return dataOut;
}
/*********************************************************/
#ifndef que_h
#define que_h
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

typedef struct QNODE
{
    void *data;
    struct QNODE *next;

}QNODE;


typedef struct QUE
{
    QNODE *head;
    QNODE *tail;
    int qCount;

}QUE;


QUE *createQue();

QUE *destroyQue(QUE *que);
bool enque(QUE *que, void *data);
void *deque(QUE *que);

#endif

/********************************************/

Recommended Answers

All 3 Replies

You are missing a * on line 29

Thank you for the help. I tried a * , but get the same result. For instance if I put in 3+2 for user input. I get back all 0's or all 3's it seems no matter what I do. I figure it must be in the enque function or deque function, but I can't find anything wrong with the logic, and it steps through each line as it should for both functions.

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

int main()
{

    QUE *que;
    char *data;
    int  i = 0;
    char token;
    char infix[256] = {0};
    que = createQue();
    data = (char*)malloc(sizeof(char));

    printf("enter infix: ");
    scanf("%s", infix);
    printf("%s\n", infix);
    for(i = 0; i < strlen(infix); i++)
    {
         token = infix[i];
         *data = token;
        enque(que, data);
        }

    while(que->qCount != 0)
    {
        deque(que,(char*)data);
        printf("%c", *data);
        }
destroyQue(que);






    return 0;
}

Irrelevant parts snipped:

bool enque(QUE *que, void *data)
{
    ....
    newNode->data = data;
    ...
}

int main()
{
    ...
    char *data;
    ....
    for(i = 0; i < strlen(infix); i++)
    {
         token = infix[i];
         *data = token;
         enque(que, data);
      }
    ...
}

The main() passes the same pointer in each call to enqueue(). The enqueue() doesn't do anything with it, so each node points the same data. A simplest fix is to store not a pointer, but a value:

typedef struct QNODE
{
    char data;
    struct QNODE *next;

}QNODE;

bool enque(QUE *que, char data)
{
    ....
    newNode->data = data;
    ...
}
        ....
        enque(que, token);
        ....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.