Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:08:27+00:00 2026-05-17T00:08:27+00:00

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> /*Define custom functions */ void insertElement(); bool elementExists();

  • 0
#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 == NULL){
        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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:08:28+00:00Added an answer on May 17, 2026 at 12:08 am

    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.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> #include <time.h> #define length 100 void print_array(); int main() {
#include <stdio.h> #include <stdlib.h> #define calc(a,b) (a*b)/(a-b) void calculate(){ int a = 20, b
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
#include <stdio.h> #include <stdlib.h> int main() { void *malloc(size_t size); char *ptr, *retval; ptr
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> char** parser(char *message) { char a[9][256]; char* tmp =message; bool
# include <stdio.h> # include <stdbool.h> # include <string.h> # include <stdlib.h> int main
#include <stdio.h> #include <stdlib.h> #define rsize 3 #define csize 3 int main() { char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.