In school, learned a little C, but the level is very low, now learn to feel very laborious, but every day to learn a little, or very happy. Programming this thing, write it, think about it.
All of the following code is written in the Vim editor in the Linux environment and debugged through GCC.
The idea of writing the contents of a file into a linked list:
(1) First we need to build a file, in the example I used Stu_data.txt, create a file *fp pointer to the file
(2) We know to write the contents of the file to the list, in fact very similar to our usual, the standard input and output to the linked list. So as long as in the operation of the document, complete the linked list of the building
Stand on the good.
Realize:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct STU
{
int data;
struct stu* next;
};
typedef struct STU Snode;
typedef struct STU * SLINK;
Slink head = NULL;
Slink Receive_txt ()
{
FILE *FP;
fp = fopen ("Stu_data.txt", "R");
if (fp = NULL)
{
printf ("Open error.../n");
Exit (0);
}
Rewind (FP);
Slink h = NULL;
Slink r = NULL;
Slink s =null;
while (FGETC (FP)!=eof)
{
s = malloc (sizeof (Snode);
FSCANF (FP, "%d", s->data);
if (H ==null)
{
h = s;
R = S;
}
Else
{
R->next = s;
R = S;
}
R->next = NULL;
}
return h;
}
void display (Slink head)
{
Slink p = head;
while (P!=null)
{
printf ("%d", p->data);
p = p->next;
}
}
void Main ()
{
Head = Receive_txt ();
Display (head);
}