结构体可以看做是一种自定义的数据类型,它还有一个很重要的特性,就是结构体可以相互嵌套使用,但也是有条件的,结构体可以包含结构体指针,但绝对不能在结构体中包含结构体变量。
利用结构体的这点特殊特性,我们就可以自己生成一个环环相套的一种射线结构,一个指向另一个。
链表的学习不像想象的那么那么容易,很多人学习到这里的时候都会碰到困难,很多人也因此而放弃了学习,在这里我说,一定不能放弃,对应它的学习我们要进行分解式学习,方法很重要,理解需要时间,不必要把自己逼迫的那么紧,学习前你也得做一些最基本的准备工作,你必须具备对堆内存的基本知识的了解,还有就是对结构体的基本认识,有了这两个重要的条件,再进行分解式学习就可以比较轻松的掌握这一节内容的难点。
下面我们给出一个完整的创建链表的程序,不管看的懂看不懂希望读者先认真看一下,想一想,看不懂没有关系,因为我下面会有分解式的教程,但之前的基本思考一定要做,要不即使我分解了你也是无从理解的。
代码如下,我在重要部分做了注解:
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//程序作者:管宁
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//站点:www.cndev-lab.com
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//所有稿件均有版权,如要转载,请务必著名出处和作者
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <iostream>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct test
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
char name[10];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
float socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
test *head;//创建一个全局的引导进入链表的指针
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
test *create()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *ls;//节点指针
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *le;//链尾指针
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ls = new test;//把ls指向动态开辟的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>ls->name>>ls->socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=NULL;//进入的时候先不设置head指针指向任何地址,因为不知道是否一上来就输入null跳出程序
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le=ls;//把链尾指针设置成刚刚动态开辟的堆内存地址,用于等下设置le->next,也就是下一个节点的位置
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(strcmp(ls->name,"null")!=0)//创建循环条件为ls->name的值不是null,用于循环添加节点
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(head==NULL)//判断是否是第一次进入循环
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=ls;//如果是第一次进入循环,那么把引导进入链表的指针指向第一次动态开辟的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le->next=ls;//如果不是第一次进入那么就把上一次的链尾指针的le->next指向上一次循环结束前动态创建的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le=ls;//设置链尾指针为当前循环中的节点指针,用于下一次进入循环的时候把上一次的节点的next指向上一次循环结束前动态创建的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ls=new test;//为下一个节点在堆内存中动态开辟空间
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>ls->name>>ls->socre;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le->next=NULL;//把链尾指针的next设置为空,因为不管如何循环总是要结束的,设置为空才能够在循环显链表的时候不至于死循环
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
delete ls;//当结束的时候最后一个动态开辟的内存是无效的,所以必须清除掉
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return head;//返回链首指针
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void showl(test *head)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"链首指针:"<<head<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(head)//以内存指向为null为条件循环显示先前输入的内容
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<head->name<<"|"<<head->socre<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=head->next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
showl(create());
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin.get();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin.get();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
上面的代码我们是要达到一个目的:就是要存储你输入的人名和他们的得分,并且以链状结构把它们组合成一个链状结构。
程序种有两个组成部分
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
test *create()
和
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void showl(test *head)
这两个函数,create是用来创建链表的 ,showl是用来显示链表的。
create函数的返回类型是一个结构体指针,在程序调用的时候我们用了showl(create());,而不用引用的目的原因是引导指针是一个全局指针变量,我们不能在showl()内改变它,因为showl()函数内有一个移动操作head=head->next;,如果是引用的话我们就破坏了head指针的位置,以至于我们再也无法找会首地址的位置了。
下面我们来分解整个程序,以一个初学者的思想来思考整个程序,由浅入深的逐步解释。
首先,我们写这个程序,要考虑到由于是一个链表结构,我们不可能知道它的大小到底是多大,这个问题我们可以用动态开辟堆内存来解决,因为堆内存在程序结束前始终是有效的,不受函数栈空间生命期的限制,但要注意的是我们必须有一个指针变量来存储这一链状结构的进入地址,而在函数内部来建立这一指针变量显然是不合适的,因为函数一旦退出,这个指针变量也随之失效,所以我们在程序的开始声明了一个全局指针变量。
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
test *head;//创建一个全局的引导进入链表的指针
好解决了这两个问题,我们接下去思考
有输入就必然有输出,由于输出函数和输入函数是相对独立的,为了不断测试程序的正确性好调试我们先写好输出函数和main函数捏的调用,创建函数我们先约定好名为create。
我们先写出如下的代码:
程序写到这里,基本形态已经出来,输入和调用我们已经有了。
下面我们来解决输入问题,链表的实现我们是通过循环输入来实现的,既然是循环我们就一定得考虑终止循环的条件,避免死循环和无效循环的发生。
在create()函数内部我们先写成这样:
在循环创建之前我们必须考虑一个都不输入的情况。
程序一单进入create函数我们首先必然要创建一个节点,我们先创建一个节点指针,后把者个节点指针指向到动态开辟的test类型的动态内存地址位置上。
所以我们有了
程序既然是循环输入,而结构成员test *next又是用来存储下一个接点的内存地址的,每次循环我们又要动态创建一个新的内存空间,所以我们必须要有一个指针来存储上一次循环动态开辟的内存地址,于是就有了
test *le;
接下来在进入循环前我们要创建链表的第一个节点,第一个节点必然是在循环外创建,于是就有了
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
cin>>ls->name>>ls->socre;
程序执行者的情况是位置的,所以我们必然要考虑,一上来就不想继续运行程序的情况,所以我们一开始先把head引导指针设置为不指向任何地址也就是
head=NULL;
为了符合le也就是链尾指针的设计思路,我们在循环前一定要保存刚刚动态开辟的内存地址,好在下一次循环的时候设置上一个节点中的next成员指向,于是我们便有了:
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
le=ls;
为了实现循环输入我们又了下面的代码:
程序是循环必然要有终止循环的条件,所以我们的循环条件是:
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
while(strcmp(ls->name,"null")!=0)
输入的名字是null的时候就停止循环。
为了保证第一次进入循环,也就是在循环内准备创建第二个节点前,设置引导指针的指向我们有了如下的判断代码:
代码中的else条件是为了设置前一个节点next指向而写的,这点我们记住先看下面的代码,稍后大家回过头想就明白了
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
le=ls;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
ls=new test;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
cin>>ls->name>>ls->socre;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
le=ls;//这么写就是为了保存上一次循环指针的位置而设的,正是为了上面的else代码而做的预先保留
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
ls=new test;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
cin>>ls->name>>ls->socre;
这两行代码的意思就是继续开辟下一个节点空间,和输入节点内容!
循环一旦结束也就结束了程序,为了保持程序不出错,也就是最后一个节点的next成员指向为空我们有了下面的代码
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
le->next=NULL;
程序的思路始终是以先开辟后判断为思路的,所以到最后一个不成立的时候总会有一个多开辟的内存空间,为了删除掉它,我们有了下面的代码
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
delete ls;
程序到最后由于返回head指针
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
return head;
显示链表的函数没有什么太多特别的也只需要注意下面这样就可以了!
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
head=head->next;
我们之所以不用head+=1;来写就是因为链表是我们动态开辟的,而每一个节点的位置并不是相连的,next成员指针的意义也就是下一个节点的内存地址。
到这里整个创建函数的设计思路也都说完了,笔者不一定说的很好,但基本思路是这样的,希望读者多思考,多对比,相信此教程还是对大家有帮助的,程序设计就是利用逐步思考的方式进行的,写好的代码往往直接看看不懂就是因为中间的细节并不是一次都能够想到的。
下面我们来说一下链表节点的删除!
我们以上面的程序为基础,但为了我们方便学习删除我们休整结构体为
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct test
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int number;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
float socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
number为唯一的编号每一个节点的。
删除的我就不多说了,里面重要部分有注解。
特别注意deletel函数的参数意义,指针的引用在这里很重要,如果只是指针,或者只是应用都是不行的,为什么仔细思考,很多知名的教材在这一问题上都很模糊,而且很多书还有错误,程序不错,但思路是错的,我这里特别不说,请大家仔细阅读程序,如果还是有问题,可以回此帖,我会回答的。
完整代码如下:
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct test
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int number;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
float socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
最后我学习一下如何在已有的链表上插入节点
我们要考虑四中情况,
1.链表为空!
2.插入点在首节点前
3.插入点找不到的情况我们设置放在最后!
4.插入点在中间的情况!
今天的程序在昨天的基础上做了进一步的修改,可以避免删除点找不到的情况,如果找不到删除点就退出函数!
代码入下:重点部分我有了详细的注解,插入函数名为insterl()
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//程序作者:管宁
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//站点:www.cndev-lab.com
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//所有稿件均有版权,如要转载,请务必著名出处和作者
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <iostream>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct test
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int number;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
float socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
test *head;//创建一个全局的引导进入链表的指针
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
test *create()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *ls;//节点指针
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *le;//链尾指针
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ls = new test;//把ls指向动态开辟的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"请输入第一个节点number和节点score,输入0.0跳出函数"<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>ls->number>>ls->socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=NULL;//进入的时候先不设置head指针指向任何地址,因为不知道是否一上来就输入null跳出程序
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le=ls;//把链尾指针设置成刚刚动态开辟的堆内存地址,用于等下设置le->next,也就是下一个节点的位置
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(ls->number!=0)//创建循环条件为ls->number的值不是null,用于循环添加节点
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(head==NULL)//判断是否是第一次进入循环
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=ls;//如果是第一次进入循环,那么把引导进入链表的指针指向第一次动态开辟的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le->next=ls;//如果不是第一次进入那么就把上一次的链尾指针的le->next指向上一次循环结束前动态创建的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le=ls;//设置链尾指针为当前循环中的节点指针,用于下一次进入循环的时候把上一次的节点的next指向上一次循环结束前动态创建的堆内存地址
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ls=new test;//为下一个节点在堆内存中动态开辟空间
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"请下一个节点number和节点score,输入0.0跳出函数"<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>ls->number>>ls->socre;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le->next=NULL;//把链尾指针的next设置为空,因为不管如何循环总是要结束的,设置为空才能够在循环显链表的时候不至于死循环
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
delete ls;//当结束的时候最后一个动态开辟的内存是无效的,所以必须清除掉
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return head;//返回链首指针
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void showl(test *head)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"链首指针:"<<head<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(head)//以内存指向为null为条件循环显示先前输入的内容
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<head->number<<"|"<<head->socre<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=head->next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void deletel(test *&head,int number)//这里如果参数换成test *head,意义就完全不同了,head变成了复制而不是原有链上操作了,特别注意,很多书上都不对这里
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *point;//判断链表是否为空
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(head==NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"链表为空,不能进行删除工作!";
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int derror=1;//设置找不到的情况的条件,预先设置为真
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *check=head;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(check)//利用循环进行查找
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if (check->number==number)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
derror=0;//条件转为假
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
check=check->next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(derror)//如果为假就跳出函数
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(head->number==number)//判删除的节点是否为首节点
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
point=head;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"删除点是链表第一个节点位置!";
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=head->next;//重新设置引导指针
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
delete point;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *fp=head;//保存连首指针
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(test *&mp=head;mp->next;mp=mp->next)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(mp->next->number==number)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
point=mp->next;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mp->next=point->next;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
delete point;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=fp;//由于head的不断移动丢失了head,把进入循环前的head指针恢复!
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void insterl(int number)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *point=new test;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"请输入节点number和节点score"<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>point->number>>point->socre;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(head==NULL)//链表为空的情况下插入
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=point;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
point->next=NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int ierror=1;//设置找不到的情况的条件,预先设置为真
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *le;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
test *check=head;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(check)//利用循环进行查找
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if (check->number==number)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ierror=0;//条件转为假
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le=check;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
check=check->next;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(ierror)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<le->number;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
le->next=point;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
point->next=NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(head->number==number)//检测是否是在第一个节点处插入
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
point->next=head;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=point;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(test *&mp=head;mp->next;mp=mp->next)//在链表中间插入
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(mp->next->number==number)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
point->next=mp->next;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mp->next=point;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
head=create();//调用创建
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
showl(head);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int dp;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"请输入删除点如果找不到就跳出函数"<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>dp;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
deletel(head,dp);//调用删除
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
showl(head);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int ip;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout<<"请输入插入点如果找不到就在链尾添加"<<endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin>>ip;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
insterl(ip);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
showl(head);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin.get();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cin.get();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
到此关于结构体的内容已经全部讨论结束,链表的建立删除插入操作可以很好的对前面所学知识进行一个总结,它既考察了程序员对内存大理解(堆内存操作、指针操作)也考察了对结构化编程掌握的熟悉程序。