Write a function to create a one -way dynamic linked list with 3 student data.
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 (void)//define function returns a pointer to a linked header
{
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);
}
int main ()
{
struct Student *pt;
PT = creat ();//function returns the address of the 1th node of the list.
printf ("\nnum=%ld\nscore=%5.1f\n", Pt->num, Pt->score);//Output The member value of the 1th node
return 0;
}
Results:
1001,67
1003,89
1005,99
0,0
num=1001
Score= 67.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/1750276
C Language: Write a function create a one-way dynamic linked list with 3 student data