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 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

C语言中的数据类型强制转换

《C语言中的数据类型强制转换》:本文主要介绍C语言中的数据类型强制转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C语言数据类型强制转换自动转换强制转换类型总结C语言数据类型强制转换强制类型转换:是通过类型转换运算来实现的,主要的数据类型转换分为自动转换

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

C语言实现两个变量值交换的三种方式

《C语言实现两个变量值交换的三种方式》两个变量值的交换是编程中最常见的问题之一,以下将介绍三种变量的交换方式,其中第一种方式是最常用也是最实用的,后两种方式一般只在特殊限制下使用,需要的朋友可以参考下... 目录1.使用临时变量(推荐)2.相加和相减的方式(值较大时可能丢失数据)3.按位异或运算1.使用临时

使用C语言实现交换整数的奇数位和偶数位

《使用C语言实现交换整数的奇数位和偶数位》在C语言中,要交换一个整数的二进制位中的奇数位和偶数位,重点需要理解位操作,当我们谈论二进制位的奇数位和偶数位时,我们是指从右到左数的位置,本文给大家介绍了使... 目录一、问题描述二、解决思路三、函数实现四、宏实现五、总结一、问题描述使用C语言代码实现:将一个整