
数据结构和算法
暗里い着迷
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
交换两个数的方法
不定义第三个变量,交换两个数的方法总结1、使用加法和减法交换,仅适用于整数和浮点数,存在溢出风险a = a + b;b = a - b;a = a - b;2、使用异或运算符,仅适用于整数,不存在溢出风险a ^= b;b ^= a;a ^= b;原创 2024-06-27 11:26:33 · 432 阅读 · 0 评论 -
数据结构—栈
#include <stdio.h>#include <stdlib.h>typedef struct stack{ int *arr; int cap; int top;}stack_t;void stack_init(stack_t *stack ,int cap){ stack->arr = (int *)malloc(cap * sizeof(int)); stack->cap = cap; stack->top = 0;}v.原创 2021-09-24 08:54:21 · 137 阅读 · 0 评论 -
数据结构—队列
#include <stdio.h>#include <stdlib.h>typedef struct queue{ int *arr; int cap; int size; int front; int rear;}queue_t;void queue_init(queue_t *queue ,int cap){ queue->arr = (int *)malloc(cap * sizeof(queue->arr[0])); queue-&g.原创 2021-09-24 08:59:04 · 110 阅读 · 0 评论 -
数据结构—单向链表
#include <stdio.h>#include <stdlib.h>#include <string.h> // memsettypedef struct node { int data; struct node *next; }node_t;typedef struct list { struct node *head; struct node *tail; }list_t;/* 定义遍历函数打印 */v.原创 2021-09-24 09:07:48 · 161 阅读 · 0 评论 -
数据结构—双向链表
#include <stdio.h>#include <stdlib.h>typedef struct node{ int data; struct node *next; struct node *prev;}node_t;typedef struct list{ node_t *head; node_t *tail;}list_t;// 初始化链表void list_init(list_t * list){ list->head = (no.原创 2021-09-24 11:19:12 · 168 阅读 · 0 评论 -
数据结构—二叉树
#include <stdio.h>#include <stdlib.h>typedef struct node{ int data; struct node *left; struct node *right;}node_t;typedef struct tree{ node_t *root; int cnt;}tree_t;// 初始化二叉树void tree_init(tree_t *tree){ tree->root = NULL; .原创 2021-09-24 11:30:10 · 157 阅读 · 0 评论 -
二分查找算法
// 实现二分查找的功能函数int find(int arr[],int left,int right,int data){ // 保证数组中确实存在元素 if(left <= right) { // 计算中间元素的下标 int p = (left+right)/2; // 寻找中间元素进行比较 if(data == arr[p]) { return p; } // 如果目标元素小于中间元素,左边查找 else if(data < arr[p]).原创 2021-09-24 11:34:06 · 112 阅读 · 0 评论 -
常用排序算法
【代码】常用排序算法。原创 2021-09-17 21:44:50 · 108 阅读 · 0 评论 -
字符串相关算法
/* 字符串逆转 */void my_reverse(char *p){ int len = strlen(p); for (int i = 0;i < len/2; ++i) { int tmp = p[i]; p[i] = p[len-1-i]; p[len-1-i] = tmp; }}/* 小写转大写 */void atoA(char *p){ while (*p) { if (*p > 'a' && *p < 'z') { .原创 2021-09-22 18:42:02 · 153 阅读 · 0 评论