C linked list search function [closed]
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*Define custom functions */
void insertElement();
bool elementExists();
int getNumElements();
/*Create linked list */
struct node {
int number;
int occurence;
struct node *next;
};
/*Call our linked list freqTable */
struct node *freqTable = NULL;
unsigned int numElements = 0;
int main(){
int readNumElements = 0;
int i = 0;
int newNum, status;
status = scanf("%d", &readNumElements);
if(status == -1){
fprintf(stderr, "%d is not a number\n", readNumElements);
exit(-1);
}
for (i = 0; i < readNumElements;i++) {
status = scanf("%d", &newNum);
if(status == -1){
fprintf(stderr, "%d is not a number\n", newNum);
exit(-1);
}
if(elementExists(newNum)){
printf("%d exists\n", newNum);
}else{
insertElement(&freqTable, newNum);
}
}
return 0;
}
void insertElement(struct node **list, int n){
struct node *new_input;
new_input = malloc(sizeof(struct node));
if(new_input == NU开发者_如何学运维LL){
fprintf(stderr,"Error: Failed to create memory for new node\n");
exit(EXIT_FAILURE);
}
new_input->number = n;
new_input->occurence = 1;
new_input->next = *list;
numElements++;
*list = new_input;
}
bool elementExists(int n){
printf("%d\n", freqTable->number);
return false;
}
int getNumElements(){
return numElements;
}
Ok heres what i got. This should compile.
Problem comes at
if(elementExists(newNum)){
printf("%d exists\n", newNum);
}else{
insertElement(&freqTable, newNum);
}
I get segmentation error and i am not sure why.
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.
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.
精彩评论