c语言:编写一个输出链表的函数print
2016-05-29 13:41
387 查看
编写一个输出链表的函数print。
解:程序:
#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()//建立链表的函数
{
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)//输出链表的函数
{
struct Student *p;
printf("\nNow,These %d records are:\n",n);//records记录
p = head;//p指向第1个结点
if (head != NULL)//若不是空表
{
do
{
printf("%ld %5.1f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}
}
void main()
{
struct Student *head;
head= creat();//调用函数返回链表第1个结点的地址
print(head);
}
结果:
1001,77
1003,88
1005,99
0,0
Now,These 3 records are:
1001 77.0
1003 88.0
1005 99.0
请按任意键继续. . .
本文出自 “岩枭” 博客,请务必保留此出处http://yaoyaolx.blog.51cto.com/10732111/1750277
解:程序:
#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()//建立链表的函数
{
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)//输出链表的函数
{
struct Student *p;
printf("\nNow,These %d records are:\n",n);//records记录
p = head;//p指向第1个结点
if (head != NULL)//若不是空表
{
do
{
printf("%ld %5.1f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}
}
void main()
{
struct Student *head;
head= creat();//调用函数返回链表第1个结点的地址
print(head);
}
结果:
1001,77
1003,88
1005,99
0,0
Now,These 3 records are:
1001 77.0
1003 88.0
1005 99.0
请按任意键继续. . .
本文出自 “岩枭” 博客,请务必保留此出处http://yaoyaolx.blog.51cto.com/10732111/1750277
相关文章推荐
- c语言:通过指针变量访问整型变量
- c语言:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法?
- c语言:用8个2*1的小矩形横着或竖着无重叠地覆盖一个2*8的大矩形,总共有多少种方法?
- c语言:输入两个正整数m和n,求其最大公约数和最小公倍数
- c语言:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
- c语言:∑k(k取[1,100])+∑k^2(k取[1,50])+∑1/k(k取[1,10])的和
- c语言:编程找出1000之内的所有完数。
- c语言:两个乒乓球队进行比赛,各出3人,A说他不和x比,C说他不和x,z比。找出3对赛手的名单
- c语言:用迭代法求x=a^0.5,迭代公式为x1 = (x0 + a / x0) / 2;前后两次求出的x的差的绝对值小于10^-5
- c语言:用牛顿迭代法求方程在1.5附近的根:2x^3-4x^2+3x-6=0.
- c语言:用二分法求方程在(-10,10)之间的根:2x^3-4x^2+3x-6=0.
- c语言:把只含因子2、3和5的数称为丑数,求按从小到大的顺序的第1500个丑数(两种方法比较)
- c语言:对10个数组元素依次赋值为0,1,2,3,4,5,6,7,8,9,要求按逆序输出。
- c语言:用数组来处理求Fibonacci数列问题
- c语言:有10个地区的面积,要求它们按从小到大的顺序排列。(起泡法的应用)
- c语言:将一个二维数组行和列的元素互换,存到另一个二维数组中。
- c语言:3*4的矩阵,编程序求出其中最大的那个元素的值,以及其所在的行号和列号。(打擂台算法)
- c语言:输出一个菱形图
- c语言:输入一行字符,统计其中有多少个单词,单词之间用空格分隔开
- c语言:有3个字符串,找出其中最大者