C语言基础(三十三)

2024-09-03 03:36
文章标签 语言 基础 三十三

本文主要是介绍C语言基础(三十三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、链表排序之归并排序与线性搜索

测试代码:

#include "date.h" 
#include <stdio.h>
#include <stdlib.h>// 链表节点结构体
typedef struct Node {int data;struct Node *next;
} Node;// 插入节点到链表末尾
Node* insertNode(Node *head, int data) {Node *newNode = (Node*)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;if (head == NULL) {head = newNode;} else {Node *current = head;while (current->next != NULL) {current = current->next;}current->next = newNode;}return head;
}// 归并排序
Node* merge(Node *head) {if (head == NULL || head->next == NULL) {return head;}// 分割链表Node *slow = head, *fast = head->next;while (fast != NULL && fast->next != NULL) {slow = slow->next;fast = fast->next->next;}Node *head2 = slow->next;slow->next = NULL;// 合并排序head = merge(head);head2 = merge(head2);Node dummy;Node *ptr = &dummy;while (head != NULL && head2 != NULL) {if (head->data < head2->data) {ptr->next = head;head = head->next;} else {ptr->next = head2;head2 = head2->next;}ptr = ptr->next;}ptr->next = (head != NULL) ? head : head2;return dummy.next;
}// 遍历链表
void printList(Node *head) {Node *current = head;while (current != NULL) {printf("Data: %d, Address: %p\n", current->data, (void*)current);current = current->next;}
}// 线性搜索
int linearSearch(Node *head, int target, Node **foundNodes) {int count = 0;Node *current = head;while (current != NULL) {if (current->data == target) {foundNodes[count] = current;count++;}current = current->next;}return count;
}int main() {int times = getTime();int n;printf("Enter the number of random numbers: ");scanf("%d", &n);Node *head = NULL;for (int i = 0; i < n; i++) {int randomNumber = rand() % 100; // 生成0-99范围内的随机数head = insertNode(head, randomNumber);}printf("Before sorting:\n");printList(head);head = merge(head);printf("After sorting:\n");printList(head);int target;printf("Enter the element to search for: ");scanf("%d", &target);Node *foundNodes[n];int count = linearSearch(head, target, foundNodes);if (count > 0) {printf("Found %d elements:\n", count);for (int i = 0; i < count; i++) {printf("Data: %d, Address: %p\n", foundNodes[i]->data, (void*)foundNodes[i]);}} else {printf("Element not found.\n");}return 0;
}

运行结果如下:

 

2、 链表排序之插入排序与二分搜索:

测试代码:

#include "date.h" 
#include <stdio.h>
#include <stdlib.h>typedef struct Node {int data;struct Node* next;
} Node;// 插入排序
void insertSort(Node** head) {if (*head == NULL || (*head)->next == NULL) {return;}Node* sorted = NULL;Node* current = *head;while (current != NULL) {Node* next = current->next;if (sorted == NULL || current->data < sorted->data) {current->next = sorted;sorted = current;} else {Node* temp = sorted;while (temp->next != NULL && temp->next->data < current->data) {temp = temp->next;}current->next = temp->next;temp->next = current;}current = next;}*head = sorted;
}// 输出链表
void printList(Node* head) {Node* current = head;while (current != NULL) {printf("Data: %d, Address: %p\n", current->data, (void*)current);current = current->next;}
}// 二分搜索
Node* binarySearch(Node* head, int target) {Node* left = head;Node* right = NULL;// 获取链表结尾for (Node* curr = head; curr != NULL; curr = curr->next) {right = curr;}while (left != right) {Node* mid = left;int len = 0;while (mid != right) {len++;mid = mid->next;}len /= 2;mid = left;for (int i = 0; i < len; i++) {mid = mid->next;}if (mid->data == target) {return mid;} else if (mid->data < target) {left = mid->next;} else {right = mid;}}return NULL;
}int main() {int times = getTime();int n;printf("Enter the number of elements: ");scanf("%d", &n);Node* head = NULL;// 生成随机数并添加到链表for (int i = 0; i < n; i++) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->data = rand();newNode->next = head;head = newNode;}printf("Unsorted List:\n");printList(head);// 对链表进行插入排序insertSort(&head);printf("\nSorted List:\n");printList(head);// 二分搜索指定的元素int target;printf("\nEnter the element to search: ");scanf("%d", &target);Node* result = binarySearch(head, target);if (result != NULL) {printf("Element found - Data: %d, Address: %p\n", result->data, (void*)result);} else {printf("Element not found\n");}return 0;
}

运行结果如下;

 

这篇关于C语言基础(三十三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1131913

相关文章

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

Spring的基础事务注解@Transactional作用解读

《Spring的基础事务注解@Transactional作用解读》文章介绍了Spring框架中的事务管理,核心注解@Transactional用于声明事务,支持传播机制、隔离级别等配置,结合@Tran... 目录一、事务管理基础1.1 Spring事务的核心注解1.2 注解属性详解1.3 实现原理二、事务事

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

Java中最全最基础的IO流概述和简介案例分析

《Java中最全最基础的IO流概述和简介案例分析》JavaIO流用于程序与外部设备的数据交互,分为字节流(InputStream/OutputStream)和字符流(Reader/Writer),处理... 目录IO流简介IO是什么应用场景IO流的分类流的超类类型字节文件流应用简介核心API文件输出流应用文

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam