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
/********************************************/