Write a function printfor the output list.
Solution: Program:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof (struct Student)
struct Student
{
Long num;
Float score;
struct Student *next;
};
int n;
struct Student *creat ()//function to create a linked list
{
struct Student *head;
struct Student *p1, *p2;
n = 0;
P1 = P2 = (struct Student *) malloc (LEN);
scanf ("%ld,%f", &p1->num, &p1->score);
head = NULL;
while (P1->num! = 0)
{
n = n + 1;
if (n = = 1)
{
head = p1;
}
Else
{
P2->next = p1;
}
P2 = p1;
P1 = (struct Student *) malloc (LEN);
scanf ("%ld,%f", &p1->num, &p1->score);
}
P2->next = NULL;
return (head);
}
void print (struct Student *head)//function of the output list
{
struct Student *p;
printf ("\nnow,these%d records are:\n", N);//records record
p = head;//p points to 1th node
if (head! = NULL)//Is not an empty table
{
Do
{
printf ("%ld%5.1f\n", P->num, P->score);
p = p->next;
} while (P! = NULL);
}
}
void Main ()
{
struct Student *head;
Head= creat ();//Call function returns the address of the 1th node of the list.
Print (head);
}
Results:
1001,77
1003,88
1005,99
0,0
Now,these 3 Records is:
1001 77.0
1003 88.0
1005 99.0
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1750277
C Language: Write a function print for the output list