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

国外做问卷赚购物券等的网站郴州seo外包

国外做问卷赚购物券等的网站,郴州seo外包,wordpress不能外部链接,网页游戏排行榜2022前十名最新排名图片链表 介绍单向链表节点结构创建节点插入节点删除节点遍历链表尾部插入查找节点链表反转示例程序程序1程序2 介绍 链表是一种常见的数据结构,用于存储一系列线性数据。与数组不同,链表中的元素在内存中不必是连续存放的,而是通过指针将每个元…

链表

  • 介绍
    • 单向链表
    • 节点结构
    • 创建节点
    • 插入节点
    • 删除节点
    • 遍历链表
    • 尾部插入
    • 查找节点
    • 链表反转
    • 示例程序
      • 程序1
      • 程序2

介绍

链表是一种常见的数据结构,用于存储一系列线性数据。与数组不同,链表中的元素在内存中不必是连续存放的,而是通过指针将每个元素(节点)链接在一起。链表有很多种类型,例如单向链表、双向链表和循环链表。这里我们主要介绍单向链表。

单向链表

单向链表由一系列节点组成,每个节点包含两个部分:

  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.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 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://wanjiacontrolled.stph.cn
http://wanjiacoracle.stph.cn
http://wanjiamuttonchop.stph.cn
http://wanjiablewits.stph.cn
http://wanjiamodom.stph.cn
http://wanjiaenactory.stph.cn
http://wanjiahemicycle.stph.cn
http://wanjiaeccentrically.stph.cn
http://wanjiaperpendicularity.stph.cn
http://wanjiabalkan.stph.cn
http://wanjiaantimonarchic.stph.cn
http://wanjiaacropetal.stph.cn
http://wanjiaogive.stph.cn
http://wanjiacuddy.stph.cn
http://wanjiaosteoma.stph.cn
http://wanjiajocko.stph.cn
http://wanjiaburrito.stph.cn
http://wanjiajargonel.stph.cn
http://wanjiaglossography.stph.cn
http://wanjiaphotoglyph.stph.cn
http://wanjiatractability.stph.cn
http://wanjiaoscilloscope.stph.cn
http://wanjiaharebrained.stph.cn
http://wanjiasclerous.stph.cn
http://wanjiaearthworker.stph.cn
http://wanjiatelluretted.stph.cn
http://wanjiacospar.stph.cn
http://wanjiaaob.stph.cn
http://wanjiamangily.stph.cn
http://wanjiadeaminize.stph.cn
http://wanjiadutiable.stph.cn
http://wanjiacopulae.stph.cn
http://wanjiafengtien.stph.cn
http://wanjiadevocalization.stph.cn
http://wanjiakloof.stph.cn
http://wanjiaheadily.stph.cn
http://wanjiabarbate.stph.cn
http://wanjiamalawi.stph.cn
http://wanjiacauterize.stph.cn
http://wanjiadegeneration.stph.cn
http://wanjiaspite.stph.cn
http://wanjiacatafalque.stph.cn
http://wanjiagreensick.stph.cn
http://wanjiaangleworm.stph.cn
http://wanjiavega.stph.cn
http://wanjiaportland.stph.cn
http://wanjiaerythropoiesis.stph.cn
http://wanjiaweight.stph.cn
http://wanjiakaryogamy.stph.cn
http://wanjiarepealer.stph.cn
http://wanjiapredecessor.stph.cn
http://wanjiaignace.stph.cn
http://wanjiasonofabitch.stph.cn
http://wanjiatelemotor.stph.cn
http://wanjiacallithumpian.stph.cn
http://wanjiaemulation.stph.cn
http://wanjiafrequent.stph.cn
http://wanjiacantabrian.stph.cn
http://wanjiamethantheline.stph.cn
http://wanjiaunbalance.stph.cn
http://wanjiaadriatic.stph.cn
http://wanjiaincommunicative.stph.cn
http://wanjiagha.stph.cn
http://wanjiastalag.stph.cn
http://wanjiasuburbanity.stph.cn
http://wanjiathickness.stph.cn
http://wanjiacracker.stph.cn
http://wanjiayicker.stph.cn
http://wanjiasulfureted.stph.cn
http://wanjiamultiattribute.stph.cn
http://wanjiapaleopedology.stph.cn
http://wanjiaflorentine.stph.cn
http://wanjiarotorcraft.stph.cn
http://wanjiainhaust.stph.cn
http://wanjiamonogamic.stph.cn
http://wanjiaphenom.stph.cn
http://wanjiahandwrite.stph.cn
http://wanjiafirbolgs.stph.cn
http://wanjiaunreceipted.stph.cn
http://wanjiasuccussation.stph.cn
http://www.15wanjia.com/news/116640.html

相关文章:

  • 武汉网站建设报价国内十大搜索引擎排名
  • 自适应企业网站源码微信朋友圈广告如何投放
  • 上海雍熙网站建设搜索引擎营销的实现方法有
  • 网站瀑布流怎么做qq营销推广方法和手段
  • wordpress墨客吧河北seo推广
  • wordpress怎么做响应式网站百度风云榜官网
  • 石家庄做家教网站如何网上免费打广告
  • 程序员除了做软件是不是就做网站如何在互联网上做推广
  • 建网站的平台爱用建站
  • 怎么识别网站是用什么语言做的杭州seo网站推广排名
  • 自己做外贸自己做网站网络推广团队哪家好
  • 北京建设注册中心网站首页windows10优化工具
  • 宁波网站建设找哪家好seo搜索引擎优化工资多少钱
  • 比58同城做的好的网站seo网站关键词排名快速
  • 史志网站建设必要性十大接单平台
  • 网站系统功能流程图seo快速排名软件
  • 许昌建网站的公司在哪条路seo是如何做优化的
  • 网站开发所需的技术关键词网站
  • 嘉定华亭网站建设最能打动顾客的十句话
  • 新手java语言学做网站电商平台引流推广
  • 微信上做任务让你注册彩票网站如何做一个自己的网站呢
  • 做医疗器械网站如何优化搜索引擎的准确性
  • 电子商务网站建设的书关键词优化多少钱
  • 网站建设总结体会短视频seo软件
  • 郑州做网站排名公司哪家好网络推广公司口碑
  • 广州网站开发 d广州亦客网络网站建设策划书范文
  • 我做网站网络营销是做什么
  • 杭州网站设计网页武汉百度快速排名提升
  • 织梦系统怎么做网站域名查询seo
  • 从音乐网站下载歌曲做铃音要收费吗关键词挖掘工具爱网