Thread: Linked List by recursion

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Unhappy Linked List by recursion

    I have made the below 3 functions. But I don't understand why I cannot print out the data from the list.

    Anyone can give me a big hand on this??? A million thks.

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

    struct alumniRecord {
    char lastName[32]; /* last name of the alumni */
    char firstName[32]; /* first name of the alumni */
    char id[8]; /* ID in the alumni association */
    int year; /* the Form 5 graduation year */
    struct alumniRecord* next; /* pointing to the next alumniRecord in the linked list */
    };

    typedef struct alumniRecord AlumniRecord;

    /* This function adds a new AlumniRecord to the end of the linked-list */
    AlumniRecord* addAlumniLast(AlumniRecord* records, char* lastName,
    char* firstName, char* id, int year) {

    /* stopping condition */
    if (records == NULL) {
    /* create a new record */
    records = (AlumniRecord*)malloc(sizeof(AlumniRecord));
    strcpy(records->lastName, lastName);
    strcpy(records->firstName, firstName);
    strcpy(records->id, id);
    records->year = year;
    records->next = NULL;
    return records;
    }
    /* recursive step */
    else {
    addAlumniLast(records->next, lastName, firstName, id, year);
    }
    return records;
    }

    /**
    This function returns the year of graduation of the alumni given the
    first name and the last name
    It returns -1 if the given name does not exist
    */
    int getYearOfAlumni(AlumniRecord* records, char* lastName, char* firstName) {
    if(records == NULL)
    return -1;
    else {
    if ((strcmp(records->lastName, lastName))&&(strcmp(records->firstName, firstName)))
    return (records->year);
    else
    return getYearOfAlumni(records->next, lastName, firstName);
    }
    }


    /**
    This function adds a new AlumniRecord in the ascending order sorted by
    first the graduation year and then the member ID
    */
    AlumniRecord* addAlumniInOrder(AlumniRecord* records, char* lastName,
    char* firstName, char* id, int year) {
    /* stopping condition */
    if (records == NULL) {
    /* create a new record */
    records = (AlumniRecord*)malloc(sizeof(AlumniRecord));
    strcpy(records->lastName, lastName);
    strcpy(records->firstName, firstName);
    strcpy(records->id, id);
    records->year = year;
    records->next = NULL;
    return records;
    }
    /* recursive step */
    else {
    if ((year <= records->year)&&(id <= records->id)) {
    AlumniRecord* temp;
    temp = (AlumniRecord*)malloc(sizeof(AlumniRecord));
    strcpy(temp->lastName, lastName);
    strcpy(temp->firstName, firstName);
    strcpy(temp->id, id);
    temp->year = year;
    temp->next = records->next;
    records->next = temp;
    }
    else
    addAlumniLast(records->next, lastName, firstName, id, year);
    }
    return records;

    }

    void main() {
    AlumniRecord* records = NULL;
    AlumniRecord* rec = NULL;
    records = addAlumniLast(records, "Smith", "Jane", "12345", 2000);
    printf("%s\n%s\n%s\n%d\n%s\n", records->lastName, records->firstName, records->id, records->year, records->next);
    records = addAlumniLast(records, "Willi", "Ken", "23456", 2001);
    rec = records->next;
    printf("%s\n%s\n%s\n%d\n%s\n", rec->lastName, rec->irstName, rec->id, rec->year, rec->next);

    getchar();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf("%s\n%s\n%s\n%d\n%s\n",
    records->lastName,
    records->firstName,
    records->id,
    records->year,
    records->next
    );
    Why are you using "%s" to print out "records->next"? If this is
    a NULL pointer, this is a "BadThing(TM)". How about:

    printf("%s\n%s\n%s\n%d\n%d\n",
    records->lastName,
    records->firstName,
    records->id,
    records->year,
    records->next
    );
    This will print the address.

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

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    Hi

    Dear programmer ,
    The first and main problem of ur program is that u did not linked
    your lists.
    this means that when u define a new structure u have to linked
    the previous one to it
    u can solve ur problem just by adding this line between the
    lines specified by "//":


    //printf("%s\n%s\n%s\n%d\n%s\n", records->lastName, records-//>firstName
    //, records->id, records->year, records->next);
    rec = records;
    //records = addAlumniLast(records, "Willi", "Ken", "23456", 2001);
    rec->next = records;
    //printf("%s\n%s\n%s\n%d\n%s\n", rec->lastName, rec-//>firstName, rec->id, rec->year, rec->next);
    by the way u do not have to print out the address ->next
    bye

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Dear dumbasses,

    Please USE CODE TAGS! While you're at it, read all of the forum rules. You may want to pay close attention to the "don't bump old threads" section.

    Best regards,

    Quzah.

    PS: Have some candy.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM