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 7793335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:27:16+00:00 2026-06-01T22:27:16+00:00

I am trying to implement a hash table with linked list chaining. The following

  • 0

I am trying to implement a hash table with linked list chaining. The following code below works –

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TABSIZ 200

struct record {
    struct record *next;
    char name[BUFSIZ];
    int data;
};

static struct record *htable[TABSIZ];

unsigned hash(char *s)
{
    unsigned h;

    for (h = 0; *s; s++)
        h = *s;
//printf("%d", h%TABSIZ);
//I know its not a good hash function but i wanted to check chaining
    return h % TABSIZ;
}

struct record *find(char *name)
{
    struct record *item;

    for (item = htable[hash(name)]; item; item = item->next)
    {
        if (strcmp(name, item->name) == 0)
            return item;
    }

    return NULL;
}

struct record *insert(char *name,int value)
{
    struct record *item;
    unsigned h;

    if ((item = find(name)) == NULL)
    {
        if ((item = malloc(sizeof (*item))) == NULL)
            return NULL;

        strcpy(item->name, name);
        item->data=value;
        h = hash(name);
        item->next = htable[h];
        htable[h] = item;
    }

    return item;
}
void printTable()
{
    int i=0;
    struct record *temp;
    for(i=0;i<=TABSIZ;i++)
    {
        temp=htable[i];
        while(temp!=NULL)
        {
            printf("\n%d - %s - %d\n", i,temp->name, temp->data);
            temp=temp->next;
            }
    }
}
int main(void)
{
    char buf[BUFSIZ];int value;
    struct record *item;
    do{
    printf("Enter the name of the student:\n");
    scanf("%s", buf);
    if(strcmp(buf,"stop")==0) break;
    printf("Enter the marks of the student:\n");
    scanf("%d", &value);
    if(insert(buf, value)==NULL)
    {
        break;
    }
}while((strcmp(buf,"stop"))!=0);

    printf("Enter a name to find: ");
    scanf("%s", buf);
    if((item=find(buf))!=NULL)
        printf("The marks of the student is %d\n", item->data);
    else printf("\n Not Found\n");
    printTable();
    return 0;
}

Now I am trying to remove the global variable and use local variable for the array of structures. I removed the global declaration of htable and declared it in main as

struct record *htable[TABSIZ];

and changed the functions to

struct record *find(struct record *htable, char *name);
struct record *insert(struct record *htable, char *name,int value);

and I’m calling the functions as

find(htable, name);
insert(htable,name,value);

but now my program is segfaulting. Am i passing the array of structures right? and have I declared it correctly. Any help would be greatly appreciated.

  • 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-06-01T22:27:18+00:00Added an answer on June 1, 2026 at 10:27 pm

    I was going down the wrong path with my earlier answer.

    When it’s a global, it’s automatically initialized to 0.

    When it’s declared on the stack of main, it’s not initialized.

    Add a memset( htable, 0, sizeof(htable)) in main(), and that should return it to the previous behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
I'm trying to implement a Chord distributed hash table. I want to use SHA-1
I am trying to implement a hash table
I'm trying to implement an efficient hash table where collisions are solved using linear
im trying to implement a hash function in python. Would you consider the following
I've been trying to implement a hash table in Java for an exercise. For
I am trying to implement hash_multimap in C++. Here is the code: #include <hash_map>
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
I'm trying to implement some data structures (like HAMP-hash array mapped trie) in Common
Hi I am trying to serialize a hash table but not happening private void

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.