1#include"getwhitelist_demo.h"2 3 voidInitialize_list (List *l)4 {5*l= (List)malloc(sizeof(Node));6(*l)->next=NULL;7 }8 9 voidInsert (elemtype x,list L)Ten { OneList tmpcell,p=L; ATmpcell = (List)malloc(sizeof(Node)); -Tmpcell->element= (Elemtype)malloc(Strlen (X) *sizeof(*x));///0001 - if(tmpcell==NULL) the return 0; - ///tmpcell->element=x;error0011 -strcpy (TMPCELL->ELEMENT,X);///0002 -tmpcell->next=NULL; + while(p->next!=NULL) -P=p->Next; +p->next=Tmpcell; A return 1; at } - -List getlists (Const Char*file) - { - List lst,p; -Initialize_list (&lst); inFILE *FP; - Charbuf[1024x768]; toFp=fopen (file,"rw+"); + if(!FP) -Perror ("File:"); the while(Fgets (BUF,1023, fp)! =NULL) * { $ Insert (buf,lst);Panax Notoginseng } - returnlst; the } + A voidprintout (List llst) the { + Position P; -P=llst->Next; $ while(p!=NULL) $ { -printf"Element:%s",p->Element); -P=p->Next; the } -}
View Code
1 #ifndef Getlists_demo2 #defineGetlists_demo3 4#include <stdio.h>5#include <string.h>6 7typedefChar*Elemtype;8 9typedefstructnode node;Tentypedefstructnode*List; One typedef List Position; A structNode - { - elemtype Element; the List Next; - }; - - #endif //Getlists_demo
View Code
Today made a mistake, in the first paragraph of the code is a error0001 error, here is just "repeated to the file to read the address of the character array," so that every time the list is printed to always the last value.
After replacing the error0001 with 0001 or 22 lines, the problem is solved because it is true that the value of all the files read is stored in the linked list, not just the repetitive copy of the pointer address (and a pointer address).
Linked list stores data read from a file