当前位置:   article > 正文

C++通过类实现单链表(保姆级教程,附源码,可实现多种操作)_while(head->next!=null)

while(head->next!=null)

源码提取链接:百度网盘 请输入提取码百度网盘为您提供文件的网络备份、同步和分享服务。空间大、速度快、安全稳固,支持教育网加速,支持手机端。注册使用百度网盘即可享受免费存储空间https://pan.baidu.com/s/1_Yl1tg7TXJERmk7qaOc29g
提取码:s7h1

目录

0.需要引入的库函数

1.认识和构造链表

1.1什么是链表

1.1.1节点

1.1.2链表的连接方式

1.1.3链表的灵魂——head指针

1.1.4链表的构造——带头结点&不带头结点

1.1.5编写析构函数(必写)

1.2空链表及链表的判空条件

2.基础功能实现

2.1返回头指针

2.2返回with_head值(用以外部判断带不带头结点)

2.3返回尾指针

2.4清空链表

2.5链表的长度获取

2.6输入数据构造链表

2.7输出链表

3.更多有意思的方法实现

3.1按位查找结点(返回第i个结点的结点指针)

3.2按值查找(返回其所在结点是第几个)

3.3保存第k个结点的信息(需要提供一个datas结构体来存储)

3.4按位删除结点

3.5按值删除

3.6翻转链表

4.插值

4.1按位插入

4.2单调性判别

4.3按值插入

4.4改进按位插入

5.一些测试函数(自己调试的时候写的)

5.1方法函数测试

5.2带头结点链表测试

5.3不带头结点链表测试

6.LinkList类整合


0.需要引入的库函数

  1. #include <iostream>
  2. #include <string>

1.认识和构造链表

1.1什么是链表

老规矩,先康康百度百科对它的定义

1.1.1节点

 可见链表的最小组成单元便是节点,如下图所示,节点则一般由两部分构成——1.数据2.next指针(用于指向下一节点)。

那便先定义一个最小组成单元节点吧!因为囊括了两个部分,所以我们采用结构体来储存这俩数据。那么问题来了,数据data是什么数据类型,指针类型又是什么呢?

data的类型好办,想存一个整形那就来个int data;想存个字符那就char data;但是如果我比较贪,我既想保存一个数字又想保存一串字符呢?那就采用一个结构体来保存数据吧!该部分代码如下:

  1. typedef struct datas
  2. {
  3. int num;
  4. string intro;
  5. datas()
  6. {
  7. num = 0;
  8. intro = "无";
  9. };
  10. datas(int n, string s)
  11. {
  12. num = n;
  13. intro = s;
  14. };
  15. }datas;

这样我们就定义好了一个结构体datas,它囊括两个信息——1.一个整型数据num2.一个字符串intro

并且定义了两种构造方式——无参构造时让数字为0信息为“无”,有参构造时第一个参数传给整形数据,第二个传给字符串数据。

接下来想一想next指针的类型应该是啥,前面说了next指针是用来指向下一节点的地址的,那么假设节点的类型是NodeType,那么next的类型即为NodeType*。而前面又说了节点我们应该构造成一个结构体,欸嘿这一来就成了。

我们得到了节点结构体node。 

  1. typedef struct node
  2. {
  3. datas* information;
  4. node* next;
  5. }node;

 (其中information是指向datas结构体的一个指针。)

再来完善一手这个结构体,既然定义了一个结构体那我们不妨给它也写上构造函数好了。先考虑一下无参构造,无参构造时只需要构造一个能放入数据的孤立节点就好。

什么是孤立节点呢?这是我随口胡诌的词哈哈哈,就是说这一个节点的data部分是可以存入数据滴,但是它的next指针指向NULL,意思就是它和其他任何节点没有关联,就像一个孤儿,所以我叫它孤儿孤立节点。

这么一来无参构造的方式就很明确了,给information分配一个内存以确保可以存入数据,再将next指向NULL,如此一来便大功告成!

  1. node() //无参构造结点
  2. {
  3. information = new datas;
  4. next = NULL;
  5. };

再来带参构造一个节点,我们关心的只有俩参数,一个是数据指针information(代表这个节点包含何种信息),一个是next指针(指明这个节点将要带领我们去向何方),但往往我们带参只是想建立一个有初始数据的孤立节点,比如下面要说的无头节点链表中的构造方式。 

总的来说带参构造节点时information取何值必须与用户交互,而next可以给以默认值 NULL来构造成带值的孤立节点。于是我们得到了如下的带参构造函数:

  1. node(datas& item, node* ptr = NULL) //带参构造结点(无头结点)
  2. {
  3. information = &item;
  4. next = ptr;
  5. };

这时候发挥一下裁缝天赋,将上面的node结构体和两种构造方式一缝合,欸嘿,就得到了完整的节点结构体:

  1. //节点类结构体
  2. typedef struct node
  3. {
  4. datas* information;
  5. node* next;
  6. node() //无参构造节点
  7. {
  8. information = new datas;
  9. next = NULL;
  10. };
  11. node(datas& item, node* ptr = NULL) //带参构造节点(无头节点)
  12. {
  13. information = &item;
  14. next = ptr;
  15. };
  16. }node;

1.1.2链表的连接方式

知道和构造好了最小组成单元之后再需要figure out的就是组成方式,即链表是通过怎样的方式将一个个最小组成单元——节点连接在一起的。(突然放洋屁哈哈哈)

链表的连接方式很简单,即将每一个节点的next指针指向下一个节点的地址。假设我们有两个孤立节点指针node1, node2(注意这里node1,node2都是节点指针node*,指向两个孤立节点),这时我们将他们连接起来:node1->next = node2;这样就得到了一个长度为2的链表,好耶!

那这时候我们就可以通过node1来访问node2的值,即:node->next->information所指向的datas结构体就是节点2中的信息,同理如果有n个孤立节点依次连接起来,我们就可以通过第一个节点的指针挨个遍历所有节点的信息啦!

1.1.3链表的灵魂——head指针

链表有了节点和连接方式之后,就还差临门一脚了。仿佛此时一个链表类LinkList已经呼之欲出了,但是总觉少了些什么罢,却又是什么呢,我竟答不上来......

究竟少了个啥呢?想想说连接方式里最后说的我们可以通过第一个节点的指针挨个遍历所有节点信息,这个指针就像是所有连接在一起的节点的老大哥一样,所以我们形象的称呼他为老大哥head

显然由于head指向的是链表的第一个节点,所以它的类型是node*,这样我们就得到了LinkList类的雏形:

  1. class LinkList
  2. {
  3. private:
  4. node* head;
  5. };

接下来完成以下链表的构造就算是得到了一个初具其形的链表类!

1.1.4链表的构造——带头结点&不带头结点

链表的构造形式有两种:1.带头结点的链表2.不带头结点的链表

它们的区别在于你所感兴趣的信息是从*head结点开始还是(*head->next)结点开始存储。扯白了说就是含有有用信息的结点是从第一个开始捏还是第二个开始捏。

当所感兴趣的信息从(*head->next)结点(也就是第二个结点)开始时第一个结点就只沦为一个指路的工具人,哦不,工具点,此时这个head只用来指路,它带不带信息,带什么信息都雨我无瓜。这便是带头结点的单链表

当所感兴趣的信息从(*head)结点(也就是第一个结点)开始时,第一个结点便不单单是指路的工具,它所携带的数据*(head->information)同样是俺们感兴趣的玩意。这便是不带头结点的单链表。

此时,最大的区别就体现在二者的长度计算上了,来,康些好康的方便理解:

由于两者在读取信息和计算长度上都有差异,所以添加一个私有成员bool with_head,以此判别是否带头结点! 

那我们由此可以得出带头结点的构造是非参构造,因为此时head指向的结点的数据信息无关紧要不需要进行人为输入,直接申请一个新结点的空间即可。即:head = new node;

  1. //不带参构造——带头结点的单链表
  2. LinkList()
  3. {
  4. head = new node;
  5. with_head = true;
  6. };

而当我们想要构造不带头结点的单链表时,我们就需要人为地输入一个带有所需信息的结点来初始化head,这便是带参构造(按道理以data作为初始化信息看上去更合理,但是我是懒鬼,所以我的源码里用的是以一个结点指针作为初始化参数的,欸嘿)

版本1——以node*为输入参数

  1. //带参数构造——不带头结点的单链表
  2. LinkList(node* x)
  3. {
  4. head = x;
  5. with_head = false;
  6. };

版本2——以datas*为输入参数

  1. //带参数构造——不带头结点的单链表
  2. LinkList(datas* x)
  3. {
  4. head = new node; //建立一个孤立节点
  5. head->information = x; //将第一个节点的信息装进去
  6. with_head = false;
  7. };

至此,我们的LinkList类已经初具其形啦!让我们来康康:

  1. class LinkList
  2. {
  3. public:
  4. //无参构造,带头结点的链表
  5. LinkList()
  6. {
  7. head = new node;//给头结点分配内存
  8. with_head = true;
  9. };
  10. //带参构造,无头结点的链表
  11. LinkList(node* x)
  12. {
  13. head = x;
  14. with_head = false;//不带头结点
  15. };
  16. private:
  17. node* head;//头节点指针
  18. bool with_head;
  19. };

1.1.5编写析构函数(必写)

这是很重要很重要的一步!!!你可能会纳闷在前面构造线性表类SeqList都没提什么析构函数,怎么这里却突然冒出来一个析构函数呢?

因为在编写链表时我们会用到大量的指针,包括在实现一些功能时常常会定义一个孤立节点并给它分配空间拿来使用,如果没有析构函数,这些给出去的内存空间就像嫁出去的媳妇、泼出去的水,不及时清理这些指针就会导致内存泄露!!!

所以我们在析构函数中就一个目的,清除链表中占用的空间,也就是清空链表!由此可见清空链表是一个很重要的功能,我们必须要实现这一功能,假如我们已经通过函数void makeEmpty()实现了这一功能(具体实现见下文),那么析构函数就很好办了:

  1. //析构函数(用于释放内存,防止内存泄露)
  2. ~LinkList()
  3. {
  4. makeEmpty();
  5. }

1.2空链表及链表的判空条件

怎样是一个空的链表呢?由于链表的必备元素就一结点指针head,那我们只能由head下手并从此判断,这时细心的你应该注意到了在上文中说过带头结点和不带头结点的单链表在计数上是有差异的,那么自然对于二者的判空也有差异。

什么是空链表呢,咱就是说在这个链表里没有一个结点包含咱想要的信息,这样的链表就叫空链表。

先考虑一手带头结点的链表何时为空:由于带头结点的信息存储是从头结点的下一个结点开始的,那么当头结点的下一个结点不存在时,就称此时的带头结点的链表为空。即:head->next==NULL时为空!

再考虑一手不带头结点的链表何时为空:由于不带头结点的信息存储是从head所指向的结点开始的,那么当head所指向的结点不存在时,就称此时的带头结点的链表为空。即:head==NULL时为空!

也即判空条件可用一个三目运算符表示:(with_head)?head->next==NULL:head==NULL;

于是我们得到了判空函数isEmpty()

  1. bool isEmpty()
  2. {
  3. return (with_head) ? head->next == NULL : head == NULL;
  4. }

2.基础功能实现

2.1返回头指针

  1. node* get_head()
  2. {
  3. return head;
  4. }

2.2返回with_head值(用以外部判断带不带头结点)

  1. bool withHead()
  2. {
  3. return with_head;
  4. }

2.3返回尾指针

这个实现很容易,但是很实用。尾指针也就是单链表末尾的指针,那不管单链表带不带头结点,它的尾结点均满足一个条件——其所指的下一个结点不存在,也就是node->next==NULL,而这一个结点指针node就是我们要找的尾指针。

再考虑一个特殊情况——当链表尾空,它没有任何连接的结点,那尾结点都不存在何谈尾指针,所以空表时直接返回NULL。

不空的时候我们怎么来找到尾结点呢,我们定义一个结点指针p让它等于head,随后利用while循环只要p不是尾指针(也就是p->next不是NULL),那么p就一直下移,即:p = p->next。

这么一来我们就得到了尾指针的返回函数:

  1. node* last()
  2. {
  3. if (isEmpty())
  4. {
  5. return NULL;
  6. }
  7. node* p = head;
  8. while ( p->next != NULL )
  9. {
  10. p = p->next;
  11. }
  12. return p;
  13. }

2.4清空链表

想要清空一个链表那么就让它变成空表就好了,先考虑带头结点的情况,此时只需最后使得head->next= NULL并且所有数据节点占用的空间得到释放。

这里注意要让所有数据结点占用的空间得到释放,而遍历链表是从头开始一个一个遍历的,那么想要释放数据结点的空间也就只好一个一个释放过去。

这里我们将清空链表分为4步:

1.建立一个新结点指针p

2.如果head->next != NULL,则将head->next指向下下个

(第二步的实现方法为先用p指向待删结点,也就是p = head->next;然后再将第一个结点与待删节点的下一个结点进行连接,即:head->next = p->next;)

3.释放p的空间

4.重复2、3步直到head->next == NULL(也即此时该带头结点的链表为空)

具体如下图所示:

好了到这里我们就获得了一个清空带头结点的清空函数:

  1. void makeEmpty()
  2. {
  3. node* p;
  4. while (head->next != NULL)
  5. {
  6. p = head->next;
  7. head->next = p->next;
  8. delete p;
  9. }
  10. }

那么如果不带头结点怎么办呢,通过上图可以发现删除完之后head所指向的头结点是被保留了的,也就是说当链表不带头结点,我们还要手动删除掉head指向的结点(也就是第一个节点)

即if (!with_head)        head = NULL;

于是,我们得到了完整的makeEmpty()函数:

  1. void makeEmpty()
  2. {
  3. node* p;
  4. while (head->next != NULL)
  5. {
  6. p = head->next;
  7. head->next = p->next;
  8. delete p;
  9. }
  10. if (!with_head)
  11. head = NULL;
  12. }

哈哈哈,写着写着突然清空时发现忽略了一种异常情况——所需清空的已经是空表了,此时应当抛错或者直接return,我比较喜欢抛错。

  1. void makeEmpty()
  2. {
  3. if (isEmpty())
  4. {
  5. cout << "空的不能再空啦!" << endl;
  6. throw 1;
  7. return;
  8. }
  9. node* p;
  10. while (head->next != NULL)
  11. {
  12. p = head->next;
  13. head->next = p->next;
  14. delete p;
  15. }
  16. if (!with_head)
  17. head = NULL;
  18. }

2.5链表的长度获取

获取长度的原理十分简单,就是从第一个数据结点开始一直往后遍历,同时记录次数,最后返回次数即可。当然,空表的长度为0,这个情况单独拎出来考虑即可。

关键在第一个数据结点的指针是head还是head->next呢?这时候用三目运算符就好啦,如果是带头结点的那么信息存储结点显然是从head->next开始的,于是得到:

node*p = (with_head) ? head->next : head;

p就是我们遍历的起始指针啦,结束标志自然是当p为尾结点的时候,也就是说初始操作次数初始化为1,因为如果p就是尾结点,那么显然链表长度为1且此时进不了循环。

于是我们得到了长度获取函数length()

  1. int length()
  2. {
  3. if (isEmpty())
  4. return 0;
  5. node*p = (with_head) ? head->next : head;
  6. int count = 1;
  7. while ( p->next != NULL )
  8. {
  9. p = p->next;
  10. count++;
  11. }
  12. return count;
  13. }

2.6输入数据构造链表

这里的构成方法分为两种,一种是前插法,另一种是尾插法。

前插法是指每一个输入数据形成的新节点总是插入在head所指的结点之后。

尾插法是指每一个输入数据形成的新节点总是插在尾结点之后。(这就是为啥要写个last()函数)

那么先来思考一下输入的问题,由于我预先不知道用户需要输入多少个数据,所以我们采用while输入,那么这时候就需要一个结束标志,我们定义一个datas endTag = { 0, "结束" },也就是当用户输入的数字信息为0并且文字信息为"结束"时我们才结束输入的过程。

注意考虑一个特殊情况,如果用户手贱点开输入,为了退出而直接输入结束标志,此时直接return出去就好了。

  1. void input(datas endTag={ 0, "结束" }, bool front=true)
  2. {
  3. datas new_data; //用来存储输入的数据
  4. cout << "请输入第一个数据:";
  5. cin >> new_data.num;
  6. cin >> new_data.intro;
  7. //直接输入结束符直接开溜
  8. if ((new_data.num == endTag.num) && (new_data.intro == endTag.intro))
  9. {
  10. return;
  11. }
  12. //不满足结束标志则持续输入
  13. while ((new_data.num != endTag.num) || (new_data.intro != endTag.intro))
  14. {
  15. cout << "请输入数据:";
  16. cin >> new_data.num;
  17. cin >> new_data.intro;
  18. }
  19. }

接着考虑插入问题:由于输入的是datas数据,我们还要将之打包成一个新的结点

构造新节点&保存数据:

  1. node* p = new node; //为新插入的结点申请内存
  2. p->information->num = new_data.num;
  3. p->information->intro = new_data.intro;

前插法: 

将新节点插入头结点后,此时为了保留原本的表链,需要先将新节点的next指向head->next,再将head->next指向新节点。即:

  1. p->next = head->next;
  2. head->next = p;

后插法:

后插法最简单,直接将尾指针的next指向新节点即可:last()->next = p;

这里巧妙在p是无参构造的node,它的next原本就是NULL,恰好与尾结点的定义不谋而合

last()->next = p;

于是咱们就得到了完整的输入函数input()

  1. void input(datas endTag={ 0, "结束" }, bool front=true)
  2. {
  3. //为新元素申请结点
  4. node* p;
  5. datas* new_data = new datas;
  6. if (front)//前插法构造链表
  7. {
  8. cout << "请输入第一个数据:";
  9. cin >> new_data->num;
  10. cin >> new_data->intro;
  11. //直接输入结束符,立刻开溜
  12. if ((new_data->num == endTag.num) && (new_data->intro == endTag.intro))
  13. {
  14. return;
  15. }
  16. //不满足结束标志则持续输入
  17. while ((new_data->num != endTag.num) || (new_data->intro != endTag.intro))
  18. {
  19. p = new node;//为新结点申请空间
  20. p->information->num = new_data->num;
  21. p->information->intro = new_data->intro;
  22. if (head->next != NULL)
  23. {
  24. p->next = head->next;
  25. }
  26. head->next = p;
  27. cout << "请输入数据:";
  28. cin >> new_data->num;
  29. cin >> new_data->intro;
  30. }
  31. }
  32. else//尾插法构造链表
  33. {
  34. cout << "请输入第一个数据:";
  35. cin >> new_data->num;
  36. cin >> new_data->intro;
  37. if ((new_data->num == endTag.num) && (new_data->intro == endTag.intro))
  38. {
  39. return;
  40. }
  41. //不满足结束标志则持续输入
  42. while ((new_data->num != endTag.num) || (new_data->intro != endTag.intro))
  43. {
  44. p = new node;//为新结点申请空间
  45. p->information->num = new_data->num;
  46. p->information->intro = new_data->intro;
  47. last()->next = p;
  48. cout << "请输入数据:";
  49. cin >> new_data->num;
  50. cin >> new_data->intro;
  51. }
  52. }
  53. }

2.7输出链表

输出一个链表很容易,只要从第一个存有所需信息的结点开始一直遍历并输出下去即可。

但是要注意一个异常,如果为空链表,你可以选择抛错说“空链表无法输出”,也可以输出个“空空如也”啥的,看你自己乐意哪种,我个人倾向于抛错。

只需要注意一点就是开始遍历的结点p应该等于(with_head) ? head->next : head,因为带不带头结点关乎第一个存储信息的结点的位置!

还有一件事!注意这里不是遍历到尾结点就可以跑路的!!!

因为尾结点是含有信息的,所以也要输出,所以结束输出的条件为p == NULL

  1. void output()
  2. {
  3. if (isEmpty())
  4. {
  5. cout << "空链表无法输出!" << endl;
  6. throw 1;
  7. return;
  8. }
  9. node* p = (with_head) ? head->next : head;
  10. //带头结点的链表信息输出
  11. if (with_head)
  12. {
  13. p->information = head->next->information;
  14. int count = 1;
  15. while (p != NULL)
  16. {
  17. cout << "第" << count << "个结点数据信息如下:" << endl;
  18. cout << "数字为:" << p->information->num << endl;
  19. cout << "信息为:" << p->information->intro << endl;
  20. count++;
  21. p = p->next;
  22. }
  23. }
  24. //不带头结点的链表信息输出
  25. else
  26. {
  27. //将初始数据输出
  28. cout << "第1" << "个结点数据信息如下:" << endl;
  29. cout << "数字为:" << head->information->num << endl;
  30. cout << "信息为:" << head->information->intro << endl;
  31. p->information = head->next->information;
  32. int count = 1;
  33. while (p != NULL)
  34. {
  35. cout << "第" << count+1 << "个结点数据信息如下:" << endl;
  36. cout << "数字为:" << p->information->num << endl;
  37. cout << "信息为:" << p->information->intro << endl;
  38. count++;
  39. p = p->next;
  40. }
  41. }
  42. }

3.更多有意思的方法实现

3.1按位查找结点(返回第i个结点的结点指针)

上面实现了一些基操,那么下面再来实现一些有趣的方法,按位查找首当其冲嗷!这个操作很简单,通过上面的方法实现,相信你已经熟练掌握了如何遍历一个链表,那只要在遍历的时候一遍遍历一遍数数,数到i的时候就直接return p;

在开写之前还是先考虑一手异常情况,当i<=0的时候是死都找不到的,直接回NULL(i=0的时候带头结点的可以找到头结点但这没意义所以一并当作异常处理掉!)

于是我们得到了如下代码:

  1. //检查无误!
  2. node* locate(int i)
  3. {
  4. //i<=0回空指针
  5. if (i <= 0)
  6. {
  7. return NULL;
  8. }
  9. node* current = head;//当前所在结点指针
  10. int k = (with_head) ? 0 : 1;//计数
  11. while (current != NULL && k < i)
  12. {
  13. current = current->next;
  14. k++;
  15. }
  16. return current;
  17. }

3.2按值查找(返回其所在结点是第几个)

按位和按值总是被一起提及,那我们也不能棒打鸳鸯,既然写了按位查找,就再写一个按值查找吧!

首先考虑一手异常情况

1.空表,没东西给你找

2.要找的数据压根找不到,返回-1(效仿py哈哈哈

然后寻找所用的结点指针p初始化时要考虑带不带头结点(用那个三目运算符,老生常谈了属于是)

所以逻辑顺序是

处理异常->找找找(找到了立刻return然后开溜)->找不到(回-1)

  1. int find(datas& x)
  2. {
  3. node* p = (with_head) ? head->next : head;
  4. int i = 1;
  5. if (isEmpty())
  6. {
  7. cout << "无数据可供查找!" << endl;
  8. throw 2;
  9. }
  10. while ((p->information->num != x.num) || (p->information->intro != x.intro))
  11. {
  12. if (p->next == NULL)
  13. break;
  14. p = p->next;
  15. i++;
  16. }
  17. if ((p->information->num == x.num) && (p->information->intro == x.intro))
  18. {
  19. return i;
  20. }
  21. //找不到
  22. return -1;
  23. }

3.3保存第k个结点的信息(需要提供一个datas结构体来存储)

首先看到第k个,马上想到异常——空表和当k溢出范围,即当k不属于区间[1,length]

那如果k合法且表不空呢?该如何存储数据呢?

很简单嘛,用3.1实现的locate方法,locate(i)返回的是第i个结点的地址信息那我们通过对它所指向的结构体指针infromation取个址就得到了所需数据datas啦!

于是得到下面的代码:

  1. void save_data(int k, datas& saved)
  2. {
  3. if (isEmpty())
  4. {
  5. cout << "鬼!这里没东西可存!" << endl;
  6. throw 1;
  7. return;
  8. }
  9. else if (k < 0 || k > length())
  10. {
  11. cout << "所给下标溢出范围" << endl;
  12. throw 2;
  13. return;
  14. }
  15. else
  16. {
  17. saved = *(locate(k)->information);
  18. }
  19. }

3.4按位删除结点

接下来实现一手按位删除元素的操作,还记得之前写清空链表时的操作嘛?要删掉一个结点很简单,找到它上一个结点并将他先连接到下下个结点,再杀掉当前的待删结点。

而待删结点前一个结点怎么获得呢?欸嘿,直接locate(k-1)

异常处理和按位寻找一样,所以直接上代码:

  1. datas remove(int k)
  2. {
  3. //异常处理
  4. if(k<1 || k>length())
  5. {
  6. cout << "下标不合法!" << endl;
  7. throw 1;
  8. }
  9. if (isEmpty())
  10. {
  11. cout << "所要移去的结点为空结点" << endl;
  12. throw 1;
  13. }
  14. node* p = locate(k - 1);
  15. node* del = p->next; //被删除的结点的指针
  16. p->next = del->next;
  17. datas* save = new datas;
  18. save->num = del->information->num;
  19. save->intro = del->information->intro;
  20. delete del;
  21. return *save;
  22. }

3.5按值删除

按值删除只需要先处理异常,然后通过前面实现的find方法获取待删下标,再按位删除就好啦!

代码如下:

  1. datas remove_num(int num)
  2. {
  3. if(isEmpty())
  4. {
  5. cout << "空表无法进行remove操作" << endl;
  6. throw 1;
  7. }
  8. int index = find(num);
  9. if (index == -1)
  10. {
  11. return;
  12. }
  13. return remove(index);
  14. }

3.6翻转链表

想要翻转一个链表可以怎么做捏?我想到的方法是:

1.先把尾结点挪到第一个数据结点前,并记录这个被挪动的结点指针end

2.不断将尾结点挪动到end所指的结点上,并刷新end

3.重复第二步直到第一个数据点的next指向NULL

具体如下图:

 上图考虑的是带头结点的情况,此时不难发现只要将head->next指向end就完成了翻转

那么如果不带头结点呢?那只要把head指向end就好了,因为end指向的是翻转后的第一个数据结点。

于是我们得到了如下代码:

  1. //翻转链表
  2. void reverse()
  3. {
  4. if (isEmpty())
  5. {
  6. cout << "空表无法反转!" << endl;
  7. throw 1;
  8. return;
  9. }
  10. node* start = (withHead()) ? head->next : head;
  11. node* end = last();
  12. node* pre = start;
  13. //翻转
  14. while (start->next != NULL)
  15. {
  16. //暂时储存下一节点的next信息
  17. node* temp = start->next->next;
  18. //下一节点的next接到前一结点
  19. start->next->next = pre;
  20. //储存前一结点信息以便下一次拼接
  21. pre = start->next;
  22. //当前结点的next后跳一位
  23. start->next = temp;
  24. }
  25. //重新赋值head
  26. if (withHead())
  27. {
  28. //有头指针时,反转后将头结点指向end
  29. head->next = end;
  30. }
  31. else
  32. {
  33. //无头指针时,翻转后只需要把end赋给head
  34. head = end;
  35. }
  36. }

4.插值

因为插值是我作业的重点任务,所以单独拎出来说说,而且链表的插值还要判别下单调性啥的,还蛮有意思。

4.1按位插入

到了这个地步,按位插入拆分成按位和插入的话只需要解决后者了,因为找到该插入的结点可以直接locate一手,欸嘿!

那我们只要明确一下插入的逻辑,想将datas的数据插入第k个结点,那也就是相当于原来的第k-1个结点要与新结点连接并且新节点的next指向原第k个结点。

封装成新节点十分简单:

  1. node* p = new node;
  2. *(p->information) = ins_data;//ins_data为输入的要插入的数据

如果新节点指针为p,那么上述逻辑写成代码就是:

  1. node* current = locate(k-1);
  2. p->next = current->next;
  3. current->next = p;

 于是我们得到了按位插入方法:

  1. void insert(datas ins_data, int k)
  2. {
  3. node* current = locate(k - 1);
  4. node* p = new node;//插入结点
  5. //将插入数据放入插入节点
  6. p->information->intro = ins_data.intro;
  7. p->information->num = ins_data.num;
  8. //插入结点
  9. p->next = current->next;
  10. current->next = p;
  11. }

4.2单调性判别

看着这个标题直接梦回大一学高数那会哈哈!在线性表那我有说过按值插入的前提是序列有序,那么在进行按值插入前我们就得先完成一个用来甄别链表是否具有有序性的方法。

首先确定函数类型,由于单调性有三种情况——1.无单调性2.递增3.递减

所以简单的bool型已经无法满足我们的需求,所以我选择采用整形,返回值为0代表无单调性,1代表递增,2代表递减。

首先考虑特殊情况——1.空表(无单调性)2.长度为1的链表(这个情况的处理我直接当成递增了)

于是得到以下代码:

  1. if (isEmpty())
  2. {
  3. cout << "空表无单调性!" << endl;
  4. return 0;
  5. }
  6. if (length() == 1)
  7. {
  8. return 1;
  9. }

接着来做初始化,有两个东西得初始化,第一个自然是老生常谈的结点指针p,因为判断单调性从有数据的结点开始就好了所以node* p = (with_head) ? head->next : head;

第二个则是判断一开始的单调性(通过判断第一个和第二个的大小来获得初始单调性)

int sign = (p->information->num < p->next->information->num) ? 1 : 2;//1增2减

接着便是遍历链表,看之后的大小关系是否会打脸sign初始值,即如果第一个节点的num比第二个的大,那初始单调性是递增,如果后来出现一个结点的num值反倒小于后一个结点的num则属于是打脸了,一打脸就代表该链表没得单调性,直接return 0;

  1. //判断是否具有单调性
  2. switch (sign)
  3. {
  4. case 1://递增
  5. while (p->next != NULL)
  6. {
  7. if (p->information->num > p->next->information->num)
  8. {
  9. return 0;
  10. }
  11. p = p->next;
  12. }
  13. return 1;
  14. break;
  15. case 2://递减
  16. while (p->next != NULL)
  17. {
  18. if (p->information->num < p->next->information->num)
  19. {
  20. return 0;
  21. }
  22. p = p->next;
  23. }
  24. return 2;
  25. break;
  26. default:
  27. return 0;
  28. break;
  29. }

至此我们便获得了一个用于判断单调性的函数:

  1. int single()
  2. {
  3. if (isEmpty())
  4. {
  5. cout << "空表无单调性!" << endl;
  6. return 0;
  7. }
  8. node* p = (withHead()) ? head->next : head;
  9. if (length() == 1)
  10. {
  11. return 1;
  12. }
  13. int sign = (p->information->num < p->next->information->num) ? 1 : 2;//1增2减
  14. //判断是否具有单调性
  15. switch (sign)
  16. {
  17. case 1://递增
  18. while (p->next != NULL)
  19. {
  20. if (p->information->num > p->next->information->num)
  21. {
  22. return 0;
  23. }
  24. p = p->next;
  25. }
  26. return 1;
  27. break;
  28. case 2://递减
  29. while (p->next != NULL)
  30. {
  31. if (p->information->num < p->next->information->num)
  32. {
  33. return 0;
  34. }
  35. p = p->next;
  36. }
  37. return 2;
  38. break;
  39. default:
  40. return 0;
  41. break;
  42. }
  43. }

4.3按值插入

有了上述两个方法就很好办啦!逻辑也很简单:

先判断该链表有没有单调性,没有的话没法按值插入

有单调性则先比较两头,比较一头一尾,因为按位插入基于locate方法,所以没法将待插结点变成头或尾。

所以对先将一头一尾与待插数据进行大小比较,插头插尾拎出来考虑。

该插入的地方既不在头也不在尾则遍历链表寻找合适的index,这里注意如果待插结点在第i个结点和第i+1个结点符合递变规律,那么应该让它成为新的第i+1个结点,即insert(ins_data, index+1)

于是我们得到了按值插入函数:

  1. void insert(datas ins_data)
  2. {
  3. //初始化为第一个有信息结点用来判断特殊情况
  4. node* p = (withHead()) ? head->next : head;
  5. int index = 1;//当前处于第一个结点
  6. switch (single())
  7. {
  8. case 0://无序时抛错
  9. cout << "该链表数字排列无序,无法按值插入!" << endl;
  10. throw 1;
  11. return;
  12. break;
  13. case 1://递增排列
  14. //比第一个还小就插在第一个
  15. if (ins_data.num <= p->information->num)
  16. {
  17. insert(ins_data, 1);
  18. }
  19. //比最后一个还大就插在尾
  20. else if (ins_data.num >= last()->information->num)
  21. {
  22. node* ins_node = new node;
  23. //插入值赋给插入节点
  24. *(ins_node->information) = ins_data;
  25. last()->next = ins_node;//插入尾结点后
  26. }
  27. //不然就寻找合适的位置下标index
  28. else
  29. {
  30. while (1)
  31. {
  32. if (ins_data.num >= p->information->num && //比这一个大
  33. ins_data.num <= p->next->information->num)//比下一个小
  34. {
  35. insert(ins_data, index + 1);
  36. break;
  37. }//循环边界,当ins_data.num在第index和第index+1数据之间则插成第index+1个
  38. p = p->next;
  39. index++;
  40. }
  41. }
  42. break;
  43. case 2://递减排列
  44. //比第一个还大就插在第一个
  45. if (ins_data.num >= p->information->num)
  46. {
  47. insert(ins_data, 1);
  48. }
  49. //比最后一个还小就插在尾
  50. else if (ins_data.num <= last()->information->num)
  51. {
  52. node* ins_node = new node;
  53. //插入值赋给插入节点
  54. *(ins_node->information) = ins_data;
  55. last()->next = ins_node;//插入尾结点后
  56. }
  57. //不然就寻找合适的位置下标index
  58. else
  59. {
  60. while (1)
  61. {
  62. if (ins_data.num <= p->information->num && //比这一个小
  63. ins_data.num >= p->next->information->num)//比下一个大
  64. {
  65. insert(ins_data, index + 1);
  66. break;
  67. }//循环边界,当ins_data.num在第index和第index+1数据之间则插成第index+1个
  68. p = p->next;
  69. index++;
  70. }
  71. }
  72. break;
  73. break;
  74. default:
  75. break;
  76. }
  77. }

4.4改进按位插入

通过4.3发现之前的按位插入漏掉了一头一尾两种情况

如果要插入最头上还应分成是否带有头结点来考虑,即:

  1. if (k == 1)//要插成第一个
  2. {
  3. if (with_head)
  4. {
  5. p->next = head->next;
  6. head->next = p;
  7. }
  8. else
  9. {
  10. p->next = head;
  11. head = p;
  12. }
  13. }

插成尾就比较简单,直接:

last()->next = p;

即可,所以改进后的按位插入函数为:

  1. void insert(datas ins_data, int k)
  2. {
  3. node* p = new node;//插入结点
  4. //将插入数据放入插入节点
  5. p->information->intro = ins_data.intro;
  6. p->information->num = ins_data.num;
  7. if (k == 1)//要插成第一个
  8. {
  9. if (with_head)
  10. {
  11. p->next = head->next;
  12. head->next = p;
  13. }
  14. else
  15. {
  16. p->next = head;
  17. head = p;
  18. }
  19. }
  20. else if (k == length() + 1)//代表成为新尾结点
  21. {
  22. last()->next = p;
  23. }
  24. node* current = locate(k - 1);
  25. //插入结点
  26. p->next = current->next;
  27. current->next = p;
  28. }

5.一些测试函数(自己调试的时候写的)

5.1方法函数测试

  1. void Func_test()
  2. {
  3. LinkList L1;
  4. L1.input();
  5. L1.output();
  6. cout << L1.isEmpty() << endl;
  7. cout << L1.length() << endl;
  8. datas d = { 2, "b" };
  9. cout << L1.find(d) << endl;
  10. if (L1.locate(L1.find(d))->information->num == 2)
  11. {
  12. cout << "locate函数检查无误!" << endl;
  13. }
  14. else
  15. {
  16. cout << "locate函数有误!" << endl;
  17. }
  18. cout << "尾部数字为:" << L1.last()->information->num << endl;
  19. //检查单调性
  20. if (L1.single() == 0)
  21. {
  22. cout << "无单调性" << endl;
  23. }
  24. else
  25. {
  26. string dd_str = (L1.single() == 1) ? "递增" : "递减";
  27. cout << dd_str << endl;
  28. }
  29. //检查按值插入
  30. datas insert_data = { 2, "被按值插入的数字" };
  31. L1.insert(insert_data, L1.withHead());
  32. L1.output();
  33. //检查按位插入
  34. insert_data = { 0, "被按位插入的数字" };
  35. L1.insert(insert_data, 1);
  36. L1.output();
  37. //检查remove
  38. datas rem, temp;//用来储存被删除的结点信息
  39. int length = L1.length();
  40. temp = *(L1.locate(1)->information);
  41. rem = L1.remove(1);
  42. int length_ = L1.length();
  43. if (length_ == length - 1 && rem.num == temp.num && rem.intro == temp.intro)
  44. {
  45. cout << "remove函数检查无误!" << endl;
  46. }
  47. else
  48. {
  49. cout << "remove函数出问题咯!" << endl;
  50. }
  51. //翻转
  52. L1.reverse();
  53. L1.output();
  54. //检查makeEmpty
  55. L1.makeEmpty();
  56. string mE_ass_err = (L1.length() == 0) ? "makeEmpty函数正确无误!" :
  57. "makeEmpty函数有问题";
  58. cout << mE_ass_err << endl;
  59. }

5.2带头结点链表测试

  1. void head_display()
  2. {
  3. LinkList L1;
  4. L1.input();
  5. L1.output();
  6. cout << L1.isEmpty() << endl;
  7. cout << L1.length() << endl;
  8. datas d = { 2, "b" };
  9. cout << L1.find(d) << endl;
  10. if (L1.locate(L1.find(d))->information->num == 2)
  11. {
  12. cout << "locate函数检查无误!" << endl;
  13. }
  14. else
  15. {
  16. cout << "locate函数有误!" << endl;
  17. }
  18. cout << "尾部数字为:" << L1.last()->information->num << endl;
  19. //检查单调性
  20. if (L1.single() == 0)
  21. {
  22. cout << "无单调性" << endl;
  23. }
  24. else
  25. {
  26. string dd_str = (L1.single() == 1) ? "递增" : "递减";
  27. cout << dd_str << endl;
  28. }
  29. //检查按值插入
  30. datas insert_data = { 30, "被插入的数字" };
  31. L1.insert(insert_data);
  32. L1.output();
  33. //检查按位插入
  34. insert_data = { 2, "被插入的数字" };
  35. L1.insert(insert_data, 1);
  36. L1.output();
  37. //检查save_data
  38. datas save_data;
  39. L1.save_data(3, save_data);
  40. cout << "--------被保存的是第三个结点,信息如下----------" << endl;
  41. cout << "num:" << save_data.num << endl;
  42. cout << "intro:" << save_data.intro << endl;
  43. datas remove_data = L1.remove(2);
  44. L1.output();
  45. cout << "------被删除的结点信息如下--------" << endl;
  46. cout << "num:" << remove_data.num << endl;
  47. cout << "intro:" << remove_data.intro << endl;
  48. //翻转演示
  49. L1.reverse();
  50. cout << "------翻转后的链表信息如下--------" << endl;
  51. L1.output();
  52. //清空检查
  53. L1.makeEmpty();
  54. cout << "清空后链表长度为:" << L1.length() << endl;
  55. }

5.3不带头结点链表测试

  1. void nohead_display()
  2. {
  3. datas* init_data = new datas;
  4. *init_data = { 0, "初始结点" };
  5. node* init_node = new node;
  6. init_node->information = init_data;
  7. //构造无头结点的链表
  8. LinkList L1(init_node);
  9. L1.input();
  10. L1.output();
  11. cout << "第2个结点数字为" << L1.locate(2)->information->num << endl;
  12. cout << "------长度如下------" << endl;
  13. cout << L1.length() << endl;
  14. //按位插值检验
  15. datas ins_data = { 10, "被插入的结点" };
  16. L1.insert(ins_data, 2);
  17. L1.output();
  18. //按位删除检查
  19. datas remove_data = L1.remove(2);
  20. L1.output();
  21. cout << "------被删除的结点信息如下--------" << endl;
  22. cout << "num:" << remove_data.num << endl;
  23. cout << "intro:" << remove_data.intro << endl;
  24. //单调性检查
  25. if (L1.single() == 0)
  26. {
  27. cout << "无单调性" << endl;
  28. }
  29. else
  30. {
  31. string ddx = (L1.single() == 1) ? "递增" : "递减";
  32. cout << ddx << endl;
  33. }
  34. //按值插入检查
  35. ins_data = { 15, "被插入的结点" };
  36. L1.insert(ins_data, L1.withHead());
  37. L1.output();
  38. //翻转演示
  39. L1.reverse();
  40. cout << "------翻转后的链表信息如下--------" << endl;
  41. L1.output();
  42. }

6.LinkList类整合

  1. class LinkList
  2. {
  3. public:
  4. //无参构造,带头结点的链表
  5. LinkList()
  6. {
  7. head = new node;//给头结点分配内存
  8. with_head = true;
  9. };
  10. //带参构造,无头结点的链表
  11. LinkList(node* x)
  12. {
  13. head = x;
  14. with_head = false;//不带头结点
  15. };
  16. //析构函数(用于释放内存,防止内存泄露)
  17. ~LinkList()
  18. {
  19. makeEmpty();
  20. }
  21. bool isEmpty()
  22. {
  23. return (with_head) ? head->next == NULL : head == NULL;
  24. }
  25. node* get_head()
  26. {
  27. return head;
  28. }
  29. bool withHead()
  30. {
  31. return with_head;
  32. }
  33. node* last()
  34. {
  35. if (isEmpty())
  36. {
  37. return NULL;
  38. }
  39. node* p = head;
  40. while ( p->next != NULL )
  41. {
  42. p = p->next;
  43. }
  44. return p;
  45. }
  46. void makeEmpty()
  47. {
  48. if (isEmpty())
  49. {
  50. cout << "空的不能再空啦!" << endl;
  51. throw 1;
  52. return;
  53. }
  54. node* p;
  55. while (head->next != NULL)
  56. {
  57. p = head->next;
  58. head->next = p->next;
  59. delete p;
  60. }
  61. if (!with_head)
  62. head = NULL;
  63. }
  64. int length()
  65. {
  66. if (isEmpty())
  67. return 0;
  68. node*p = (with_head) ? head->next : head;
  69. int count = 1;
  70. while ( p->next != NULL )
  71. {
  72. p = p->next;
  73. count++;
  74. }
  75. return count;
  76. }
  77. void input(datas endTag={ 0, "结束" }, bool front=true)
  78. {
  79. //为新元素申请结点
  80. node* p;
  81. datas* new_data = new datas;
  82. if (front)//前插法构造链表
  83. {
  84. cout << "请输入第一个数据:";
  85. cin >> new_data->num;
  86. cin >> new_data->intro;
  87. //直接输入结束符,立刻开溜
  88. if ((new_data->num == endTag.num) && (new_data->intro == endTag.intro))
  89. {
  90. return;
  91. }
  92. //不满足结束标志则持续输入
  93. while ((new_data->num != endTag.num) || (new_data->intro != endTag.intro))
  94. {
  95. p = new node;//为新结点申请空间
  96. p->information->num = new_data->num;
  97. p->information->intro = new_data->intro;
  98. if (head->next != NULL)
  99. {
  100. p->next = head->next;
  101. }
  102. head->next = p;
  103. cout << "请输入数据:";
  104. cin >> new_data->num;
  105. cin >> new_data->intro;
  106. }
  107. }
  108. else//尾插法构造链表
  109. {
  110. cout << "请输入第一个数据:";
  111. cin >> new_data->num;
  112. cin >> new_data->intro;
  113. if ((new_data->num == endTag.num) && (new_data->intro == endTag.intro))
  114. {
  115. return;
  116. }
  117. //不满足结束标志则持续输入
  118. while ((new_data->num != endTag.num) || (new_data->intro != endTag.intro))
  119. {
  120. p = new node;//为新结点申请空间
  121. p->information->num = new_data->num;
  122. p->information->intro = new_data->intro;
  123. last()->next = p;
  124. cout << "请输入数据:";
  125. cin >> new_data->num;
  126. cin >> new_data->intro;
  127. }
  128. }
  129. }
  130. void output()
  131. {
  132. if (isEmpty())
  133. {
  134. cout << "空链表无法输出!" << endl;
  135. throw 1;
  136. return;
  137. }
  138. node* p = (with_head) ? head->next : head;
  139. //带头结点的链表信息输出
  140. if (with_head)
  141. {
  142. p->information = head->next->information;
  143. int count = 1;
  144. while (p != NULL)
  145. {
  146. cout << "第" << count << "个结点数据信息如下:" << endl;
  147. cout << "数字为:" << p->information->num << endl;
  148. cout << "信息为:" << p->information->intro << endl;
  149. count++;
  150. p = p->next;
  151. }
  152. }
  153. //不带头结点的链表信息输出
  154. else
  155. {
  156. //将初始数据输出
  157. cout << "第1" << "个结点数据信息如下:" << endl;
  158. cout << "数字为:" << head->information->num << endl;
  159. cout << "信息为:" << head->information->intro << endl;
  160. p->information = head->next->information;
  161. int count = 1;
  162. while (p != NULL)
  163. {
  164. cout << "第" << count+1 << "个结点数据信息如下:" << endl;
  165. cout << "数字为:" << p->information->num << endl;
  166. cout << "信息为:" << p->information->intro << endl;
  167. count++;
  168. p = p->next;
  169. }
  170. }
  171. }
  172. //检查无误!
  173. node* locate(int i)
  174. {
  175. //i<=0回空指针
  176. if (i <= 0)
  177. {
  178. return NULL;
  179. }
  180. node* current = head;//当前所在结点指针
  181. int k = (with_head) ? 0 : 1;//计数
  182. while (current != NULL && k < i)
  183. {
  184. current = current->next;
  185. k++;
  186. }
  187. return current;
  188. }
  189. int find(datas& x)
  190. {
  191. node* p = (with_head) ? head->next : head;
  192. int i = 1;
  193. if (isEmpty())
  194. {
  195. cout << "无数据可供查找!" << endl;
  196. throw 2;
  197. }
  198. while ((p->information->num != x.num) || (p->information->intro != x.intro))
  199. {
  200. if (p->next == NULL)
  201. break;
  202. p = p->next;
  203. i++;
  204. }
  205. if ((p->information->num == x.num) && (p->information->intro == x.intro))
  206. {
  207. return i;
  208. }
  209. //找不到
  210. return -1;
  211. }
  212. void save_data(int k, datas& saved)
  213. {
  214. if (isEmpty())
  215. {
  216. cout << "鬼!这里没东西可存!" << endl;
  217. throw 1;
  218. return;
  219. }
  220. else if (k < 0 || k > length())
  221. {
  222. cout << "所给下标溢出范围" << endl;
  223. throw 2;
  224. return;
  225. }
  226. else
  227. {
  228. saved = *(locate(k)->information);
  229. }
  230. }
  231. datas remove(int k)
  232. {
  233. //异常处理
  234. if(k<1 || k>length())
  235. {
  236. cout << "下标不合法!" << endl;
  237. throw 1;
  238. }
  239. if (isEmpty())
  240. {
  241. cout << "所要移去的结点为空结点" << endl;
  242. throw 1;
  243. }
  244. node* p = locate(k - 1);
  245. node* del = p->next; //被删除的结点的指针
  246. p->next = del->next;
  247. datas* save = new datas;
  248. save->num = del->information->num;
  249. save->intro = del->information->intro;
  250. delete del;
  251. return *save;
  252. }
  253. datas remove_num(int num)
  254. {
  255. if(isEmpty())
  256. {
  257. cout << "空表无法进行remove操作" << endl;
  258. throw 1;
  259. }
  260. int index = find(num);
  261. if (index == -1)
  262. {
  263. return;
  264. }
  265. return remove(index);
  266. }
  267. //翻转链表
  268. void reverse()
  269. {
  270. if (isEmpty())
  271. {
  272. cout << "空表无法反转!" << endl;
  273. throw 1;
  274. return;
  275. }
  276. node* start = (withHead()) ? head->next : head;
  277. node* end = last();
  278. node* pre = start;
  279. //翻转
  280. while (start->next != NULL)
  281. {
  282. //暂时储存下一节点的next信息
  283. node* temp = start->next->next;
  284. //下一节点的next接到前一结点
  285. start->next->next = pre;
  286. //储存前一结点信息以便下一次拼接
  287. pre = start->next;
  288. //当前结点的next后跳一位
  289. start->next = temp;
  290. }
  291. //重新赋值head
  292. if (withHead())
  293. {
  294. //有头指针时,反转后将头结点指向end
  295. head->next = end;
  296. }
  297. else
  298. {
  299. //无头指针时,翻转后只需要把end赋给head
  300. head = end;
  301. }
  302. }
  303. int single()
  304. {
  305. if (isEmpty())
  306. {
  307. cout << "空表无单调性!" << endl;
  308. return 0;
  309. }
  310. node* p = (withHead()) ? head->next : head;
  311. if (length() == 1)
  312. {
  313. return 1;
  314. }
  315. int sign = (p->information->num < p->next->information->num) ? 1 : 2;//1增2减
  316. //判断是否具有单调性
  317. switch (sign)
  318. {
  319. case 1://递增
  320. while (p->next != NULL)
  321. {
  322. if (p->information->num > p->next->information->num)
  323. {
  324. return 0;
  325. }
  326. p = p->next;
  327. }
  328. return 1;
  329. break;
  330. case 2://递减
  331. while (p->next != NULL)
  332. {
  333. if (p->information->num < p->next->information->num)
  334. {
  335. return 0;
  336. }
  337. p = p->next;
  338. }
  339. return 2;
  340. break;
  341. default:
  342. return 0;
  343. break;
  344. }
  345. }
  346. void insert(datas ins_data, int k)
  347. {
  348. node* p = new node;//插入结点
  349. //将插入数据放入插入节点
  350. p->information->intro = ins_data.intro;
  351. p->information->num = ins_data.num;
  352. if (k == 1)//要插成第一个
  353. {
  354. if (with_head)
  355. {
  356. p->next = head->next;
  357. head->next = p;
  358. }
  359. else
  360. {
  361. p->next = head;
  362. head = p;
  363. }
  364. }
  365. else if (k == length() + 1)//代表成为新尾结点
  366. {
  367. last()->next = p;
  368. }
  369. node* current = locate(k - 1);
  370. //插入结点
  371. p->next = current->next;
  372. current->next = p;
  373. }
  374. void insert(datas ins_data)
  375. {
  376. //初始化为第一个有信息结点用来判断特殊情况
  377. node* p = (withHead()) ? head->next : head;
  378. int index = 1;//当前处于第一个结点
  379. switch (single())
  380. {
  381. case 0://无序时抛错
  382. cout << "该链表数字排列无序,无法按值插入!" << endl;
  383. throw 1;
  384. return;
  385. break;
  386. case 1://递增排列
  387. //比第一个还小就插在第一个
  388. if (ins_data.num <= p->information->num)
  389. {
  390. insert(ins_data, 1);
  391. }
  392. //比最后一个还大就插在尾
  393. else if (ins_data.num >= last()->information->num)
  394. {
  395. node* ins_node = new node;
  396. //插入值赋给插入节点
  397. *(ins_node->information) = ins_data;
  398. last()->next = ins_node;//插入尾结点后
  399. }
  400. //不然就寻找合适的位置下标index
  401. else
  402. {
  403. while (1)
  404. {
  405. if (ins_data.num >= p->information->num && //比这一个大
  406. ins_data.num <= p->next->information->num)//比下一个小
  407. {
  408. insert(ins_data, index + 1);
  409. break;
  410. }//循环边界,当ins_data.num在第index和第index+1数据之间则插成第index+1个
  411. p = p->next;
  412. index++;
  413. }
  414. }
  415. break;
  416. case 2://递减排列
  417. //比第一个还大就插在第一个
  418. if (ins_data.num >= p->information->num)
  419. {
  420. insert(ins_data, 1);
  421. }
  422. //比最后一个还小就插在尾
  423. else if (ins_data.num <= last()->information->num)
  424. {
  425. node* ins_node = new node;
  426. //插入值赋给插入节点
  427. *(ins_node->information) = ins_data;
  428. last()->next = ins_node;//插入尾结点后
  429. }
  430. //不然就寻找合适的位置下标index
  431. else
  432. {
  433. while (1)
  434. {
  435. if (ins_data.num <= p->information->num && //比这一个小
  436. ins_data.num >= p->next->information->num)//比下一个大
  437. {
  438. insert(ins_data, index + 1);
  439. break;
  440. }//循环边界,当ins_data.num在第index和第index+1数据之间则插成第index+1个
  441. p = p->next;
  442. index++;
  443. }
  444. }
  445. break;
  446. default:
  447. break;
  448. }
  449. }
  450. private:
  451. node* head;//头节点指针
  452. bool with_head;
  453. };

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/703441
推荐阅读
相关标签
  

闽ICP备14008679号