correction it works now thanks everyone.
Okay so now I have updated my main file to
main.c
#include "queue.h"
int main(){
int i;
int* dataPtr;
int number;
QUEUE* numbers;
numbers = createQueue ();
printf("Please enter the 10 numbers you want to know the sum and average of.\n");
for (i = 0; i < 10; i++){
printf("Number %d: ", i+1);
scanf("%d", &number);
*dataPtr = number;
enqueue(numbers, dataPtr);
}
system("Pause");
return 0;
}
But my program crashes when it is ran
Does anyone know why this is happening?
Original Post
I am having trouble figuring out how to fill a queue with user input in C. Can anyone point me in the right direction.
queue.h:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//Queue ADT Type Definitions
typedef struct node{
void* dataPtr;
struct node* next;
} QUEUE_NODE;
typedef struct{
QUEUE_NODE* front;
QUEUE_NODE* rear;
int count;
} QUEUE;
//Prototype Declarations
QUEUE* createQueue (void);
QUEUE* destroyQueue (QUEUE* queue);
bool dequeue (QUEUE* queue, void** itemPtr);
bool enqueue (QUEUE* queue, void* itemPtr);
bool queueFront (QUEUE* queue, void** itemPtr);
bool queueRear (QUEUE* queue, void** itemPtr);
int queueCount (QUEUE* queue);
bool emptyQueue (QUEUE* queue);
bool fullQueue (QUEUE* queue);
//End of Queue ADT definitions
Queue.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
QUEUE* createQueue (void){
QUEUE* queue;
queue = (QUEUE*) malloc (sizeof (queue));
if (queue){
queue->front = NULL;
queue->rear = NULL;
queue->count = 0;
}
return queue;
}
bool enqueue (QUEUE* queue, void* itemPtr){
QUEUE_NODE* newPtr;
if(!(newPtr = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE))))
return false;
newPtr->dataPtr = itemPtr;
newPtr->next = NULL;
if (queue->count == 0)
queue->front = newPtr;
else
queue->rear->next = newPtr;
(queue->count)++;
queue->rear = newPtr;
return true;
}
bool dequeue (QUEUE* queue, void** itemPtr){
QUEUE_NODE* deleteLoc;
if (!queue->count)
return false;
*itemPtr = queue->front->dataPtr;
deleteLoc = queue->front;
if(queue->count == 1)
queue->rear = queue->front = NULL;
else
queue->front = queue->front->next;
(queue->count)--;
free (deleteLoc);
return true;
}
bool queueFront (QUEUE* queue, void** itemPtr){
if (!queue->count)
return false;
else{
*itemPtr = queue->front->dataPtr;
return true;
}
}
bool queueRear (QUEUE* queue, void** itemPtr){
if (!queue->count)
return true;
else{
*itemPtr = queue->rear->dataPtr;
return false;
}
}
bool emptyQueue (QUEUE* queue){
return (queue->count == 0);
}
bool fullQueue(QUEUE* queue){
QUEUE_NODE* temp;
temp = (QUEUE_NODE*)malloc(sizeof(*(queue->rear)));
if (temp){
free (temp);
return true;
}
return false;
}
int queueCount(QUEUE* queue){
return queue->count;
}
QUEUE * destroyQueue (QUEUE* queue){
QUEUE_NODE* deletePtr;
if (queue){
while (queue->front != NULL){
free (queue->front->dataPtr);
deletePtr = queue->front;
queue->front = queue->front->next;
free(deletePtr);
}
free (queue);
}
return NULL;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int main(){
int number;
int *dataPtr;
QUEUE* numbers;
numbers = createQueue ();
printf("Please enter the 10 numbers you want to know the sum and average of.\n");
for (int i = 0, i < 10; i++){
if (!(dataPtr = (int*) malloc (sizeof (int))))
printf("Overflow trying to fill queues.\n"), exit(100);
system("Pause");
return 0;
}