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

网站建设哪家好建议兴田德润怎样做自己的 优惠卷网站

网站建设哪家好建议兴田德润,怎样做自己的 优惠卷网站,新手小白如何写公众号文章,中国十大杰出建筑师前言:大家好😍,本文主要介绍数据结构——单链表 目录 一、单链表 二、使用步骤 1.结构体定义 2.初始化 3.插入 3.1 头插 3.2 尾插 3.3 按位置插 四.删除 4.1头删 4.2 尾删 4.3 按位置删 4.4按值删 五 统计有效值个数 六 销毁…

前言:大家好😍,本文主要介绍数据结构——单链表

目录

一、单链表

二、使用步骤

1.结构体定义

2.初始化

3.插入

3.1 头插

3.2 尾插 

3.3 按位置插

 四.删除

4.1头删

4.2 尾删

4.3 按位置删

4.4按值删

五 统计有效值个数 

六 销毁1

销毁2

七 查找

八 打印


一、单链表

概念:单链表逻辑上相邻,物理上不一定相邻的链式存储结构

所以一个结点不仅仅要保存自身的数据,还要保存自己下一个连接结点的地址

所以每一个结点需要俩个域:一个数据域,一个指针域

二、使用步骤

1.结构体定义

typedef int Elem_Type;
struct Node
{Elem_Type data;//数据域 存储有效数据struct Node* next;//指针域  存下一个有效结点的地址
};typedef struct Node Node;
typedef struct Node* pNode;
struct Node:定义了一个 结构体 Node,它包含两个成员:
Elem_Type data;: 数据域,用于存储节点中的数据。由于 Elem_Type 被定义为 int,所以这里的 data 可以存储一个整数。
struct Node* next;: 指针域,用于存储指向下一个节点的地址。这样,每个节点都可以通过 next 指针链接到下一个节点,形成链表。
typedef struct Node Node;:为结构体 Node 创建了一个别名 Node,这样在代码中可以直接 使用 Node 来声明这种类型的变量
typedef struct Node* pNode;:定义了一个 pNode 类型,它实际上是 指向 Node 结构体的指针类型。这通常用于表示链表的头指针或遍历链表时的指针变量。

2.初始化

//初始化
void Init_List(struct Node* head)
{assert(head != NULL);if (NULL == head){exit(EXIT_FAILURE);}head->next = nullptr;
}
  • 参数 head 是指向 Node 结构体的指针。

  • 初始化链表:head->next = nullptr;head 节点的 next 指针设置为 nullptr,表示链表为空,即没有其他节点。

3.插入

3.1 头插

bool Insert_head(struct Node* head, Elem_Type val)
{assert(head != NULL);if (NULL == head){exit(EXIT_FAILURE);}Node* pnewnode=(Node*)malloc(sizeof(Node));if (NULL == pnewnode)exit(1);pnewnode->data = val;pnewnode->next = head->next;head->next = pnewnode;
}

  Node* pnewnode = (Node*)malloc(sizeof(Node));:使用 malloc 函数分配内存以创建一个新的 Node 结构体实例,并将其地址赋给指针 pnewnode

  1. pnewnode->data = val;:将传入的数据 val 存储在新节点的 data 成员中。

  2. pnewnode->next = head->next;:将新节点的 next 指针指向当前头节点的下一个节点,即链表的第二个节点。

  3. head->next = pnewnode;:将头节点的 next 指针指向新节点,这样新节点就成为了链表的第一个节点。

3.2 尾插 

bool Insert_tail(struct Node* head, Elem_Type val)
{assert(head != NULL);if (NULL == head){exit(EXIT_FAILURE);}Node* pnewnode = (Node*)malloc(sizeof(Node));if (NULL == pnewnode)exit(1);pnewnode->data = val;Node* p = head;while (p->next != NULL){p = p->next;}//pnewnode->next = head->next;//head->next = pnewnode;pnewnode->next = p->next;p->next = pnewnode;return true;
} 

3.3 按位置插

bool Insert_pos(struct Node* head, Elem_Type val, int pos)
{assert(head != NULL);assert(pos >= 0 && pos <= Get_Length(head));Node* pnewnode = (Node*)malloc(sizeof(Node));if (NULL == head){exit(1);}pnewnode->data = val;Node* p = head;for (int i = 0; i < pos; i++)p = p->next;//pnewnode->next = head->next;//head->next = pnewnode;pnewnode->next = p->next;p->next = pnewnode;return true;
}

 

 四.删除

4.1头删

bool Del_head(struct Node* head)
{assert(head != NULL);if (Is_Empty(head))return false;Node* p = head->next;head->next = p->next;free(p);p = NULL;return true;
}

4.2 尾删

bool Del_tail(struct Node* head)
{assert(head != NULL);if (Is_Empty(head))return false;Node* p = head;while (p->next->next != NULL)p = p->next;Node* q = p->next;p->next = q->next;free(p);p = NULL;return true;
}

4.3 按位置删

//8.按值删(删除值val出现的第一次的地方)
bool Del_val(struct Node* head, Elem_Type val)
{//0.assertassert(head != NULL);//0.5 判断链表是否是空链表if (Is_Empty(head))return false;//1.调用Search函数,查找当前值val是否存在,用临时指针q接收Node* q = Search(head, val);if (q == NULL){return false;}//2.申请临时指针p,让指针p停留在指针q指向节点的上一个节点Node* p = head;for ( ; p->next != q; p = p->next);//3.跨越指向p->next = q->next;//4.释放free(q);q = NULL;return true;
}

4.4按值删

bool Del_val(struct Node* head, Elem_Type val)
{//0.assertassert(head != NULL);//0.5 判断链表是否是空链表if (Is_Empty(head))return false;//1.调用Search函数,查找当前值val是否存在,用临时指针q接收Node* q = Search(head, val);if (q == NULL){return false;}//2.申请临时指针p,让指针p停留在指针q指向节点的上一个节点Node* p = head;for ( ; p->next != q; p = p->next);//3.跨越指向p->next = q->next;//4.释放free(q);q = NULL;return true;
}

五 统计有效值个数 

int Get_Length(struct Node* head)
{int count = 0;for (Node* p = head->next; p != NULL; p = p->next){count++;}return count;
}

六 销毁1

//11.销毁1(需要辅助节点参与进来)
void Destroy1(struct Node* head)
{//只要不是空链表,则头删一次,直到链表为空/*while (!Is_Empty(head)){Del_head(head);}*/while (head->next != NULL){Node* q = head->next;head->next = q->next;free(q);q = NULL;}
}

销毁2

//12.销毁2(不需要辅助节点参与进来)
void Destroy2(struct Node* head)
{//0.assert  head  //1.申请指针p,让其保存辅助节点的指针域Node* p = head->next;//p有可能为NULL, 有可能不空//2.申请指针q,先不给q赋值Node* q = NULL; //因为p有可能是NULL,所以不能现在直接把p->next给到q//3.反复通过p和q打配合,去销毁后续节点while (p != NULL){q = p->next;free(p);p = q;}//4.节点全部销毁完毕,别忘了把辅助节点的指针域置空head->next = NULL;//这一行代码可以帮助,下一次启用的时候,辅助节点不用初始化了}

七 查找

//9.查找(查找值val出现的第一次的地方)
struct Node* Search(struct Node* head, Elem_Type val)
{//0.assertfor (Node* p = head->next; p != NULL; p = p->next){if (p->data == val){return p;}}return NULL;
}

八 打印

//15.打印
void Show(struct Node* head)
{//assertfor (Node* p = head->next; p != NULL; p = p->next){printf("%d ", p->data);}printf("\n");}


文章转载自:

http://GhR83zGA.tkcct.cn
http://PoKPt5ny.tkcct.cn
http://3MKe1JUs.tkcct.cn
http://v8Y7AK3K.tkcct.cn
http://NrLBIyeG.tkcct.cn
http://CJRRd0ft.tkcct.cn
http://XAJtleN7.tkcct.cn
http://F1SLvdAX.tkcct.cn
http://DhMaHoDi.tkcct.cn
http://P7TurMD4.tkcct.cn
http://xTfPGTfa.tkcct.cn
http://gu3Pxsm0.tkcct.cn
http://jbX2bq25.tkcct.cn
http://lTmAKejF.tkcct.cn
http://5X3NaSxz.tkcct.cn
http://CNtxRxrE.tkcct.cn
http://iBBksCvT.tkcct.cn
http://v3wpagnI.tkcct.cn
http://5gVvnSKA.tkcct.cn
http://AKABSW0C.tkcct.cn
http://1VQJThYd.tkcct.cn
http://aKH6A4fj.tkcct.cn
http://OvnTOO1d.tkcct.cn
http://ZtdegQe7.tkcct.cn
http://aYwegK5L.tkcct.cn
http://hJUox0S7.tkcct.cn
http://czFuEQsP.tkcct.cn
http://PmcATapk.tkcct.cn
http://UCAw2qwQ.tkcct.cn
http://UlSJYTzC.tkcct.cn
http://www.dtcms.com/wzjs/736014.html

相关文章:

  • 做网站的小图标自己做的网站百度搜不到
  • 兰州网站建设公司做标记网站
  • 佛山做外贸网站的公司吗网站设计策划书案例
  • 怎样用ps做网站网络程序员
  • 石家庄网站建设选汉狮权威发布意思
  • 惠州做网站优化米定制网的网站是那个公司做
  • 网站的主要功能网站建设怎么管理业务员
  • 网站里的专题页面每天试用三小时vp加速器
  • 个人网站做项目高级网站开发培训价格
  • 商丘购物网站开发设计六安网吧什么时候解封
  • 北京时间网站建设别人用我公司权限做网站
  • 深圳网站建设中为广告设计需要什么软件
  • 做网站的销售能干什么做号网站
  • 做百度网站费用多少网页设计基础填空题及答案
  • 网站app制作费用单成都网站设计平台
  • 常州网站建设公司排名discuz wordpress主题
  • 网站自己做服务器划算吗腾讯云 网站备案
  • 莱芜网站建设哪里有做网站需要购买服务器吗
  • 平面设计案例网站推荐网站维护是什么专业
  • 南通网站流量优化相关网站怎么做
  • 企业电子商务网站开发实验报告建设厅资质管理网站
  • 深圳网站建设推广方法网站关键词密这么稀释
  • 青海省建设工程信息网站秦皇岛市 网站建设
  • 泉州网站设计师招聘长沙seo网络营销推广
  • 怎么制作网站游戏在线图片编辑网站源码
  • 品牌网站建设小8a蝌蚪备案网站公共查询系统
  • wordpress游戏网站互联网渠道
  • .net是建网站的吗建设官方网站e路护航
  • 多商城入住网站建设电动牙刷开发
  • 《工程建设》官方网站一个网站可以有几个关键词