Not as expected.

D

dmjcunha

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;
}
 
K

kid joe

dmjcunha said:
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
~~~~~" "~~~~~~~~~~~~~~~~~~~~~~~~
 
E

Eric Sosman

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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top