嵌入式八股文面试题(二)C语言算法

embedded/2025/2/14 3:50:07/

相关概念请查看文章:C语言概念。

1. 如何实现一个简单的内存池?

简单实现:

#include <stdio.h>
#include <stdlib.h>//内存块
typedef struct MemoryBlock {void *data; // 内存块起始地址struct MemoryBlock *next; // 下一个内存块的地址
} MemoryBlock;//内存池
typedef struct MemoryPool {MemoryBlock *freeList; // 空闲内存块链表MemoryBlock *usedList; // 占用内存块链表int freeCount; // 空闲内存块数量int usedCount; // 占用内存块数量int blockCount; // 内存块总数量
} MemoryPool;
//初始化内存池
MemoryPool *InitMemoryPool(int blockSize, int blockCount) {MemoryPool *pool = (MemoryPool *)malloc(sizeof(MemoryPool)); // 为内存池分配空间if (pool == NULL) {printf("Failed to allocate memory pool!\n");return NULL;}pool->freeList = NULL;pool->usedList = NULL;pool->freeCount = 0;pool->usedCount = 0;pool->blockCount = blockCount;for (int i = 0; i < blockCount; i++) {// 创建内存块节点,插入到空闲链表MemoryBlock *block = (MemoryBlock *)malloc(sizeof(MemoryBlock));block->data = malloc(blockSize);block->next = pool->freeList;pool->freeList = block;pool->freeCount++;}return pool;
}
//分配内存块
void *AllocateBlock(MemoryPool *pool) {if (pool->freeList == NULL || pool->freeCount == 0) {printf("No free blocks available!\n");return NULL;}MemoryBlock *node = pool->freeList;// 将该内存块从空闲链表删除pool->freeList = node->next;// 将该内存块插入到占用链表node->next = pool->usedList;pool->usedList = node;// 更新空闲和占用状态pool->usedCount++;pool->freeCount--;return node->data;
}
//释放内存块
void FreeBlock(MemoryPool *pool, void *data) {MemoryBlock *cur = pool->usedList;MemoryBlock *pre = NULL;// 寻找该内存块的节点while (cur != NULL && cur->data != data) {pre = cur;cur = cur->next;}if (cur == NULL) {printf("Error: Data not found!\n");return;}// 将该内存块从占用链表删除if (pre != NULL)pre->next = cur->next;elsepool->usedList = cur->next;// 将该内存块插入到空闲链表cur->next = pool->freeList;pool->freeList = cur;pool->freeCount++;pool->usedCount--;
}
//销毁内存块
void DestroyMemoryPool(MemoryPool *pool) {if (pool == NULL) return;MemoryBlock *cur = NULL;// 释放所有空闲内存块空间while (pool->freeList != NULL) {cur = pool->freeList;pool->freeList = pool->freeList->next;free(cur->data);free(cur);}// 释放所有占用内存块空间while (pool->usedList != NULL) {cur = pool->usedList;pool->usedList = pool->usedList->next;free(cur->data);free(cur);}// 释放内存池空间free(pool);
}
int main(void) {MemoryPool *pool;pool = InitMemoryPool(10, 5); // 初始化内存池int *str = (int *)AllocateBlock(pool);  //申请内存块1*str = 2;int *ptr = (int *)AllocateBlock(pool); //申请内存块2*ptr = 3;printf("free block : %d, used block : %d\n", pool->freeCount, pool->usedCount);FreeBlock(pool, ptr); //释放内存块2printf("free block : %d, used block : %d\n", pool->freeCount, pool->usedCount);DestroyMemoryPool(pool); return 0;
}

打印结果: 

2. 实现一个双向链表。

        双向链表是一种每个节点都有两个指针,一个指向下一个节点,一个指向前一个节点的数据结构。可以在任意位置进行快速插入和删除。

#include <stdio.h>
#include <stdlib.h>// 双向链表节点
typedef struct Node {int data;struct Node *prev; //连接前一个节点的指针struct Node *next; //连接下一个节点的指针
} Node;// 创建新节点
Node* createNode(int data) {Node *newNode = (Node *)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;newNode->prev = NULL;return newNode;
}// 插入节点到链表的尾部
void append(Node **head, int data) {Node *newNode = createNode(data);if (*head == NULL) {*head = newNode;} else {Node *temp = *head;while (temp->next != NULL) {temp = temp->next;}temp->next = newNode;newNode->prev = temp;}
}
//删除一个节点
void delete_node(Node **head, int data) {if (*head == NULL){  // 如果链表为空printf("链表为空,没有要删除的元素\n");return;}Node *temp = *head;// 如果删除的是头节点if (temp->data == data) {*head = temp->next;  // 更新头节点if (*head != NULL) {  // 如果不是空链表(*head)->prev = NULL;}free(temp);return;}// 找到要删除的节点while (temp != NULL && temp->data != data) {temp = temp->next;}// 如果没有找到该节点if (temp == NULL) {printf("未找到数据为 %d 的节点\n", data);return;}// 删除的是中间或尾部节点if (temp->next != NULL) {temp->next->prev = temp->prev;  // 更新下一个节点的prev指针}if (temp->prev != NULL) {temp->prev->next = temp->next;  // 更新前一个节点的next指针}free(temp);
}
// 打印双向链表
void printList(Node *head) {Node *temp = head;while (temp != NULL) {printf("%d <-> ", temp->data);temp = temp->next;}printf("NULL\n");
}int main() {Node *head = NULL;append(&head, 10);append(&head, 20);append(&head, 30);printList(head);delete_node(&head, 30);printList(head);return 0;
}

打印结果:

3. 实现一个线程池。

   


http://www.ppmy.cn/embedded/162045.html

相关文章

DeepSeek的出现会对百度有多大影响?

当DeepSeek与ChatGPT等大模型接管搜索入口&#xff0c;我们正见证百年一遇的信息革命。 01 传统搜索已死&#xff1f;AI助手正在重写游戏规则&#xff01; 当DeepSeek与ChatGPT等大模型接管搜索入口&#xff0c;我们正见证百年一遇的信息革命。 就像汽车淘汰马车、触屏终结按键…

基础连接已经关闭: 服务器关闭了本应保持活动状态的连接

您在进行 HTTP 请求时遇到“基础连接已经关闭: 服务器关闭了本应保持活动状态的连接”的错误&#xff0c;这通常与连接的保持活动&#xff08;Keep-Alive&#xff09;设置有关。以下是可能的原因和解决方法&#xff1a; 可能的原因&#xff1a; Keep-Alive 设置&#xff1a; 默…

数据库,数据表的增删改查操作

一.数据库的基本操作 &#xff08;1&#xff09;创建数据库 创建数据库就是在数据库系统中划分一块存储数据的空间&#xff0c;方便数据的分配、放置和管理。在MySQL中使用CREATE DATABASE命令创建数据库&#xff0c;语法格式如下: CREATE DATABASE数据库名称; 注&#xff1a…

DeepSeek 中的 GRPO 算法全面解析

摘要: 为特定任务调整大型语言模型 (LLM) 通常涉及通过使用人类反馈 (RLHF) 的强化学习对偏好数据进行微调。 虽然这些数据通常来自不同的标注者群体(例如,不同的文化背景、种族、公司团队等),但传统的 RLHF 方法采用“一刀切”的方法,即,它们不加区分地假设并优化一个单…

AlmaLinux使用Ansible自动部署k8s集群

以下是使用Ansible在AlmaLinux上自动化部署Kubernetes&#xff08;K8S&#xff09;集群的详细步骤&#xff1a; 1. 环境准备 1.1 节点规划 至少3台AlmaLinux 9服务器&#xff08;1个Master&#xff0c;2个Worker&#xff09;确保所有节点网络互通&#xff0c;SSH免密登录已配…

MongoDB 的基本概念

一、数据库&#xff08;Database&#xff09; 数据库是 MongoDB 中最高层次的概念&#xff0c;是一个存储数据的逻辑容器&#xff0c;它可以包含多个集合。一个 MongoDB 实例可以管理多个数据库&#xff0c;每个数据库都有自己独立的权限和存储空间。可以使用use命令在 Mongo …

23种设计模式的定义和应用场景-C#代码-汇总

23种设计模式的定义和应用场景&#xff1a; 1. 创建型模式&#xff08;共5种&#xff09; 单例模式&#xff08;Singleton&#xff09;、工厂方法模式&#xff08;Factory Method&#xff09;、抽象工厂模式&#xff08;Abstract Factory&#xff09;、建造者模式&#xff08;…

蓝桥杯试题:冒泡排序 选择排序

一、问题描述 在一个神秘的岛屿上&#xff0c;有一支探险队发现了一批宝藏&#xff0c;这批宝藏是以整数数组的形式存在的。每个宝藏上都标有一个数字&#xff0c;代表了其珍贵程度。然而&#xff0c;由于某种神奇的力量&#xff0c;这批宝藏的顺序被打乱了&#xff0c;探险队…