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. TurtleBot3 Waffle (tx2版华夫)(2)系统安装

    Tx2系统默认是安装好的,由于镜像文件大于20G,无法上传百度网盘,所以如有需要请联系我们客服:下面主要是操作步骤: 2.1.准备工作 a.准备好利用Jetpack刷过机的Ubuntu的主机(HOST ...

  2. 浅谈java中异常处理

    java语言的异常捕获结构是由try.catch.finally,try中语句块是可能发生异常的java语句,catch用来激发捕获的异常,try语句块中如果发生异常,则调到catch语句块中执行ca ...

  3. 杭电OJ1062---Text Reverse(c++)(C++库getline的使用)

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. Redis缓存篇(四)缓存异常

    这一节,我们来学习一下缓存异常.缓存异常有四种类型,分别是缓存和数据库的数据不一致.缓存雪崩.缓存击穿和缓存穿透. 下面通过了解这四种缓存异常的原理和应对方法. 缓存和数据库的数据不一致 缓存和数据库 ...

  5. 人生苦短我用Python,本文助你快速入门

    目录 前言 Python基础 注释 变量 数据类型 浮点型 复数类型 字符串 布尔类型 类型转换 输入与输出 运算符 算术运算符 比较运算符 赋值运算符 逻辑运算符 if.while.for 容器 列 ...

  6. 【C++】《C++ Primer 》第十二章

    第十二章 动态内存 目前为止,所使用的对象都有着严格定义的生存期. 全局对象在程序启动时分配,在程序结束时销毁.局部自动对象在进入其定义所在的程序块时被创建,在离开块时销毁.局部static对象在第一 ...

  7. 剑指 Offer 16. 数值的整数次方

    实现函数double Power(double base, int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题. 来源:力扣(LeetCode) 链接 ...

  8. Oracle 索引原理分析

    索引是一种允许直接访问数据表中某一数据行的树型结构,为了提高查询效率而引入,是一个独立于表的对象,可以存放在与表不同的表空间中.索引记录中存有索引关键字和指向表中数据的指针(地址).对索引进行的I/O ...

  9. ctfhub技能树—信息泄露—备份文件下载—网站源码

    打开靶机 查看网页内容 使用dirsearch进行扫描 命令如下 python3 dirsearch.py -u http://challenge-91f1f5e6a791ab02.sandbox.c ...

  10. 分布式系统:dubbo的连接机制

    目录 研究这个问题的起因 dubbo的连接机制 为什么这么做 dubbo同步转异步 dubbo的实现 纯netty的简单实现 总结 研究这个问题的起因 起因是一次面试,一次面试某电商网站,前面问到缓存 ...