If this is a single linked list with a head pointer
How can I change the pointer to point to Tail?
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXLINES 50 #define MAXWORDLENGTH 50 FILE *ifp; struct word_link { char word[MAXWORDLENGTH]; struct word_link *pnext; }; int main(void) { const char ifn[]={"word.txt"}; char line[1+MAXLINES]; struct word_link *phead,*pthis,*pnew; ifp=fopen(ifn,"r"); if (ifp==NULL) { perror(ifn); exit(1); } phead=NULL; for(;;) { fscanf(ifp,"%s", line); if (feof(ifp)) break; if ((pnew=(struct word_link *)malloc(sizeof(struct word_link)) )==NULL) { fprintf(stderr,"link1: no storage available\n"); exit(1); } strcpy(pnew->word,line); pnew->pnext=phead; phead=pnew; } for(pthis=phead;pthis!=NULL;pthis=pthis->pnext) printf("%p %s\n",pthis,pthis->word); free(pnew); fclose(ifp); return 0; }



LinkBack URL
About LinkBacks


