Small example of a single-chain table (3) reading and saving linked lists, and single-chain examples
#include <stdio.h> #include <malloc.h> #include <string.h> struct address { int a; int b; char c; struct address *next; }; void SaveToFile(struct address *p, FILE *fp) { if(p != NULL) { do { fwrite(p, sizeof(struct address), 1, fp); p = p->next; } while(p != NULL); } } int load(FILE *fp, struct address **head){int n=0; struct address *p1,*p2; *head = (struct address *) malloc(sizeof(struct address)); memset(*head, 0, sizeof(struct address)); if( fread(*head, sizeof(struct address), 1, fp) != 1) { free(*head); *head = NULL; return(0); } p2 = *head; n++; while( !feof(fp)) { p1 = (struct address *) malloc(sizeof(struct address)); fread(p1, sizeof(struct address), 1, fp); p2->next = p1; p2 = p1; n++; } p2->next = NULL; return(n); } void main() { struct address *head; struct address *Test[10] = {0}; int i = 0; FILE *fp = NULL; for(i=0; i < 10; i++) { Test[i] = (struct address *)malloc(sizeof(struct address)); if(Test[i] != NULL) { memset(Test[i], 0, sizeof(struct address)); Test[i]->a = 65 + i; Test[i]->b = 65 + i; } } for(i = 0; i < 10; i++) { if(i < 10) { Test[i]->next = Test[i+1];} } if((fp = fopen("addrbook.txt", "wb")) != NULL) { SaveToFile(Test[0], fp); fclose(fp); } if((fp = fopen("addrbook.txt", "rb")) != NULL) { load(fp, &head); } }
How to store data and read data in a C-language one-way linked list
For a while, I wrote a simple example. The example succeeded in vs2005. test.txt is the file name, which is in the current directory.
# Include <stdio. h>
# Include <stdlib. h>
# Define TRUE 1
# Define FALSE 0
Typedef struct Node
{
Int num;
Int score;
Struct Node * next;
} Node, * Linklist;
Void InitLinklist (Linklist * L) // initialize a single-chain table to create a linked list of empty leading nodes.
{
* L = (Node *) malloc (sizeof (Node ));
(* L)-> next = NULL;
}
Void CreateLinklist (Linklist L) // create a single-chain table by means of end insertion
{
Node * r, * s;
R = L;
Int iNum, iScore;
Printf ("Please input the number and score: \ n ");
Scanf ("% d", & iNum );
Scanf ("% d", & iScore );
While (0! = IScore) // when the score is negative, end the input
{
S = (Node *) malloc (sizeof (Node ));
S-> num = iNum;
S-> score = iScore;
R-> next = s;
R = s;
Printf ("Please input the number and score: \ n ");
Scanf ("% d", & iNum );
Scanf ("% d", & iScore );
}
R-> next = NULL; // set the pointer field of the last node to NULL.
}
Int WriteLinklistToFile (const char * strFile, Linklist L)
{
FILE * fpFile;
Node * head = L-> next;
If (NULL = (fpFile = fopen (strFile, "w") // open it in write mode
{
Printf ("Open file failed \ n ");
Return FALSE;
}
While (NULL! = Head)
{
Fprintf (fpFile, "% d \ t % d \ n", head-> num, head-> score );
Head = head-> next;
}
If (NULL! = FpFile)
Fclose (fpFile );
Return TRUE;
};
Int ReadFromFile (const char * strFile)
{
FILE * fpFile;
If (NULL = (fpFile = fopen (strFile, "r") // open it in Read mode
{
Printf ("Open file failed \ n ");
Retur ...... remaining full text>
Link nodes a, B, and c into a one-way linked list and output data in the linked list nodes.
How to link? Order? Reverse Order? Increment...