LL help

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
 
W

Walter Roberson

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.
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));

No matter what the result was of the while loop, you overwrite
link->dates with the malloc()'d pointer. You might as well not
have done the while loop in this code. If it seems to you that
the while loop is needed for some purpose, then you'll have
to figure out how to merge that purpose with the overwriting
of link->dates .
 
S

Steve Chow

I'm not sure I understand how to do this then. I was under the
impression that I was working my way up through the list until a hit a
NULL and then It'd quit the loop and I'd malloc it.

Perhaps my mind isn't clear.
 
M

Michael Mair

Steve said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top