C Language: Write a function print for the output list

Source: Internet
Author: User

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

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.