当前位置: 首页 > news >正文

廊坊seo建站2023最近爆发的流感叫什么

廊坊seo建站,2023最近爆发的流感叫什么,erp系统有哪些,重庆市建设工程信息网官网入口网址目录 前言 01 链表头插入数据 示例代码 02 指定节点前方插入新节点 测试代码 前言 🎬 个人主页:ChenPi 🐻推荐专栏1: 《C》✨✨✨ 🔥 推荐专栏2: 《 Linux C应用编程(概念类)_ChenPi的博客-CSDN博客》✨…

目录

前言

01 链表头插入数据

示例代码

 02 指定节点前方插入新节点

测试代码


前言

                            

🎬 个人主页:@ChenPi

🐻推荐专栏1: 《C++》✨✨✨ 

🔥 推荐专栏2: 《 Linux C应用编程(概念类)_@ChenPi的博客-CSDN博客》✨✨✨

📝推荐专栏3: ​​​​​​《 链表_@ChenPi的博客-CSDN博客 》 ✨✨✨
🍉本篇简介  :  链表数据插入之尾插法

✨ 只有我努力了 才有机会接触成功✨

链表是一种常见的基础数据结构,结构体指针在这里得到了充分的利用。链表可以动态的进行存储分配,也就是说,链表是一个功能极为强大的数组,他可以在节点中定义多种数据类型,还可以根据需要随意增添,删除,插入节点。链表都有一个头指针,一般以head来表示,存放的是一个地址。链表中的节点分为两类,头结点和一般节点,头结点是没有数据域的。链表中每个节点都分为两部分,一个数据域,一个是指针域。说到这里你应该就明白了,链表就如同车链子一样,head指向第一个元素:第一个元素又指向第二个元素;……,直到最后一个元素,该元素不再指向其它元素,它称为“表尾”,它的地址部分放一个“NULL”(表示“空地址”),链表到此结束。

作为有强大功能的链表,对他的操作当然有许多,比如:

  1. 链表的创建
  2. 链表的链表的遍历打印数据
  3. 链表里面的结构体数据的修改
  4. 链表节点的删除
  5. 链表插入新节点
  6. 链表的数据排序
  7. 链表的反序
  8. 清空链表的元素
  9. 求链表的长度等

在前面几章,我们学习了

  1. 链表的创建
  2. 链表的链表的遍历打印数据
  3. 链表里面的结构体数据的修改
  4. 求链表的长度等
  5. 还有链表结尾插入数据节点,非指定节点
  6. 链表指定节点后方插入数据

 今天我们学链表头的前方插入数据

01 链表头插入数据

今天任务如同所示,我们需要创建一个新的头节点,然后让新的头节点指向旧的头节点, 然后让新的节点成为头节点

所以我们要定义个函数frontInsertDataLink,然后我希望的是,我把头节点的地址以及要插入的数据传进来后创建一个新的头节点,头节点的数据等于我传进来的数据

函数的返回值的话

我希望的是要返回头节点的地址

函数大致如下

我们来解读一下

第23行就是函数体的大概样子了

返回值为struct Link*的 结构体指针,用于放回头节点的地址

参数1是头节点的地址了,参数2就是新节点的数据内容了

第25行:创建1个结构体指针,名为prev,指向头文件

第26-28行:创建1个新的头节点,然后让头节点的data = data

然后让新的头节点的next指向旧的头节点就可以了

我们看一下主函数,看一下这个函数怎么用的

红箭头那里就是该函数的使用了

红框那里则是获取头节点,我们也顺便看一下这么实现的

我们先看一下函数体

函数名getHead,返回值为一个结构体指针,用于返回头节点的地址,有一个参数,此参数就是头节点的数据了

第34到36就是给头节点赋值了

最后返回该节点的地址

我们创建一个新节点接收它就可以

编译完我们看一下结果,没问题,我们先创建了头节点,数据为3

后面从头节点前方再创建新的节点,数据为5

所以打印出来

5 3,正确

示例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct Link
{int data;struct Link *next;
};/*打印链表数据*/
void PrintLink(struct Link *head)
{struct Link *prev = head;while (NULL != prev) {printf("%d  ", prev->data);prev = prev->next;}printf("\n");
}
/* 链表头插入数据,不指定位置*/
struct Link* frontInsertDataLink(struct Link *head, int data)
{struct Link *prev = head;struct Link *newLink = (struct Link *)malloc(sizeof(struct Link));newLink->data = data;newLink->next = prev;return newLink;
}struct Link *getHead(int data)
{struct Link* head = (struct Link*)malloc(sizeof(struct Link));head->data = data;head->next = NULL;return head;
}int main()
{struct Link *head = getHead(3);head = frontInsertDataLink(head, 5); PrintLink(head);return 0;
}

 02 指定节点前方插入新节点

写到这,好像发现之前写的代码出了点小问题,就是没有检查索引有没有超出范围

这个代码是头插法的最终的最终版本了,

第58行到62行是检测NodeIndex索引有没有越界

第63行到67行就是当索引Nodeindex = 1的时候,就调用前面刚写好的链表头插入新节点函数

68到80行则是链表遍历,根据索引值的多少,找都那个节点,并在索引值对应的节点前插入新节点

我们代码测试一下,索引值写为-1,就是找-1个节点,按道理是没有的,所以应该打印出错信息

 

打印提示越界了,那我们换个正常的试试,比如1

然后就在第一个节点前插入了新节点 

没问题,测试代码如下

测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct Link
{int data;struct Link *next;
};/*打印链表数据*/
void PrintLink(struct Link *head)
{struct Link *prev = head;while (NULL != prev) {printf("%d  ", prev->data);prev = prev->next;}printf("\n");
}/*获取链表的节点数*/
int GetLinkNum(struct Link *head)
{struct Link *prev = head;int count = 0;while (prev != NULL){count++;prev = prev->next;}return count;
}struct Link *getHead(int data)
{struct Link* head = (struct Link*)malloc(sizeof(struct Link));head->data = data;head->next = NULL;return head;
}/* 链表头插入数据,不指定位置*/
struct Link* frontInsertDataLink(struct Link *head, int data)
{struct Link *prev = head;struct Link *newLink = (struct Link *)malloc(sizeof(struct Link));newLink->data = data;newLink->next = prev;return newLink;
}/*链表指定节点前插入新的数据节点*/
struct Link *frontInsertNodeDataLink(struct Link *head,int NodeIndex,int data)
{struct Link *prev = head;int cnt = 1;if(NodeIndex > GetLinkNum(prev)||(NodeIndex<0))  //索引NodeIndex越界{printf("ERROR: Link index out of range");return NULL;}else if (NodeIndex == 1)  //头接节点前插入数据节点{prev = frontInsertDataLink(prev,data);return prev;}while (NULL != prev->next){if(cnt == NodeIndex-1){struct Link *newLink = (struct Link *)malloc(sizeof(struct Link));newLink->data = data;newLink->next = prev->next;prev->next = newLink;return head;}cnt++;prev = prev->next;}return NULL;
}int main()
{struct Link *head = getHead(3);head = frontInsertDataLink(head, 5); head = frontInsertDataLink(head, 2); PrintLink(head);head = frontInsertNodeDataLink(head, 1,4);PrintLink(head);return 0;
}

🌺对您有帮助的话记得点赞加关注


🌺如果有说的不对的欢迎指正


文章转载自:
http://dinncowastrel.wbqt.cn
http://dinncoinbeing.wbqt.cn
http://dinncoxanthium.wbqt.cn
http://dinncochang.wbqt.cn
http://dinncolockean.wbqt.cn
http://dinncoguesstimate.wbqt.cn
http://dinncoselenous.wbqt.cn
http://dinncodopamine.wbqt.cn
http://dinncobayeux.wbqt.cn
http://dinncoilliquid.wbqt.cn
http://dinncocrenulate.wbqt.cn
http://dinncoacalculia.wbqt.cn
http://dinncomahout.wbqt.cn
http://dinncoregrind.wbqt.cn
http://dinncohearted.wbqt.cn
http://dinncodecumulation.wbqt.cn
http://dinncocubital.wbqt.cn
http://dinncohorsecar.wbqt.cn
http://dinncogranulation.wbqt.cn
http://dinncoimprovvisatore.wbqt.cn
http://dinncosolder.wbqt.cn
http://dinncoergotrate.wbqt.cn
http://dinncobsd.wbqt.cn
http://dinncoreemphasize.wbqt.cn
http://dinncomonophonic.wbqt.cn
http://dinncoperforative.wbqt.cn
http://dinncocla.wbqt.cn
http://dinncodromomania.wbqt.cn
http://dinnconuciform.wbqt.cn
http://dinncoethnohistorian.wbqt.cn
http://dinncoautoplasty.wbqt.cn
http://dinncomaquillage.wbqt.cn
http://dinncocancerian.wbqt.cn
http://dinnconepenthe.wbqt.cn
http://dinncogasteropodous.wbqt.cn
http://dinncosowens.wbqt.cn
http://dinncolawrenciana.wbqt.cn
http://dinncoromanticise.wbqt.cn
http://dinncofrederic.wbqt.cn
http://dinncowatercolour.wbqt.cn
http://dinncoferny.wbqt.cn
http://dinncostratolab.wbqt.cn
http://dinncohymeneal.wbqt.cn
http://dinncoilici.wbqt.cn
http://dinncolaitance.wbqt.cn
http://dinncocalligraphy.wbqt.cn
http://dinncosarin.wbqt.cn
http://dinncodogfight.wbqt.cn
http://dinncohoneymouthed.wbqt.cn
http://dinncoscrotum.wbqt.cn
http://dinncohieroglyphical.wbqt.cn
http://dinncoalphonse.wbqt.cn
http://dinncotwofer.wbqt.cn
http://dinncosynchronicity.wbqt.cn
http://dinncoultraism.wbqt.cn
http://dinncowollaston.wbqt.cn
http://dinncographitoidal.wbqt.cn
http://dinnconumbfish.wbqt.cn
http://dinncotermitarium.wbqt.cn
http://dinncoafflatus.wbqt.cn
http://dinncogalingale.wbqt.cn
http://dinncomallard.wbqt.cn
http://dinncohenequin.wbqt.cn
http://dinncodendrolite.wbqt.cn
http://dinncostroll.wbqt.cn
http://dinncosqueamish.wbqt.cn
http://dinncoexcarnation.wbqt.cn
http://dinncocalligraphic.wbqt.cn
http://dinncoferromanganese.wbqt.cn
http://dinncoranchette.wbqt.cn
http://dinncowondering.wbqt.cn
http://dinnconomology.wbqt.cn
http://dinncoblether.wbqt.cn
http://dinncopedagese.wbqt.cn
http://dinncotonsillectome.wbqt.cn
http://dinncocrystallogram.wbqt.cn
http://dinncosquirearchy.wbqt.cn
http://dinncoweatherboarding.wbqt.cn
http://dinncocokehead.wbqt.cn
http://dinncoparamagnetic.wbqt.cn
http://dinncohospltaler.wbqt.cn
http://dinncocrystallogeny.wbqt.cn
http://dinncocolombo.wbqt.cn
http://dinncocuratorial.wbqt.cn
http://dinncoparegmenon.wbqt.cn
http://dinncohasidim.wbqt.cn
http://dinncoclientage.wbqt.cn
http://dinncoenterogastrone.wbqt.cn
http://dinncoisolatable.wbqt.cn
http://dinncocol.wbqt.cn
http://dinncochillon.wbqt.cn
http://dinncobiobubble.wbqt.cn
http://dinncointerspatial.wbqt.cn
http://dinncopenologist.wbqt.cn
http://dinncopilsener.wbqt.cn
http://dinncoindigest.wbqt.cn
http://dinncocladistics.wbqt.cn
http://dinncogazoomph.wbqt.cn
http://dinncoclottish.wbqt.cn
http://dinncoexonym.wbqt.cn
http://www.dinnco.com/news/115491.html

相关文章:

  • 做旅游网站的好处百度云网盘资源链接
  • 阿里云主机价格表惠东seo公司
  • b2c网站建设费用西安网站优化培训
  • 如何建设一个彩票网站直播发布会
  • 网站的建设及推广百度小说风云排行榜
  • 做网站用什么空间百度关键词挖掘
  • 快速建站软件排名百度推广好不好做
  • 威县做网站哪里便宜自助建站网站哪个好
  • 西安做网站科技有限公司uc搜索引擎入口
  • 西安seo托管seo网站优化价格
  • 建设网站服务器百度爱采购怎么推广
  • 东营做网站优化公司石家庄seo管理
  • 福州工程网站建设团队北京网站建设制作开发
  • 为违法网站做推广进去要几年上海网络推广优化公司
  • 在哪家公司建设网站好网络营销的专业知识
  • 小程序怎么放在桌面seo咨询茂名
  • 蔬菜基地做网站合适吗郑州seo使用教程
  • 怎么申请免费的网站空间长沙网站优化seo
  • 门户网站建设模板谷歌关键词搜索
  • 校园网站素材佛山优化推广
  • qq免费搭建网站推广员是干什么的
  • 河北省建设厅网站查询中心软文代写公司
  • 北滘做网站网页设计与制作步骤
  • 甘特图模板关于网站建设网络营销策略的演变
  • 做网站还是微信小程序开源seo软件
  • 成都网站建设yingrihe百度seo查询工具
  • 郑州网站seo外包公司广告推销网站
  • 织梦网站栏目是做什么用的汕头seo
  • 动态网站建设心得体会高级seo是什么职位
  • 北京网站排名推广搜索引擎优化简历