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

怎么注册晋江网站做的南京网站制作哪家专业

怎么注册晋江网站做的,南京网站制作哪家专业,上海市建设安装协会网站,wordpress备份恢复链表 介绍单向链表节点结构创建节点插入节点删除节点遍历链表尾部插入查找节点链表反转示例程序程序1程序2 介绍 链表是一种常见的数据结构#xff0c;用于存储一系列线性数据。与数组不同#xff0c;链表中的元素在内存中不必是连续存放的#xff0c;而是通过指针将每个元… 链表 介绍单向链表节点结构创建节点插入节点删除节点遍历链表尾部插入查找节点链表反转示例程序程序1程序2 介绍 链表是一种常见的数据结构用于存储一系列线性数据。与数组不同链表中的元素在内存中不必是连续存放的而是通过指针将每个元素节点链接在一起。链表有很多种类型例如单向链表、双向链表和循环链表。这里我们主要介绍单向链表。 单向链表 单向链表由一系列节点组成每个节点包含两个部分 数据部分存储节点的实际内容。指针部分存储指向下一个节点的指针。 节点结构 在 C 语言中我们可以通过定义一个结构体来表示链表的节点 struct Node {int data; // 数据部分struct Node* next; // 指针部分 };创建节点 可以通过动态内存分配函数 malloc 来创建新节点 #include stdio.h #include stdlib.h// 定义节点结构 struct Node {int data;struct Node* next; };// 创建新节点 struct Node* createNode(int data) {struct Node* newNode (struct Node*)malloc(sizeof(struct Node));if (!newNode) {printf(内存分配失败\n);exit(1);}newNode-data data;newNode-next NULL;return newNode; }插入节点 我们可以在链表的头部或尾部插入节点。以头部插入为例 // 头部插入 void insertAtHead(struct Node** head, int data) {struct Node* newNode createNode(data);newNode-next *head;*head newNode; }删除节点 删除链表中的节点也很常见。假设我们要删除值为 key 的节点 void deleteNode(struct Node** head, int key) {struct Node* temp *head;struct Node* prev NULL;// 如果头节点本身就是要删除的节点if (temp ! NULL temp-data key) {*head temp-next;free(temp); // 释放内存return;}// 搜索要删除的节点while (temp ! NULL temp-data ! key) {prev temp;temp temp-next;}// 如果没找到要删除的节点if (temp NULL) return;// 断开链接并释放内存prev-next temp-next;free(temp); }遍历链表 遍历链表可以通过循环来实现 void printList(struct Node* head) {struct Node* current head;while (current ! NULL) {printf(%d - , current-data);current current-next;}printf(NULL\n); }尾部插入 尾部插入与头部插入类似我们需要找到链表的最后一个节点然后将新节点添加到末尾。 // 尾部插入 void insertAtTail(struct Node** head, int data) {struct Node* newNode createNode(data);if (*head NULL) {*head newNode;return;}struct Node* temp *head;while (temp-next ! NULL) {temp temp-next;}temp-next newNode; }查找节点 查找链表中的某个节点返回其位置或者指针 struct Node* searchNode(struct Node* head, int key) {struct Node* current head;while (current ! NULL) {if (current-data key) {return current;}current current-next;}return NULL; // 未找到 }链表反转 反转链表是一个经典的问题可以通过迭代的方式实现 void reverseList(struct Node** head) {struct Node* prev NULL;struct Node* current *head;struct Node* next NULL;while (current ! NULL) {next current-next; // 暂存下一个节点current-next prev; // 反转当前节点的指针prev current; // 移动 prev 指针current next; // 移动 current 指针}*head prev; }示例程序 程序1 #include stdio.h #include stdlib.hstruct Node {int data;struct Node* next; };struct Node* createNode(int data) {struct Node* newNode (struct Node*)malloc(sizeof(struct Node));if (!newNode) {printf(内存分配失败\n);exit(1);}newNode-data data;newNode-next NULL;return newNode; }void insertAtHead(struct Node** head, int data) {struct Node* newNode createNode(data);newNode-next *head;*head newNode; }void deleteNode(struct Node** head, int key) {struct Node* temp *head;struct Node* prev NULL;if (temp ! NULL temp-data key) {*head temp-next;free(temp);return;}while (temp ! NULL temp-data ! key) {prev temp;temp temp-next;}if (temp NULL) return;prev-next temp-next;free(temp); }void printList(struct Node* head) {struct Node* current head;while (current ! NULL) {printf(%d - , current-data);current current-next;}printf(NULL\n); }int main() {struct Node* head NULL;insertAtHead(head, 1);insertAtHead(head, 2);insertAtHead(head, 3);printf(Created Linked List: );printList(head);deleteNode(head, 2);printf(Linked List after deletion of 2: );printList(head);return 0; }输出结果 程序2 #include stdio.h #include stdlib.h// 定义节点结构 struct Node {int data;struct Node* next; };// 创建新节点 struct Node* createNode(int data) {struct Node* newNode (struct Node*)malloc(sizeof(struct Node));if (!newNode) {printf(内存分配失败\n);exit(1);}newNode-data data;newNode-next NULL;return newNode; }// 头部插入 void insertAtHead(struct Node** head, int data) {struct Node* newNode createNode(data);newNode-next *head;*head newNode; }// 尾部插入 void insertAtTail(struct Node** head, int data) {struct Node* newNode createNode(data);if (*head NULL) {*head newNode;return;}struct Node* temp *head;while (temp-next ! NULL) {temp temp-next;}temp-next newNode; }// 删除节点 void deleteNode(struct Node** head, int key) {struct Node* temp *head;struct Node* prev NULL;if (temp ! NULL temp-data key) {*head temp-next;free(temp);return;}while (temp ! NULL temp-data ! key) {prev temp;temp temp-next;}if (temp NULL) return;prev-next temp-next;free(temp); }// 查找节点 struct Node* searchNode(struct Node* head, int key) {struct Node* current head;while (current ! NULL) {if (current-data key) {return current;}current current-next;}return NULL; }// 反转链表 void reverseList(struct Node** head) {struct Node* prev NULL;struct Node* current *head;struct Node* next NULL;while (current ! NULL) {next current-next;current-next prev;prev current;current next;}*head prev; }// 打印链表 void printList(struct Node* head) {struct Node* current head;while (current ! NULL) {printf(%d - , current-data);current current-next;}printf(NULL\n); }int main() {struct Node* head NULL;insertAtHead(head, 1);insertAtHead(head, 2);insertAtHead(head, 3);printf(初始链表: );printList(head);// 测试尾部插入insertAtTail(head, 4);printf(尾部插入 4 后的链表: );printList(head);// 测试删除节点deleteNode(head, 2);printf(删除节点 2 后的链表: );printList(head);// 测试查找节点struct Node* foundNode searchNode(head, 3);if (foundNode) {printf(找到节点 3\n);} else {printf(未找到节点 3\n);}// 测试反转链表reverseList(head);printf(反转后的链表: );printList(head);return 0; }输出结果
http://www.hkea.cn/news/14457667/

相关文章:

  • 网站建设术语陕西专业网站开发联系电话
  • 自己怎么做外贸英文网站做详情页生成代码的网站
  • 企业网站建设公司网络企业cms wordpress
  • 温州做网站制作东阳网站建设报价
  • 安卓手机做服务器网站做网站虚拟主机多少钱
  • 资产负债表在哪个网站可以做新乐网站建设
  • 南通seo网站建设费用网站内链代码
  • 海南省住房和城乡建设厅官方网站免费的素材网站推荐
  • 做外贸应该去什么网站云南网红景点
  • 网站开发用什么系统比较好建设一个境外网站
  • 建设外贸网站的细节谷歌seo建站
  • 建设网上商城网站的目的和意义软文的目的是什么
  • 网站建设简称什么公司注册地址查询系统
  • 优化网站公司外包网店seo排名优化
  • 玉林市建设局网站手机界面设计说明
  • 计算机速成班无锡 网站 seo 优化
  • 国外购物网站欣赏如何来做网站优化
  • 网站建设外包注意事项网站建设前
  • 昆山网站建设 熊掌号全球可以做外贸的社交网站有哪些
  • gta5买房子网站建设光遇网页制作素材
  • 网站如何在百度搜索到外包小程序价格
  • 网络公司怎么优化网站网站不交换友情链接可以吗
  • 如何在网站做旅游产品平面设计主要学什么内容
  • 洛阳高新区做网站公司阳江网络问政平台下载
  • 网站开发 兼职wordpress 修改后台地址
  • 内江如何做百度的网站代理办公司注册大概多少钱
  • 网站样板股票发行ipo和seo是什么意思
  • 网站seo优化效果做公司网站需注意什么
  • php开发网站项目心得苏州企业如何建网站
  • 给公司创建网站网站建设合同有法律效益吗