linked list and strings

hi..
can some1 plz tel me how do i store each character of a string in a linked list using C
and not C++..
Last edited on
Two choices.

Every node must contain as its data element either a fixed length array (if you know
the maximum length of the string) or a char* (pointer to a dynamically allocated
array large enough to hold the string).

thanx jsmith, i hav created a structure like this..
struct node
{
char info;
struct node * next;
};
typedef struct node* NODEP;

i am able to insert only the 1st character..
later i dunno how to go to a new node..
this is my code.. please help..
/***************/
#include <stdio.h>
#include <string.h>
#include <conio.h>
struct node
{
char info;
struct node * next;
};
typedef struct node* NODEP;

NODEP getnode(void)
{
// to allocate mem for the new node.
NODEP p;
p=(NODEP)malloc(sizeof(struct node));
return p;
}

NODEP insnode(NODEP first, char str)
{
NODEP p,q;
p=first;


if(first==NULL)
{
p=getnode();
}
q=p;

while(q!=NULL)
q=q->next;
q=getnode();

q->info=str;
q->next=NULL;
//first=p;
return first;
}


int main()
{
NODEP temp,p,q,first=NULL;
char str[]="Hello";
int i;
clrscr();
for(i=0;str[i]!='\0';i++)
{
first=insnode(first, str[i]);
}
printf("\n\nList contains\n\n");
while(temp != '\0')
{
printf("%c",temp->info);
temp=temp->next;
}
getch();
return 0;
}

/********************/
insnode appears to create the list if it doesn't already exist, so the pointer passed in should be passed by reference.

insnode has the code:
1
2
3
q=p;
while(q!=NULL)
    q=q->next;

doesn't really do anything.

1
2
3
4
q=getnode();
q->info=str;
q->next=NULL;
return first;

Creates doesn't link the new node to anything, it creates garbage.
Topic archived. No new replies allowed.