tags:

views:

76

answers:

3

Okay I have updated my code quite a bit. I am getting a new problem, but it seems to be on a correct path. Now when I enter in the numbers it just continually spits out the first number I entered instead of moving to the next number.

main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cType.h>
#include "list.h"


#define amount 3 

//Prototypes
void printList(LIST* number);

int main(){
    int i;
    int* dataPtr;
    int number;

    LIST* numberList;
    numberList = createList();

    printf("Please enter %d numbers for the linked list\n", amount);

    for(i = 0; i < amount; i++){
          printf("#%d: ", i+1);
          scanf("%d", &number);
          dataPtr = malloc(sizeof(int));
          *dataPtr = number;
          addNode(numberList, dataPtr);
    }
    printList(numberList);
    system("PAUSE");    
    return 0;
}

void printList(LIST* number){
     int* dataPtr;
     while (!emptyList(number)){
           traverse(number,0, (void*)&dataPtr);
           printf("%d\n", *dataPtr);
           addNode(number, dataPtr);
     }
}

list.h

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

//List ADT Type Definitions
typedef struct node{
        void* dataPtr;
        struct node* link;
        } NODE;

typedef struct{
        int count;
        NODE* pos;
        NODE* head;
        NODE* rear;
        } LIST;
//Prototype Declarations
LIST* ceateList(void);

bool traverse (LIST* pList, int fromWhere, void** dataOutPtr);

int listCount (LIST* pList);
bool emptyList (LIST* pList);
bool fullList (LIST* pList);

bool addNode (LIST* pList, void* dataInPtr);

list.c

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

#include "list.h"

LIST* createList(void){
      LIST* list;

      list = (LIST*) malloc (sizeof(list));

      if(list){
               list->head = NULL;

               list->rear = NULL;
               list->count = 0;
      }
      return list;
}

bool addNode(LIST* pList, void* dataInPtr){
       NODE* pNew;

       if(!(pNew = (NODE*) malloc(sizeof(NODE))))
                 return false;

       pNew->dataPtr = dataInPtr;
       pNew->link =  NULL;

       if(pList->count == 0){
               pNew->link = pList->head;
               pList->head = pNew;
               if(pList->count == 0)
                 pList->rear = pNew;
       }
       else{
            pNew->link = pNew;

            if(pNew->link == NULL)
               pList->rear = pNew;
       }
       (pList->count)++;
       return true;
}


bool emptyList(LIST* pList){
     return(pList->count == 0);
}

bool fullList(LIST* pList){
     NODE* temp;

     if((temp = (NODE*)malloc(sizeof(*(pList->head))))){
              free(temp);
              return false;
     }
     return true;
}

int listCount(LIST* pList){
    return pList->count;
}

bool traverse(LIST* pList, int fromWhere, void** dataPtrOut){
     if(pList->count == 0)
                     return false;

     if(fromWhere == 0){
                  pList->pos = pList->head;
                  *dataPtrOut = pList->pos->dataPtr;
                  return true;
     }
     else{
          if (pList->pos->link == NULL)
             return false;
          else{
               pList->pos = pList->pos->link;
               *dataPtrOut = pList->pos->dataPtr;
               return true;
          }
     }
}
+1  A: 

When you call _insert() you havn't initialized pPre. In C, uninitialized memory is not nessescarily NULL.

Also, if I were you I'd remove the pos pointer from the LIST type - using it makes your code very much not reentrant.

gnud
How would you initialize it? as for the pos pointer I need that for the position.
shinjuo
like this pPre = malloc(sizeof(NODE));?
shinjuo
no. Your `_insert` method expets a pointer to the location where it should insert the new data. Where do you want to insert the new data? at the beginning of the list? At the end? In the middle?
gnud
hmmm I think I will just pull that part out then. A linked list doesnt necessarily need that does it? if it does how can I fix what you said?
shinjuo
+5  A: 
list = (LIST*) malloc (sizeof(list));

You have the size of a pointer, not that of a struct.

Tronic
what do you mean?
shinjuo
`sizeof(list) == sizeof(LIST*) == sizeof(void*) != sizeof(LIST)`
gnud
would I just put that right after list = (LIST*) malloc (sizeof(list));?
shinjuo
eh, no. You would replace your line with `list = malloc(sizeof(LIST));`
gnud
still crashes after I input the amount of desired numbers. The program takes in the 5 numbers and then sits and crashes
shinjuo
A: 

I think learning to use a debugger would be a good idea. It is an essential piece of any devs skill set.

If this is on a Linux system using gcc (most likely) then compile the code with -g and run it using gdb. If you need more guidance on this then says so

PS: I agree with earlier poster that you should not use all caps variable names. Traditionally all caps are for macros and constants

pm100
I am using dev C++ to compile my program. As for the way the variables are, as I said they base of this is being copied from my Data structures and algorithms book. I just happen to be changing the program that it went to originally
shinjuo
i dont know what 'Dev c++' is - does it have a debugger?
pm100
yes but I am not sure how to use it
shinjuo
learning how to use it is a good investment in your time. It is always interesting and sometimes surprising to watch your code running in a debugger
pm100
I will do that. I am a little surprised I have never had a teacher make us learn it. This is my fifth programing class
shinjuo