Steve Chow schrieb:
I was wondering if someone could help with my assignment. I've pretty
done finished the work but cannot figure out why it only prints the
last one added. Here is my code. The way things are done might not make
much sense but it's part of the requirement.
#include <stdio.h>
#include <stdlib.h>
//Meetings
struct dates
{
int m;
int d;
int y;
struct dates *next;
};
//Employee
struct database
{
char *name;
struct dates *dates;
};
int
add_dates(struct database *tdatabase, int m, int d, int y)
{
struct database *link = tdatabase;
You are duplicating the address of a database, i.e. everything
you do to *link also happens to *tdatabase; in other words:
In the following, you could use tdatabase instead of link.
while(link->dates != NULL)
link->dates = link->dates->next;
You have a memory leak as you lose all previously non-null
struct dates pointers in the list.
link->dates = malloc(sizeof(struct dates));
link->dates->m= m;
link->dates->d = d;
link->dates->y = y;
link->dates->next = NULL;
You do not return a value even though you claimed that your
function returns int.
}
void
print_dates(struct database *head)
{
struct database *tmap = head;
Same mistake as above.
while(tmap->dates != NULL)
{
//MM-DD-YY
printf("%d-%d-%d",
tmap->dates->m, tmap->dates->d, tmap->dates->y);
tmap->dates = tmap->dates->next;
}
Your print function destroys your list.
}
int
main(int argc, char **argv)
{
struct database *mdatabase;
mdatabase = malloc(sizeof(struct database));
mdatabase->dates = malloc(sizeof(struct dates));
You forgot to check for malloc() success. mdatabase->dates->next = NULL;
This first element of the list is uninitialized and
unnecessary. If you call print_dates(), something
undefined happens.
add_dates(mdatabase, 12, 22, 86);
add_dates(mdatabase, 1, 19, 90);
add_dates(mdatabase, 6, 15, 88);
add_dates(mdatabase, 3, 4, 85);
add_dates(mdatabase, 4, 7, 84);
print_dates(mdatabase);
return 0;
}
Consider
int
add_dates(struct database *tdatabase, int m, int d, int y)
{
struct dates *newdate;
newdate = malloc(sizeof *newdate);
if (newdate == NULL) {
fprintf(stderr, "new date allocation failed\n");
return 1;
}
newdate->m = m;
newdate->d = d;
newdate->y = y;
newdate->next = NULL;
if (tdatabase->dates == NULL) {
tdatabase->dates = newdate;
} else {
struct dates *date = tdatabase->dates;
while (date->next != NULL)
date = date->next;
date->next = newdate;
}
return 0;
}
and
int
main (int argc, char **argv)
{
struct database *mdatabase;
mdatabase = malloc(sizeof(struct database));
if (mdatabase == NULL) { /* Always check malloc() success */
fprintf(stderr, "database allocation failed\n");
exit(EXIT_FAILURE);
}
add_dates(mdatabase, 12, 22, 86);
add_dates(mdatabase, 1, 19, 90);
add_dates(mdatabase, 6, 15, 88);
add_dates(mdatabase, 3, 4, 85);
add_dates(mdatabase, 4, 7, 84);
print_dates(mdatabase);
/* Free database entry storage and database storage here */
return 0;
}
as replacements of the above; replace print_dates() appropriately.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.