HashIndex

This is a discussion on HashIndex within the C Programming forums, part of the General Programming Boards category; So i have to write this function that creates a node to hold a student record specified by siid(student_id), lastname, ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    HashIndex

    So i have to write this function that creates a node to hold a student record specified by siid(student_id), lastname, firstname and gpa. After creating the new node i have to insert it to the proper list in the hash table pointed by hashtable. The memory of the new node is dynamically allocated. my question is: Two students cannot have the same student_id , so if the inserted record has the same student_id with an existing record in the list my program should print to the standard output:

    insertion failed: student_id xxxxxx already exists

    where xxxxxx is the student_id value. my second question: If the insertion of a record is successful it should print:
    .
    record xxxxx inserted

    this is what i have so far:

    Code:
    void insertRecord(Student **hashtable, unsigned int sid, char *lastname, char *firstname, float gpa); 
    {                
            
            Student *p, *head;
            p=(student *) malloc(sizeof(student));
            
            int i;
            
            Student *s;/*node*/
            
            if (s==NULL)
            {
                s->student_id =sid;
                strcpy(s->firstname,firstname);/*copying first and lastname to s*/
                strcpy(s->lastname,lastname);
                s->gpa=gpa;
                hashtable[sid%M] = s;
            }
                
            else
            {/*s != NULL;*/
                 s = s-> next;
                 s->student_id=sid;
                
            }
            if (student_id = s)
            {
                printf("insertion failed: student_id %d already exists\n", sid);
            }
            printf("record %d inserted", sid);
    Last edited by Salem; 03-17-2010 at 11:31 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Use code tags.
    2. Why are you checking s when you haven't actually done anything with it? What does it matter if it's NULL or not -- you haven't assigned anything to it, so whatever it has is completely irrelevant.
    3. If it was NULL, you immediately try to use it. That's bad.
    4. Your last if check uses = instead of == to test for equality.
    5. You dupe posted. Just press submit once.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    main

    i actually have the main function that tests the program when it is done. i use s as a pointer because i have to go through the hashtable.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    quzah in point 2 is talking about this:
    Code:
    Student *s;/*node*/
    
    if (s==NULL)
    {
    s->student_id =sid;
    You use s without initializing it. That's bad. Then you go on to dereference s if it's NULL. That's bad too.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HashIndex
    By hayood in forum C Programming
    Replies: 1
    Last Post: 03-17-2010, 11:27 AM
  2. Hash Table
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2009, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21