Thread: Linked Lists

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    12

    Linked Lists

    Hello!

    I got anassigmnet in linked lists and I need your help please.

    In this assignment, we deal with students that register
    to courses. We keep lists of students and lists of courses,
    where each student knows to which course they are registered,
    and each course also “knows” which students are
    registered to it.

    The structures student and course are informational:

    Code:
    typedef struct student {
    char *name;
    int id;
    struct clist *courses;
    } student;
    typedef struct course {
    char *title;
    int number;
    struct slist *students;
    } course;
    The structures slist and clist keep the data structure:

    Code:
    typedef struct slist {
    student *info;
    struct slist *next;
    } slist;
    typedef struct clist {
    course *info;
    struct clist *next;
    } clist;
    I need to implent a function that receive a list of students, a student ID and a
    course number (where each is guaranteed to exist in the
    respective list), unregister the student from the course. The function need to be defined in the following prototype:

    Code:
    void unreg_student(slist *students,int id, int number);
    How can I write the function??

    Thank you for your help in advance and merry X-mas!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    This seems to be an awkward way to store the information, the part where both you track students to courses and courses to students. Anyway, here is some code/pseudocode to give you an idea of how to do it
    Code:
    void unreg_student(slist *students,int id, int number)
      do a (linear) search for the given student id, 'id'
      do a (linear) search for the given course number, 'number', on the student struct found in the previous step
      remove this course from the list, using a "standard" way to delete items from a linked list
      (i.e. remove the "curr" item and fix the links so that prev->next points to curr->next, opposed to prev->next pointing to curr as it does now)
      
      finally remove this student from this list of courses

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    This was the same algorithem I tried to use but I have a problem to translate it to C code...
    Indeed it's an akward way for what the function should get but this is the assigment, to use excaly the same argument they implented... If only I could use the function with other arguments it was so much better

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Indeed it's an akward way for what the function should get but this is the assigment, to use excaly the same argument they implented... If only I could use the function with other arguments it was so much better
    Well the function arguments seem perfect. The awkward thing, as I said above, isnt with the function prototype (i.e. arguments) but with the fact that students track courses as well as courses tracking students. But, as you said, its an assignment so you must do it exactly as told.

    Because of that comment you made, I imagine your algorithm isnt the same as what I described above. So, again, I suggest you try and implement the algorithm I described above and let us know if you have logic or programming errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM