this program reads from standard input, storing each character into a linked list, then prints out characters stored in the list, but with space character changed into underscore "_".
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node nodeType;
struct node
{
char name;
nodeType *next;
};
nodeType*
chinsert(nodeType* ptr,char c)
{
nodeType *p;
nodeType *new = (nodeType *)(malloc(sizeof(struct node)));
new->name = c;
new->next = NULL;
if(ptr==NULL)
return new;
else
{
for(p=ptr; p->next!=NULL; p=p->next);
p->next= new;
return ptr;
}
}
void
prnList(nodeType *ptr)
{
nodeType *n = ptr;
printf("\n");
while(n != NULL)
{
printf("%c",n->name);
n = n->next;
}
printf("\n");
}
void
prn(nodeType *ptr)
{
nodeType *n = ptr;
while(n != NULL)
{
if(n->name == ' ')
printf("%c",n->name='_');
else
printf("%c",n->name);
n = n->next;
}
printf("\n");
}
void
cleanUp(nodeType *ptr)
{
nodeType *n;
while(ptr->next != NULL)
{
n = ptr->next;
ptr->next = n->next;
free(n);
}
n = ptr;
free(n);
}
int
main(int argc, char *argv[])
{
char c;
nodeType *ptr = NULL;
while((c=getchar())!=EOF)
{
ptr = chinsert(ptr,c);
}
prnList(ptr);
prn(ptr);
cleanUp(ptr);
return 0;
}
can anyone see why this doesnt work?