双向链表相比单向链表,多了一个指向上一节点的结构体指针。下面以建立一个录入小型学生成绩程序为例。
链表的建立不同之处在于多了一个指向上一节点的指针 last。(建立的方法与节点删除与 1 中方法不同,但实质相同)
该程序包括学生名字与成绩的录入、依据编号查找学生成绩、删除相应编号的学生的信息
#include<stdio.h>
#include<stdlib.h>
#define len sizeof(struct Student)
struct Student
{ float score;//学生的分数
char name[20];//学生的名字
struct Student*next;
struct Student*last;
};
int N;
struct Student*creat()//双向链表的建立
{ struct Student*head,*pnew,*p;
pnew=NULL;
p=NULL;
int i;
head=(struct Student*)malloc(len);//建立表头并初始化
head->next=NULL;
head->last=NULL;
p=head;
printf("请输入 1 号学生的名字:");
scanf("%s",p->name);
printf("\n请输入他/她的成绩:");
scanf("%f",&(p->score));
for(i=1;i<N;i++)//
{
pnew=(struct Student*)malloc(len);
p->next=pnew;
printf("请输入 %d 号学生的名字:",i+1);
scanf("%s",pnew->name);
printf("请输入他/她的成绩:");
scanf("%f",&(pnew->score));
pnew->last=p;
pnew->ne

本文以建立录入小型学生成绩的程序为例,介绍了如何使用双向链表。双向链表增加了指向上一节点的指针,使得在链表操作如查找和删除时更为灵活。程序涵盖了学生信息的录入、按编号查找成绩以及删除特定编号学生等功能。
最低0.47元/天 解锁文章
1730

被折叠的 条评论
为什么被折叠?



