一、链表定义:链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
有的时候,处于内存中的数据并不是连续的。那么这时候,我们就需要在数据结构中添加一个属性,这个属性会记录下面一个数据的地址。有了这个地址之后,所有的数据就像一条链子一样串起来了,那么这个地址属性就起到了穿线连结的作用。
相比较普通的线性结构,链表结构的优势有一下三点:
(1)单个节点创建非常方便,普通的线性内存通常在创建的时候就需要设定数据的大小
(2)节点的删除非常方便,不需要像线性结构那样移动剩下的数据
(3)节点的访问方便,可以通过循环或者递归的方法访问到任意数据,但是平均的访问效率低于线性表
二、链表实验(代码经过测试)
设计链表数据结构(节点)
struct Student
{
int num; //学生编号
float score; //学生成绩
struct Student *next;
};
1、静态链表
void test_simplestruct() // 静态链表
{
struct Student a,b,c,*head,*p;
a.num=10101;a.score=89.5;
b.num=10104;b.score=91;
c.num=10107;c.score=95.8; //对节点进行赋值
head=&a; //将节点a的初始地址赋给头指针head
a.next=&b; //将节点b的初始地址赋给a的next成员
b.next=&c; //将节点c的初始地址赋给b的next成员
c.next=NULL; //将节点cnext成员赋为NULL
p=head; //使p也指向a节点
printf("NO. score\n");
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next; //使p指向下一个节点
} while (p!=NULL);
}
将
test_simplestruct() 函数放在main()中测试,实验结果:
2、动态链表
int n=0; //n 为全局变量
2.1 创建链表
struct Student *struct_creat(void) //创建链表
{
struct Student *head,*p1,*p2;
p1=p2=(struct Student *)malloc(LEN); //开辟一个新单元
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if (n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
2.2 删除链表
struct Student *struct_delete(struct Student *head,long num)
{
struct Student *p1,*p2;
if (head==NULL)
{
printf("\nlist null!\n");
return head;
}
p1=head;
while(p1->num!=num && p1->next!=NULL) //p1指向的不是要删除的节点,并且后面还有节点
{
p2=p1;
p1=p1->next; //后移一个节点
}
if (num==p1->num) //是要删除的节点
{
if (p1==head) //删除的是头结点
head=p1->next;
else //删除的不是头结点
p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
free(p1); //释放节点内存
p1=NULL; //将p1指向NULL
}
else
printf("%ld not been found!\n",num);
return head;
}
2.3 插入链表
struct Student *struct_insert(struct Student *head,struct Student *stu)
{
struct Student *p0,*p1,*p2;
p1=head; //p1指向第一个节点
p0=stu; //p0指向要插入的节点
if (head==NULL) //原来的节点为空
{
head=p0; //p0指向头结点
p0->next=NULL;
}
else
{
while((p0->num > p1->num)&&(p1->next!=NULL))
{
p2=p1; //p2指向刚才p1的节点
p1=p1->next; //p1后移一个节点
}
if (p0->num<=p1->num)
{
if (p1==head) //插到原来第一个节点之前
{
head=p1;
}
else //插到p2指向的节点之后
{
p2->next=p0;
p0->next=p1;
}
}
else //插到最后的节点之后
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1; //节点数加1
return head;
}
2.4 显示链表
void struct_print(struct Student * head)//将链表中各节点数据依次输出
{
struct Student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if (head!=NULL)
{
do
{
printf("%ld,%5.1f\n",p->num,p->score);
p=p->next;
} while (p!=NULL);
}
}
2.5 动态链表测试函数
void test_structall()
{
struct Student *head,*stu;
long del_num;
printf("input records:\n");
head=struct_creat(); //创建链表
struct_print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=struct_delete(head,del_num); //删除链表
struct_print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct Student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{
head=struct_insert(head,stu); //插入链表
struct_print(head);
printf("\ninput the inserted record:");
stu=(struct Student *)malloc(LEN); //每次插入都要新开辟一块内存
scanf("%ld,%f",&stu->num,&stu->score);
}
}
将2.5 动态链表测试函数放在 main()中测试,测试结果为: