Edit: Stupid error. I found it. Thanks for the help though.
Sorry i can't keep my code up
Edit: Stupid error. I found it. Thanks for the help though.
Sorry i can't keep my code up
As suggested in the comments, you're dereferencing freqTable
in elementExists
before any nodes are allocated. Thus, freqTable
is always NULL, so you invoke undefined behavior. You could just add a check for NULL in elementExists
, and return false in that case.
In the function elementExists
you need to ensure that freqTable is not NULL
:
bool elementExists(int n){
if(freqTable) { // add this check
printf("%d\n", freqTable->number);
}
}
Also your elementExists
does not do what its supposed to do(check for existence of a node with value n
), you should do something like:
bool elementExists(int n) {
if(!freqTale) { // table does not exist..return false.
return false;
}
// table exists..iterate node by node and check.
struct node *tmp = freqTable;
while(tmp) { // loop till tmp becomes NULL
if(*tmp == n) { // it node contains n..return false.
return true;
}
tmp = tmp->next; // move on
}
return false; // n does not exist in the list..return false.
}
There's a problem with the call to elementExists
:
if(elementExists(&freqTable, newNum)){
You pass the address of freqTable (i.e. a pointer to where the pointer to the first element of the list is stored) instead of its value. Yet in elementExists
you dereference the argument as though it were a pointer to a list element:
printf("Compare with: %d\n", list->number);
and
else list = list->next;
Remove the &
from the call to elementExists
. And don't reference the global freqTable
inside elementExists
if you are passing the list as an argument.