Linked list stores data read from a file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.