C Language: Write a function create a one-way dynamic linked list with 3 student data

Source: Internet
Author: User
Tags define function

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

Related Article

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.