views:

86

answers:

3

Okay I have the queue program that I have been working on and I finally figured most of it out. The problem I am having now is that everytime I enter numbers into the keyboard and then access them I get the same number. if I enter 5 ones when it goes to add them together it says the answer is 37 which is not right. here is my code again:

main.c

#include "queue.h"

#define loop 5
int main(){
    int i;
    int* dataPtr;
    int number;
    int sum;
    int average;
    QUEUE* numbers;
    numbers = createQueue ();

    printf("Please enter the 10 numbers you want to know the sum and average of.\n");

    for (i = 0; i < loop; i++){
        printf("Number %d: ", i+1);
        scanf("%d", &number);
        *dataPtr = number;
        enqueue(numbers, dataPtr);
    }
    for (i = 0; i < loop; i++){
        number = dequeue(numbers, (void*)&dataPtr);
        sum += number;
        enqueue(numbers, dataPtr);
    }
    average = sum/loop;

    printf("\nSum: %d", sum);
    printf("\nAverage: %d", average);
    system("Pause");
  return 0;

}

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;
}

I dont need to pester so much but I am trying to learn this stuff

+2  A: 

You absolutely must use a debugger and watch what happens as you cycle through your program. ddd is a great tool!

Hamish Grubijan
+4  A: 

Your dequeue function is returning a bool (which when converted to a number is 0 for false and 1 for true) so you are summing the wrong value.

R Samuel Klatchko
I see that now. I need to use the dataPtr as my source not the return value
shinjuo
nevermind I see that using the dataPtr just gives me back the memory location and not the data there. I need to look into a little more
shinjuo
To get the data pointed to by a pointer, use `*pointer`.
Vatine
+1  A: 

OK, there are a couple of problems here.

First, dataPtr is never initialized, so *dataPtr points to a random place in memory and you're setting it to an arbritary integer value. This invokes undefined behaviour (so nasal demons are a possibility).

Second, you need to allocate new storage each time through the loop, otherwise you will end up with ten references to the last number you read.

Vatine