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. JVM笔记——类加载

    1.在java代码中,类型(如class enum interface)的加载.连接.初始化过程都是在程序运行期完成的.这个特性,使得本为静态语言的java,拥有了动态语言的某些特征 加载:查找并加载 ...

  2. Kubernetes官方java客户端之四:内部应用

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. Socket粘包问题的3种解决方案,最后一种最完美!

    在 Java 语言中,传统的 Socket 编程分为两种实现方式,这两种实现方式也对应着两种不同的传输层协议:TCP 协议和 UDP 协议,但作为互联网中最常用的传输层协议 TCP,在使用时却会导致粘 ...

  4. 你知道 react-color 的实现原理吗

    一.前言 ReactColor 是一个优秀的 React 颜色选择器组件,官方给了多种布局供开发者选择. 笔者常用的主题为 Sketch,这种主题涵盖了颜色面板.推荐色块.RGB颜色输入等功能,比较完 ...

  5. Detectron2 快速开始,使用 WebCam 测试

    本文将引导快速使用 Detectron2 ,介绍用摄像头测试实时目标检测. Detectron2: https://github.com/facebookresearch/detectron2 环境准 ...

  6. 2020DevOps状态报告——变更管理

    如果你的公司还没有走向平台化,现在仍然可以是很大的飞跃.您仍然可以通过解决公司的变更管理流程来加快软件交付.在本章中,我们将研究我们在公司内部所学的变更管理模式.我们将向您展示什么是有效的,什么是无效 ...

  7. SQL注入之堆叠注入(堆查询注入)

    Stached injection -- 堆叠注入 0x00 堆叠注入的定义 ​ Stacked injection 汉语翻译过来后,称 为堆查询注入,也有称之为堆叠注入.堆叠注入为攻击者提供了很多的 ...

  8. 【JavaWeb】JSP 页面

    JSP 页面 简介 JSP(Java Server Pages),即 Java 的服务器页面.它的主要作用是代替 Servlet 程序回传 HTML 页面的数据,因为 Servlet 程序回传 HTM ...

  9. 【SpringMVC】SpringMVC 响应数据

    SpringMVC 响应数据 文章源码 返回值分类 返回值是字符串 Controller 方法返回字符串可以指定逻辑视图的名称,通过视图解析器解析为物理视图的地址. @Controller @Requ ...

  10. LeetCode144 二叉树的前序遍历

    给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? /** * Defin ...