470,862 Members | 1,852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

LL help

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

May 30 '06 #1
4 1572
In article <11**********************@j33g2000cwa.googlegroups .com>,
Steve Chow <ms*********@hotmail.com> wrote:
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 .
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
May 30 '06 #2
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.

May 30 '06 #3
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.
May 30 '06 #4
Thank you, Michael.

May 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
6 posts views Thread by wukexin | last post: by
3 posts views Thread by Colin J. Williams | last post: by
7 posts views Thread by Corepaul | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.