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", &current -> age); /* Read the horse's age */ printf("\nHow high is %s ( in hands )? ", current -> name );
scanf("%d", &current -> 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", &current -> age); /* Read the horse's age */ printf("\nHow high is %s ( in hands )? ", current -> name);
scanf("%d", &current -> 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语言入门经典例题的更多相关文章

  1. c语言入门经典(第5版)

    文章转载:http://mrcaoyc.blog.163.com/blog/static/23939201520159135915734 文件大小:126MB 文件格式:PDF    [点击下载] C ...

  2. C语言学习书籍推荐《C语言入门经典(第4版)》

    霍顿 (Ivor Horton) (作者), 杨浩 (译者) <C语言入门经典(第4版)>的目标是使你在C语言程序设计方面由一位初学者成为一位称职的程序员.读者基本不需要具备任何编程知识, ...

  3. C语言学习书籍推荐《C语言入门经典(第5版)》下载

    霍尔顿 (Ivor Horton) (作者), 杨浩 (译者) 下载地址:点我 C语言是每一位程序员都应该掌握的基础语言.C语言是微软.NET编程中使用的C#语言的基础:C语言是iPhone.iPad ...

  4. C语言入门经典书目推荐--转

    国内良莠不齐的C语言教程数不胜数,同名如"C程序设计""C语言程序设计""C语言程序设计教程"的都多如牛毛,这些不知名的就不予考虑了,要看就 ...

  5. c语言入门教程 / c语言入门经典书籍

    用C语言开始编写代码初级:C语言入门必备(以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言的数 ...

  6. 【转】c语言入门教程 / c语言入门经典书籍

    用C语言开始编写代码 初级:C语言入门必备 (以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言 ...

  7. c语言入门经典必背18个程序

    1 . /* 输出 9*9 口诀.共 9 行 9 列, i 控制行, j 控制列. */ #include "stdio.h" main() {int i,j,result; fo ...

  8. C语言入门经典题目及其答案

    写在开始: 我叫风骨散人,名字的意思是我多想可以不低头的自由生活,可现实却不是这样.家境贫寒,总得向这个世界低头,所以我一直在奋斗,想改变我的命运给亲人好的生活,希望同样被生活绑架的你可以通过自己的努 ...

  9. Java实现算法竞赛入门经典例题-蚂蚁

    问题描述 一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒. 当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计). 给出每只蚂蚁的初始位置和朝向,计算T秒之后每只蚂 ...

随机推荐

  1. 第十二章节 BJROBOT 摄像头寻线 【ROS全开源阿克曼转向智能网联无人驾驶车】

    关于摄像头:普通摄像头, USB 免驱摄像头都可以使用. 1.如下图所示,用红色胶布在地板上贴一条线,小车摆放在线的一头处,让线在小车的中间位置,摄像头角度往下调整倾斜一点,好让摄像头识别到红线.注意 ...

  2. uniapp H5引入腾讯地图

    在网上搜索了许多关于uniapp引入腾讯地图的方法都以失败告终,我开发的应用主要使用于H5,小程序与H5是不同的sdk,就不在这说了,况且小程序有手把手教学,可参考腾讯地图官网https://lbs. ...

  3. 【并发编程】- 内存模型(针对JSR-133内存模型)篇

    并发编程模型 1.两个关键问题 1)线程之间如何通信 共享内存 程之间共享程序的公共状态,通过写-读内存中的公共状态进行隐式通信 消息传递 程之间没有公共状态,线程之间必须通过发送消息来显式进行通信 ...

  4. 深入理解Kafka必知必会(1)

    Kafka的用途有哪些?使用场景如何? 消息系统: Kafka 和传统的消息系统(也称作消息中间件)都具备系统解耦.冗余存储.流量削峰.缓冲.异步通信.扩展性.可恢复性等功能.与此同时,Kafka 还 ...

  5. .NET 云原生架构师训练营(模块二 基础巩固 RabbitMQ HelloWorld)--学习笔记

    2.6.3 RabbitMQ -- HelloWorld 发送端 接收端 rabbitmq container 发送信息 https://www.rabbitmq.com/tutorials/tuto ...

  6. pixi.js 自定义光标样式

    pixi 介绍 Pixi是一个超快的2D渲染引擎,通过Javascript和Html技术创建动画或管理交互式图像,从而制作游戏或应用. 项目地址:https://github.com/pixijs/p ...

  7. R语言学习笔记-Corrplot相关性分析

    示例图像 首先安装需要的包 install.packages("Corrplot") #安装Corrplot install.packages("RColorBrewer ...

  8. Itranswarp 搭建个人 Wiki

    www.swack.cn - 原文链接:Itranswarp 搭建个人 Wiki 从零开始 搭建个人Wiki站点 Itranswarp.js 是一款基于nodejs开发的博客系统,通过Apache L ...

  9. openstack octavia的实现与分析(一)openstack负载均衡的现状与发展以及lvs,Nginx,Haproxy三种负载均衡机制的基本架构和对比

    [负载均衡] 大量用户发起请求的情况下,服务器负载过高,导致部分请求无法被响应或者及时响应. 负载均衡根据一定的算法将请求分发到不同的后端,保证所有的请求都可以被正常的下发并返回. [主流实现-LVS ...

  10. Tomcat配置上遇到的一些问题

    Tomcat启动:在bin目录下双击startup.bat文件就行. 访问:在浏览器输入http://localhost:8080 回车访问的是自己 的界面: http://othersip:8080 ...