C链表-C语言入门经典例题
struct student
{
long num;
float score;
struct student *next;
};
注意:只是定义了一个struct student类型,并未实际分配存储空间。只有定义了变量才分配内存单元。
#include<iostream>
using namespace std;
int main() {
struct student a,b,c,*head,*p;
a.num = 99101;
a.score = 89.5;
b.num = 99103;
b.score = 90;
c.num = 99107;
c.score = 85; /*对结点的num和score成员赋值*/
head = &a; /*将结点a的起始地址赋给头指针head*/
a.next = &b; /*将结点b的起始地址赋给a结点的next成员*/
b.next = &c; /*将结点c的起始地址赋给b结点的next成员*/
c.next = NULL; /*c结点的next成员不存放其他结点地址*/
p = head; /*使p指针指向a结点*/
do {
cout<<p->num<<" "<<p->score; /*输出p指向的结点的数据*/
p=p->next; /*使p指向下一结点*/
} while (p!=NULL); /*输出完c结点后p的值为NULL*/
return 0;
}
C语言入门经典 单链表!
/* Program 11.4 Daisy chaining the horses */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h> int main(void)
{
struct horse /* Structure declaration */
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
struct horse *next; /* Pointer to next structure */
}; struct horse *first = NULL; /* Pointer to first horse */
struct horse *current = NULL; /* Pointer to current horse */
struct horse *previous = NULL; /* Pointer to previous horse */ char test = '\0'; /* Test value for ending input */ for( ; ; )
{
printf("\nDo you want to enter details of a%s horse (Y or N)? ",
first != NULL?"nother " : "" );
scanf(" %c", &test );
if(tolower(test) == 'n')
break; /* Allocate memory for a structure */
current = (struct horse*) malloc(sizeof(struct horse)); if(first == NULL)
first = current; /* Set pointer to first horse */ if(previous != NULL) previous -> next = current; /* Set next pointer for previous horse */ printf("\nEnter the name of the horse: ");
scanf("%s", current -> name); /* Read the horse's name */ printf("\nHow old is %s? ", current -> name);
scanf("%d", ¤t -> age); /* Read the horse's age */ printf("\nHow high is %s ( in hands )? ", current -> name );
scanf("%d", ¤t -> height); /* Read the horse's height */ printf("\nWho is %s's father? ", current -> name);
scanf("%s", current -> father); /* Get the father's name */ printf("\nWho is %s's mother? ", current -> name);
scanf("%s", current -> mother); /* Get the mother's name */ current->next = NULL; /* In case it's the last... */
previous = current; /* Save address of last horse */
} /* Now tell them what we know. */
current = first; /* Start at the beginning */ while (current != NULL) /* As long as we have a valid pointer */
{ /* Output the data*/
printf("\n\n%s is %d years old, %d hands high,",
current->name, current->age, current->height);
printf(" and has %s and %s as parents.", current->father,
current->mother);
previous = current; /* Save the pointer so we can free memory */
current = current->next; /* Get the pointer to the next */
free(previous); /* Free memory for the old one */
}
return 0;
}
双向链表:
/* Program 11.5 Daisy chaining the horses both ways */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h> int main(void)
{
struct horse /* Structure declaration */
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
struct horse *next; /* Pointer to next structure */
struct horse *previous; /* Pointer to previous structure */
}; struct horse *first = NULL; /* Pointer to first horse */
struct horse *current = NULL; /* Pointer to current horse */
struct horse *last = NULL; /* Pointer to previous horse */ char test = '\0'; /* Test value for ending input */ for( ; ; )
{
printf("\nDo you want to enter details of a%s horse (Y or N)? ",
first == NULL?"nother " : "");
scanf(" %c", &test );
if(tolower(test) == 'n')
break; /* Allocate memory for each new horse structure */
current = (struct horse*)malloc(sizeof(struct horse)); if( first == NULL )
{
first = current; /* Set pointer to first horse */
current->previous = NULL;
}
else
{
last->next = current; /* Set next address for previous horse */
current->previous = last; /* Previous address for current horse */
} printf("\nEnter the name of the horse: ");
scanf("%s", current -> name ); /* Read the horse's name */ printf("\nHow old is %s? ", current -> name);
scanf("%d", ¤t -> age); /* Read the horse's age */ printf("\nHow high is %s ( in hands )? ", current -> name);
scanf("%d", ¤t -> height); /* Read the horse's height */ printf("\nWho is %s's father? ", current -> name);
scanf("%s", current -> father); /* Get the father's name */ printf("\nWho is %s's mother? ", current -> name);
scanf("%s", current -> mother); /* Get the mother's name */ current -> next = NULL; /* In case it's the last horse..*/
last = current; /* Save address of last horse */
} /* Now tell them what we know. */
while(current != NULL) /* Output horse data in reverse order */
{
printf("\n\n%s is %d years old, %d hands high,",
current->name, current->age, current->height);
printf(" and has %s and %s as parents.", current->father,
current->mother);
last = current; /* Save pointer to enable memory to be freed */
current = current->previous; /* current points to previous in list */
free(last); /* Free memory for the horse we output */
}
return 0;
}
C链表-C语言入门经典例题的更多相关文章
- c语言入门经典(第5版)
文章转载:http://mrcaoyc.blog.163.com/blog/static/23939201520159135915734 文件大小:126MB 文件格式:PDF [点击下载] C ...
- C语言学习书籍推荐《C语言入门经典(第4版)》
霍顿 (Ivor Horton) (作者), 杨浩 (译者) <C语言入门经典(第4版)>的目标是使你在C语言程序设计方面由一位初学者成为一位称职的程序员.读者基本不需要具备任何编程知识, ...
- C语言学习书籍推荐《C语言入门经典(第5版)》下载
霍尔顿 (Ivor Horton) (作者), 杨浩 (译者) 下载地址:点我 C语言是每一位程序员都应该掌握的基础语言.C语言是微软.NET编程中使用的C#语言的基础:C语言是iPhone.iPad ...
- C语言入门经典书目推荐--转
国内良莠不齐的C语言教程数不胜数,同名如"C程序设计""C语言程序设计""C语言程序设计教程"的都多如牛毛,这些不知名的就不予考虑了,要看就 ...
- c语言入门教程 / c语言入门经典书籍
用C语言开始编写代码初级:C语言入门必备(以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言的数 ...
- 【转】c语言入门教程 / c语言入门经典书籍
用C语言开始编写代码 初级:C语言入门必备 (以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言 ...
- c语言入门经典必背18个程序
1 . /* 输出 9*9 口诀.共 9 行 9 列, i 控制行, j 控制列. */ #include "stdio.h" main() {int i,j,result; fo ...
- C语言入门经典题目及其答案
写在开始: 我叫风骨散人,名字的意思是我多想可以不低头的自由生活,可现实却不是这样.家境贫寒,总得向这个世界低头,所以我一直在奋斗,想改变我的命运给亲人好的生活,希望同样被生活绑架的你可以通过自己的努 ...
- Java实现算法竞赛入门经典例题-蚂蚁
问题描述 一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒. 当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计). 给出每只蚂蚁的初始位置和朝向,计算T秒之后每只蚂 ...
随机推荐
- TurtleBot3 Waffle (tx2版华夫)(10)自主导航(A2激光雷达)
1)[Remote PC] 启动roscore $ roscore 2)[TurBot3] 启动turbot3 $ roslaunch turbot3_bringup minimal.launch 3 ...
- ES6 class类 静态方法及类的继承
一.class类 ES6之前都是定义函数以及函数的原型对象实现类型, 如果想要实现共享构造函数成员,可以用prototype来共享实现 ES6出现之后,使用class类的概念来实现原型的继承 二,静态 ...
- [开源软件] 腾讯云Linux服务器一键安装LAMP/LNMP/LANMP环境 转
本帖最后由 我本戏子 于 2015-8-13 22:00 编辑OneinStack是非常优秀的一键PHP/JAVA安装脚本,提供以下环境:lnmp(Linux + Nginx+ MySQL+ PHP) ...
- SpringBoot整合Shiro完成认证
三.SpringBoot整合Shiro思路 首先从客户端发来的所有请求都经过Shiro过滤器,如果用户没有认证的都打回去进行认证,认证成功的,再判断是否具有访问某类资源(公有资源,私有资源)的权限,如 ...
- Python基础语法6-冒泡排序
用for循环实现冒泡排序(升序): array = [3,2,1] for i in range(len(array) - 1, 0, -1): for j in range(0, i): if ...
- 【Linux】rsync中sending incremental file list时间优化
每次使用rsync的时候,前面出现sending incremental file list 这句之后要等待很长时间 查了很多帖子和官方文档后,发现是-c这个选项的问题, -v, --verbose ...
- kubernets之服务的实现方式
一 服务如何通过kubernetes集群的组件来实现其功能 1.1 节点上的所有的服务相关的功能实现都是通过节点上面的kube-proxy来实现的,服务提供了一个或者多个服务IP以及端口对客户端开 ...
- poj-DNA排序
描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A ...
- 到底什么是哈希Hash?
有次面试被问到这个问题? 我说是经过运算的一串字符串,这个回答显然是让人不满意,连自己都不满意! 但是又对其很模糊,那么到底什么是Hash呢? 定义 Hash一般翻译为散列,还有音译为哈希,本文我们统 ...
- Netty的简单Demo
这个demo是通过网上下载: 使用maven构建的: 项目结构: pom.xml: <dependencies> <dependency> <groupId>io. ...