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