S
Steve Chow
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;
while(link->dates != NULL)
link->dates = link->dates->next;
link->dates = malloc(sizeof(struct dates));
link->dates->m= m;
link->dates->d = d;
link->dates->y = y;
link->dates->next = NULL;
}
void
print_dates(struct database *head)
{
struct database *tmap = head;
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;
}
}
int
main(int argc, char **argv)
{
struct database *mdatabase;
mdatabase = malloc(sizeof(struct database));
mdatabase->dates = malloc(sizeof(struct dates));
mdatabase->dates->next = NULL;
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;
}
thank you
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;
while(link->dates != NULL)
link->dates = link->dates->next;
link->dates = malloc(sizeof(struct dates));
link->dates->m= m;
link->dates->d = d;
link->dates->y = y;
link->dates->next = NULL;
}
void
print_dates(struct database *head)
{
struct database *tmap = head;
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;
}
}
int
main(int argc, char **argv)
{
struct database *mdatabase;
mdatabase = malloc(sizeof(struct database));
mdatabase->dates = malloc(sizeof(struct dates));
mdatabase->dates->next = NULL;
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;
}
thank you