04-链表的创建与输出(PPT)
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
head
p
p last
例1 编写一个函数,建立一个学生数据的单向链表,约定学号不为0,如果输入的学号 为0,则建立链表的过程结束。
struct student {
long num; float score; struct student *next; };
struct student *head,*p,*last;
if (head==NULL) head=p; else last->next=p; last=p;
p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p>score)); p->next=NULL; } free(p); return head; }
if (head==NULL) head=p;
else last->next=p; last=p;
p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p->score)); p->next=NULL; } free(p); return head; }
struct student {
long num; float score; struct student *next; }; #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct student) struct student *create() { struct student *head,*p,*last; head=NULL; p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p->score)); p->next=NULL; while(p->num!=0) {
head==NULL?
Y
N
head=p
last->next=p
last=p
创建一个新节点赋给p
释放最后一个节点的空间
#define LEN sizeof(struct student) struct student *create() {
struct student *head,*p,*last; head=NULL; p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p->score)); p->next=NULL; while(p->num!=0) {
head
p p
2 链表尾方法:把新建立的节点接到链表的表尾。
链表尾算法: 1 给head赋NULL(head=NULL)。 2 建立新节点,并把该节点的地址赋给指针变量p,将该节点的next指针设为NULL(p->next=NULL)。 3 如果head为NULL,则让head指向新节点(head=p),否则把这个新节点插到最后一个节点的后面( last->next=p)。 4 将last指针指向这个新节点(last=p)。 5 执行第(2)步,继续建立新节点。
Y
N
输出学生数据
p=p->next
直到p等于NULL为止
void print(struct student *head) {
struct student *p; p=head; if (head!=NULL)
do {
printf("%ld %f\n",p->num,p->score); p=p->next; }while(p!=NULL); }
head=NULL
创建第一个新节点赋给p
当p->num≠0时
head==NULL?
Y
N
head=p
last->next=p
last=p
创建一个新节点赋给p
释放最后一个节点的空间
malloc函数开辟一个节点 输入节点数据 p->next=NULL
head=NULL
创建第一个新节点赋给p
当p->num≠0时
2 链表的创建 创建链表是指创建一个一个的节点并输入各节点数据,然后建立起各 节点前后相链接的关系。
插表头方法 链表尾方法
(1)插表头方法:把新建立的节点作为表头插入到链表中。
插表头算法: 1 给head赋NULL(head=NULL)。 2 建立新节点,并把该节点的地址赋给指针变量p。 3如果head为NULL,则将新节点的next域设为NULL,再让head指向新节点(p->next=NULL、head=p), 否 则先将新节点的next指针指向head指针指向的那个节点,然后将head指针指向新节点(p->next=head、 head=p)。 4 执行第(2)步,继续建立新节点。
3 链表的输出 链表输出算法:
1 定义指针变量p,让p指向头节点(p=head)。 2 输出p所指节点的数据。 3 使p后移一个节点(p=p->next)。 4 如果p不为空,循环执行第(2)步;P为空,则结束输出。
head
…
p
p
p
例2 编写一个函数,输出学生数据的单向链表。
p=head
head≠NULL?
void print(struct student *head) {
struhead!=NULL)
do {
printf("%ld %f\n",p->num,p->score); p=p->next; }while(p!=NULL); } int main() { struct student *prt; prt=create(); print(prt); return 0; }
(2)链表节点的类型定义 链表的节点是结构体类型,包含若干成员。 一般格式: struct 节点结构体类型名 {
数据成员定义 struct 节点结构体类型名 *指针变量名; } 例如: struct student { long num; float score;
struct student *next; }
链表的基本操作 链表的概念 链表的创建 链表的输出 链表的插入 链表的删除
1 链表的概念 (1)链表的逻辑结构
head
数据域 指针域
数据域 指针域
数据域 指针域
数据域 指针域
链表由一系列的节点组成,每个节点包括两个部分:数据域和指针域。 链表有一个头指针变量(head),用于存放链表第一个节点的地址。 链表尾节点的指针域为空(NULL)。
p
p last
例1 编写一个函数,建立一个学生数据的单向链表,约定学号不为0,如果输入的学号 为0,则建立链表的过程结束。
struct student {
long num; float score; struct student *next; };
struct student *head,*p,*last;
if (head==NULL) head=p; else last->next=p; last=p;
p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p>score)); p->next=NULL; } free(p); return head; }
if (head==NULL) head=p;
else last->next=p; last=p;
p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p->score)); p->next=NULL; } free(p); return head; }
struct student {
long num; float score; struct student *next; }; #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct student) struct student *create() { struct student *head,*p,*last; head=NULL; p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p->score)); p->next=NULL; while(p->num!=0) {
head==NULL?
Y
N
head=p
last->next=p
last=p
创建一个新节点赋给p
释放最后一个节点的空间
#define LEN sizeof(struct student) struct student *create() {
struct student *head,*p,*last; head=NULL; p=(struct student *)malloc(LEN); scanf("%ld%f",&(p->num),&(p->score)); p->next=NULL; while(p->num!=0) {
head
p p
2 链表尾方法:把新建立的节点接到链表的表尾。
链表尾算法: 1 给head赋NULL(head=NULL)。 2 建立新节点,并把该节点的地址赋给指针变量p,将该节点的next指针设为NULL(p->next=NULL)。 3 如果head为NULL,则让head指向新节点(head=p),否则把这个新节点插到最后一个节点的后面( last->next=p)。 4 将last指针指向这个新节点(last=p)。 5 执行第(2)步,继续建立新节点。
Y
N
输出学生数据
p=p->next
直到p等于NULL为止
void print(struct student *head) {
struct student *p; p=head; if (head!=NULL)
do {
printf("%ld %f\n",p->num,p->score); p=p->next; }while(p!=NULL); }
head=NULL
创建第一个新节点赋给p
当p->num≠0时
head==NULL?
Y
N
head=p
last->next=p
last=p
创建一个新节点赋给p
释放最后一个节点的空间
malloc函数开辟一个节点 输入节点数据 p->next=NULL
head=NULL
创建第一个新节点赋给p
当p->num≠0时
2 链表的创建 创建链表是指创建一个一个的节点并输入各节点数据,然后建立起各 节点前后相链接的关系。
插表头方法 链表尾方法
(1)插表头方法:把新建立的节点作为表头插入到链表中。
插表头算法: 1 给head赋NULL(head=NULL)。 2 建立新节点,并把该节点的地址赋给指针变量p。 3如果head为NULL,则将新节点的next域设为NULL,再让head指向新节点(p->next=NULL、head=p), 否 则先将新节点的next指针指向head指针指向的那个节点,然后将head指针指向新节点(p->next=head、 head=p)。 4 执行第(2)步,继续建立新节点。
3 链表的输出 链表输出算法:
1 定义指针变量p,让p指向头节点(p=head)。 2 输出p所指节点的数据。 3 使p后移一个节点(p=p->next)。 4 如果p不为空,循环执行第(2)步;P为空,则结束输出。
head
…
p
p
p
例2 编写一个函数,输出学生数据的单向链表。
p=head
head≠NULL?
void print(struct student *head) {
struhead!=NULL)
do {
printf("%ld %f\n",p->num,p->score); p=p->next; }while(p!=NULL); } int main() { struct student *prt; prt=create(); print(prt); return 0; }
(2)链表节点的类型定义 链表的节点是结构体类型,包含若干成员。 一般格式: struct 节点结构体类型名 {
数据成员定义 struct 节点结构体类型名 *指针变量名; } 例如: struct student { long num; float score;
struct student *next; }
链表的基本操作 链表的概念 链表的创建 链表的输出 链表的插入 链表的删除
1 链表的概念 (1)链表的逻辑结构
head
数据域 指针域
数据域 指针域
数据域 指针域
数据域 指针域
链表由一系列的节点组成,每个节点包括两个部分:数据域和指针域。 链表有一个头指针变量(head),用于存放链表第一个节点的地址。 链表尾节点的指针域为空(NULL)。