Thread: Need help

  1. #1
    C-dummy
    Guest

    Need help

    Hi all,
    I have a hard time to figure out to code the program assignment below for my C course. I wish I could get a grade C for this class.

    [CODE]
    struct student
    {
    int ID;
    char name[20];
    int exam1;
    int exam2;
    int finalExam;
    };
    struct stuTable
    {
    int totalpoint;
    int count;
    struct student *stu;
    } ;

    int Search(struct stutable *T, int id, struct student **stud)
    {
    /*Purpose: locate and return a pointer to a student if the
    student is found in the stuTable.
    return 1 if student is found otherwise return 0 */

    }
    [\CODE]

    What struct student **stud mean? and can anyone help to show me how to code for the Search() function.
    Your help would appreciate.
    C_dummy

  2. #2
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Well, I am a little new at this but I had a similar question about this same context...so I'll give this a shot. I might do more harm than good but here goes....

    The function definition:
    int Search(struct stutable *T, int id, struct student **stud)

    Assuming that the variables
    stutable *A, *B; are declared.

    when it's called, it's called like this.


    Search(A, id, &B) A is the pointer to a structure. id is the numeric student ID number that you are searching your list in hopes of finding a match. You're passing the address of a pointer for B which is your list. (This has something to do with having access to the last node of the linked list. But the **stud is a double dereference. First dereference gets you access to the address of the pointer, the second gets you access to the pointer itself.)

    So when you use the pointer in your function you use it like this:
    (*stud)

    or a practical example would be...
    *stud = (*stud)->stu;
    ...to step through the list.

    so as you search through your linear linked list...
    first make a copy of the head pointer so you can get back to the beginning of the list.

    Then begin to search through each node of the list by stepping through the list in a while loop. You stop searching when you find the end of the list that you are searching through.

    in the loop, test for the equality. if(id==(*stud)->stu.ID)
    I think that's the correct useage of -> and . if your compiler complains, then I'm at least close.

    hope this gives you enough information to ask more questions or to send me flaming mail.

    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  3. #3
    C-dummy
    Guest
    I am quite clear about your explaining. Could anybody know how
    to code the Search() function and explain clearly what
    struct student **stud mean?

    C-dummy

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by C-dummy
    I am quite clear about your explaining. Could anybody know how
    to code the Search() function and explain clearly what
    struct student **stud mean?

    C-dummy
    stud is a pointer to a pointer to a struct of type student.
    hello, internet!

  5. #5
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    There's some good stuff on linked lists at
    http://cslibrary.stanford.edu/105/



    This was posted to one of my questions today.
    Your example is discussed there.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  6. #6
    C-dummy
    Guest
    I still struggling to code this Search() function. Any C expert out
    there could help?
    I think I will get a F on this program assignment for my C course.

    Here is I am trying to code:

    int Search(struct stuTable *T, int id, struct student **stud)
    {
    if (T->stu.ID == id)
    {
    *(stud->ID) = id;
    return 1;
    }
    else
    return 0;
    }

    I have tried my best, but I still vague understand about how
    to deal with struct student **stu. I am doubt about my coding
    for Search() function is correct, but that is just tryout.

    C-dummy

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    c-dummy, it seems like you're just having a bit of trouble with your english, because everyone is saying the right stuff. what is your native language?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    cdummy,

    assuming that Key id is a character string and that Struct B is a struct that looks like this:

    struct student{
    char* name;
    struct* student *NEXT;
    };

    ..and your main() calls the routine like this.
    Search(id, listexample); where 'id' is what you are looking for in the linked list and 'listexample' is the linked list
    Here is an example of a this search routine involving pointers.
    Code:
    int Search(Key id, Struct *B){
       B=B->NEXT;  //move in from the (dummy) head node
       while (B->next!=NULL){
          if(strcmp(B->name, id)){
             return 1;
          }
          else
          {
             B=B->NEXT;
          }
       }
       return 0;
    }
    now, in the case that function call is
    Search (id, &listexample);
    which is similar to your problem...

    This means that you have to double dereference the pointer
    in the function definition.
    int Search(Key id, Struct **B){
    *B=(*B)->NEXT; //move in from the (dummy) head node
    while ((*B)->next!=NULL){
    if(strcmp((*B)->name, id)){
    return 1;
    }
    else
    {
    *B=(*B)->NEXT;
    }
    }
    return 0;
    }

    essentially, I just wrote the essence of your search routine for you.

    lastly, if you don't know why the first step I did in the example was to move in one link from the dummy node, then you still have alot to learn about simple lists.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

Popular pages Recent additions subscribe to a feed