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;
}

最新文章

  1. 51nod1057(python2计算n!)
  2. POJ 1873 - The Fortified Forest 凸包 + 搜索 模板
  3. 在C/C++程序里打印调用栈信息
  4. Nofollow
  5. ElasticSearch+Springboot实际应用:索引同步建设,搜索过程
  6. Ncurses &lt;一&gt;
  7. 高放的c++学习笔记之关联容器
  8. NSLog设置不打印
  9. TextView 的新特性,Autosizing 到底是如何实现的? | 源码分析
  10. 用python爬了自己的微信,原来好友都是这样的!
  11. 文件上传的三种模式-Java
  12. Pandas 使用笔记
  13. LDA-线性判别分析(四)其他几个相关问题
  14. Ansible-playbook的简单使用 [转]
  15. Capjoint
  16. iOS常用的代码块整理
  17. C++11新特性之 std::forward(完美转发)(转)
  18. java基础(七) java四种访问权限
  19. cas4.0 session中返回更多的用户信息
  20. 黑马day18 鼠标事件&amp;amp;图片变大

热门文章

  1. DBF 文件 ORACLE 数据库恢复
  2. python第一节:变量及数据类型
  3. 软件“美不美”,UI测试一下就知道
  4. 大数据专栏 - 基础1 Hadoop安装配置
  5. ethernet
  6. maven打包时排除配置文件
  7. thinkphp redis实现文章点赞功能并同步入mysql
  8. 2021年了,`IEnumerator`、`IEnumerable`还傻傻分不清楚?
  9. Sentry(v20.12.1) K8S 云原生架构探索, SENTRY FOR JAVASCRIPT 手动捕获事件基本用法
  10. Spring中的@Valid 和 @Validated注解你用对了吗