Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Not as expected. (http://www.velocityreviews.com/forums/t742793-not-as-expected.html)

dmjcunha 01-29-2011 09:07 PM

Not as expected.
 
Hi guys. I have a simple program and I hope you can help me. The code
is below. The problem is that I was expecting the second "head->next"
printf statement not to be nil. Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

typedef struct node *link;
struct node {
char charctr;
link next; };

static link head = NULL;

void dputatbeg(char t)
{
link x;

x = malloc(sizeof *x);
x->charctr = t;
x->next = head;
head = x;
}

void dputatend(char t)
{
link x;

x = head;
if(head == NULL)
head = (x = malloc(sizeof *x));
else {
while(x != NULL)
x = x->next;
x = malloc(sizeof *x);
}
x->charctr = t;
x->next = NULL;
}

int main(void)
{
char c;

if(c = getchar()) {
dputatbeg(c);
printf("head %p\t", head);
printf("head->next %p\n", head->next);
}

if(c = getchar()) {
dputatend(c);
printf("head %p\t", head);
printf("head->next %p\n", head->next);
}

return 1;
}

kid joe 01-29-2011 09:26 PM

Re: Not as expected.
 
dmjcunha writes:
> void dputatend(char t)
> {
> link x;
>
> x = head;
> if(head == NULL)
> head = (x = malloc(sizeof *x));
> else {
> while(x != NULL)
> x = x->next;
> x = malloc(sizeof *x);
> }
> x->charctr = t;
> x->next = NULL;
> }


Hi dmjcunha

The "else" part should be:

else {
while(x->next != NULL)
x = x->next;
x->next = malloc(sizeof *x);
x = x->next;
}

~~ joe


--


( )
( )
G O O D ( ) M O R N I N G ! !
( )
) )
( ( /\
(_) / \ /\
________[_]________ /\/ \/ \
/\ /\ ______ \ / /\/\ /\/\
/ \ //_\ \ /\ \ /\/\/ \/ \
/\ / /\/\ //___\ \__/ \ \/
/ \ /\/ \//_____\ \ |[]| \
/\/\/\/ //_______\ \|__| \
/ \ /XXXXXXXXXX\ \
\ /_I_II I__I_\__________________\
I_I| I__I_____[]_|_[]_____I
I_II I__I_____[]_|_[]_____I
I II__I I XXXXXXX I
~~~~~" "~~~~~~~~~~~~~~~~~~~~~~~~


Eric Sosman 01-29-2011 09:35 PM

Re: Not as expected.
 
On 1/29/2011 4:07 PM, dmjcunha wrote:
> Hi guys. I have a simple program and I hope you can help me. The code
> is below. The problem is that I was expecting the second "head->next"
> printf statement not to be nil. Thanks in advance.
>
> [...]
> void dputatend(char t)
> {
> link x;
>
> x = head;
> if(head == NULL)
> head = (x = malloc(sizeof *x));
> else {
> while(x != NULL)
> x = x->next;
> x = malloc(sizeof *x);
> }
> x->charctr = t;
> x->next = NULL;
> }
> [...]


A really good thing to do when you're confused about what's
happening with a linked data structure is to draw "before" and
"after" diagrams. This function handles two cases: Either `head'
is NULL and the list is empty (so the new node becomes the only
node), or `head' is non-NULL and the list is non-empty. Since
you've probably figured out that the function works properly for
the first case, let's draw pictures of the second. You want to
begin with

head -> [node1] -> [node2] -> ... -> [nodeN] -> NULL

.... and and end up with

head -> [node1] -> [node2] -> ... -> [nodeN] -> [nodeX] -> NULL

Comparing the arrows in the two diagrams, you can see that two
changes must occur: The new nodeX must point to NULL, and the old
nodeN must point to nodeX. Two arrows must change, so the function
must store new values in two `next' links in nodes. Count the number
of such stores the function actually performs, and perhaps you'll be
on your way to seeing what's wrong.

--
Eric Sosman
esosman@ieee-dot-org.invalid


All times are GMT. The time now is 08:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.