Link to home
Start Free TrialLog in
Avatar of jpipitone
jpipitone

asked on

Implementing a search and delete function into a Hash table

Can anyone give me some guidance as to how to add a search and delete function to my existing code?

Thank you for any help.  No rush.

/*This is a program to demonstrate Hashing-to-Lists Functions*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMPOINTERS 5

 
/*  Structure Declaration */
typedef struct student STUDENTREC;
struct student
{
     char ss[10];
     struct student *next;
};
 

/*Function Declarations*/
STUDENTREC *insert(char ss[], STUDENTREC *student_body[], int hashval);
void traverse(STUDENTREC *student_body[]);
int hash(char ss[]);

int main()
{
     char ss[10];
     STUDENTREC *student_body[NUMPOINTERS] = {NULL};
     STUDENTREC *person;
     int hashval;
     
     while(printf("Enter Social Security Number: "), strcmp(gets(ss), "quit"))
     {
          hashval = hash(ss);
          person = insert(ss, student_body, hashval);
          if (person)
               printf("You have attempted to enter a duplicte record!\n");
     } /*end of while*/

     traverse(student_body);
     return 0;

} /*end of main() */

/*Hash Social Security number by summing the cubes of the ASCII value
of characters and then take the modulo of this sum.  Return reslt(hashval). */
int hash(char ss[])
{
     long hashval = 0;

     for(; *ss != '\0'; ss++)
          hashval += (*ss) * (*ss) * (*ss);
     return hashval % NUMPOINTERS;
} /*end of hash() */

/*Put hashed record in list indicated by hash value. */
STUDENTREC *insert(char ss[], STUDENTREC *student_body[], int hashval)
{
     STUDENTREC **mover;

     mover = &student_body[hashval];
     while (*mover)
     {
          if (strcmp(ss, (*mover)->ss) == 0)
               return (*mover);
          mover = &((*mover)->next);
     } /*end of while */

     if ((*mover = (STUDENTREC *) malloc(sizeof(STUDENTREC))) ==NULL)
     {
          printf("Malloc error in insert\n");
          exit(1);
     } /*end of if */

     strcpy((*mover)->ss, ss);
     (*mover)->next = NULL;
     printf("Person with ss number %s placed in list %d.\n", ss, hashval);
     return NULL;
} /*end of insert() */

/*searches the hash table for a social security number */
STUDENTREC *insert(char ss[], STUDENTREC *student_body[], int hashval)






/*Output contents of lists */
void traverse(STUDENTREC *student_body[])
{
     int i;
     STUDENTREC **mover;

     for (i = 0; i < NUMPOINTERS; i++)
     {
          printf("Contents of list %d:\n", i);
          printf("--------------------\n");
          for (mover = &student_body[i]; *mover; mover = &(*mover)->next)
               printf("%s\n", (*mover)->ss);
          printf("\n");
     } /*end of for */

} /* end of traverse() */
ASKER CERTIFIED SOLUTION
Avatar of burtdav
burtdav

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bkrahmer
bkrahmer

Hey look everybody!  It's 'theoriginalplan' still trying to get us to do his homework!  Please ignore this guy so he'll stop being a freeloader.

brian
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by burtdav (or delete as homework).

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer