c - Linked List - data in previous nodes is being overwritten -
i have program puts directories in linked list , prints it. linked list getting messed nodes having same data. example, have folder "test' has 3 subfolders "good one", "jump", "sunday". output -> sunday sunday sunday.
on debug, first iteration of insert function, variable data in 1st node set "good one". address of root node pointer passed main. initially, startptr->data = "good one". after readdir executed, dir->d_name "jump".but startptr->data getting changed "jump". same happens 3rd "sunday" , nodes in list have data = "sunday".
not sure why happening. appreciate on this. here code:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> /* self-referential structure */ struct listnode { char *data; /* each listnode contains character */ struct listnode *nextptr; /* pointer next node */ }; /* end structure listnode */ typedef struct listnode listnode; /* synonym struct listnode */ typedef listnode *listnodeptr; /* synonym listnode* */ /* prototypes */ void insert( listnodeptr *sptr, char *value ); void printlist( listnodeptr currentptr ); int main( void ) { listnodeptr startptr = null; /* there no nodes */ dir *d = null; struct dirent *dir = null; d = opendir ("/users/satish/documents/test"); if (d != null) { while ((dir = readdir (d)) != null) { /*printf("%s\n", dir->d_name);*/ if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) { } else insert( &startptr, dir->d_name ); /* insert item in list */ } closedir (d); } else printf("%s/n", "couldn't open directory"); printlist( startptr ); printf( "end of run.\n" ); return 0; /* indicates successful termination */ } /* end main */ /* insert new value list in sorted order */ void insert( listnodeptr *sptr, char *value ) { listnodeptr newptr = null; /* pointer new node */ listnodeptr previousptr = null; /*pointer previous node in list */ listnodeptr currentptr = null; /* pointer current node in list */ printf("%s\n", value); newptr = malloc( sizeof( listnode ) ); /* create node */ if ( newptr != null ) /* space available */ { newptr->data = value; /* place value in node */ newptr->nextptr = null; /* node not link node */ previousptr = null; currentptr = *sptr; if (currentptr == null) *sptr = newptr; else { while ( currentptr != null ) { previousptr = currentptr; currentptr = currentptr->nextptr; } previousptr->nextptr = newptr; newptr->nextptr = null; } } else { printf( "%c not inserted. no memory available.\n", value ); } /* end else */ } /* end function insert */ /* print list */ void printlist( listnodeptr currentptr ) { /* if list empty */ if ( currentptr == null ) { printf( "list empty.\n\n" ); } /* end if */ else { printf( "the list is:\n" ); /* while not end of list */ while ( currentptr != null ) { printf( "%s\n", currentptr->data ); currentptr = currentptr->nextptr; } /* end while */ printf( "null\n\n" ); } /* end else */ } /* end function printlist */
output is:
the list is: sunday sunday sunday
in insert
function pointing same address elements have allocate memory store name. see line 57 of code , add this:
newptr = malloc( sizeof( listnode ) ); /* create node */ if ( newptr != null ) /* space available */ { char *val=malloc((strlen(value)+1)*1); strcpy(val,value); newptr->data = val; /* place value in node */ newptr->nextptr = null; . . .
Comments
Post a Comment