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();
}