
基础数据结构及算法
何小柒~
生命不是代码,没有最优解;存在本身,就是最浪漫的bug。
展开
-
算法与数据结构:二叉树的遍历
文章目录1. 二叉树的前序遍历(144)1.1 题目1.2 算法思想2. 二叉树的中序遍历(94)2.1 题目2.2 算法思想3. 二叉树的后序遍历3.1 题目3.2 算法思想1. 二叉树的前序遍历(144)1.1 题目给你二叉树的根节点 root ,返回它节点值的前序 遍历。示例 1:输入:root = [1,null,2,3]输出:[1,2,3]示例 2:输入:root = []输出:[]示例 3:输入:root = [1]输出:[1]示例 4:输入:root原创 2022-11-09 10:34:54 · 336 阅读 · 0 评论 -
基础数据结构:单链表习题
习题:1.a.(4)(1)b.(7)(11)(8)(4)(1)c.(5)(12)d.(9)(4)(1)2.a.(11)(3)(14)b.(10)(12)(8)(11)(3)(4)c.(10)(12)(7)(3)(4)d.(12)(11)(3)(14)e.(12)(9)(11)(3)(4)5#include<stdio.h>#include<math.h>//5//从前向后 依次头插void Reverse(PNode plist){ //原创 2021-09-10 16:59:00 · 250 阅读 · 0 评论 -
基础数据结构:习题
1.线性表便于在尾部进行插入和删除操作2.随机访问——在尾部插入和删除3.4.#include<stdio.h>//9int i = 0;for (int i = 0; i < len; i++){ if (arr[i] > x) { break; }}for (int j = len - 1; j >= i; j--){ arr[j + 1] = arr[j];}arr[i] = x;//10while (i < len原创 2021-09-08 21:50:13 · 107 阅读 · 0 评论 -
基础数据结构18:快速排序算法
1.快速排序方法(面试常考)//***(面试笔试常考)static int Partition(int* arr, int left, int right){ assert(arr != NULL); //保证基准值在tmp里面 int tmp = arr[left]; while (left < right)//保证最少有两个值 { while (left<right && arr[right]>tmp) right--; if (left =原创 2021-09-03 11:26:29 · 123 阅读 · 0 评论 -
基础数据结构17:基数排序和快速排序
1.基数排序(桶排序):低位优先,所有数据从低位开始,一次放入到10个桶内,再次从桶里取出,直到完全有序。快速排序:每次找基准值的位置,以基准线为分界线,左边的值全部比基准值小,而右边的值肯定比基准值大。规则:从右向左找比基准值小的数据放到左边,找到后放到左边,从左向右找比基准值大的数据,找到后放到右边,重复上面的操作,直到left=right,循环退出,再将基准值放到arr[left]或者arr[right]。#include <stdio.h>#include <asser原创 2021-08-31 21:35:57 · 791 阅读 · 0 评论 -
基础数据结构16:排序
1.原创 2021-08-29 10:09:03 · 220 阅读 · 0 评论 -
基础数据结构15:冒泡排序和归并排序
1.2.原创 2021-08-26 18:12:06 · 264 阅读 · 0 评论 -
基础数据结构14:直接插入排序和希尔排序
1.2.直接插入排序#include <stdio.h>#include <assert.h>#include <stdlib.h>//八大排序 排序分为升序和降序 我们默认使用升序//算法的描述 算法的实现 算法的评价(时间复杂度,空间复杂度,稳定性)//什么是稳定性:如果排序前A在A`的前面,排序之后A孩子A`的前面,则排序算法稳定//如何判断其稳定性:看算法中是否存在跳跃交换//第一个排序算法:直接插入排序:每次从待排序队列中取一个值原创 2021-08-23 12:11:25 · 133 阅读 · 0 评论 -
基础数据结构13:字符串匹配(BF和KMP算法)
1.2.BF_KMP算法#include <stdio.h>#include <assert.h>#include <stdlib.h>#include <string.h>#pragma warning(disable:4018)/*一定得会的知识: 1.主串i为什么打死不回退 2.next数组只跟子串有关 3.给一个字符串,一定得会手写它的next数组的值 4.BF算法一定得会写*///朴素算法 BF算法 :1.同时原创 2021-08-22 19:38:28 · 298 阅读 · 0 评论 -
基础数据结构12:两个队列实现一个栈,一致性哈希
1.一致性哈希2.解决冲突问题#pragma oncetypedef int ELEM_TYPE;#define MAXQSIZE 100typedef struct Queue{ ELEM_TYPE* base;//数据域 指向malloc申请来的动态内存 int front;//头指针,当队列不空的时间,保存是队头,指向的第一个元素的下标 int rear;//尾指针,当队列不空的时间,保存的是队尾,指向的下一个元素入队的下标 //int length;//有效元素个数}Q原创 2021-08-20 21:20:47 · 76 阅读 · 0 评论 -
基础数据结构11:哈希
1.哈希概念:散列函数2.链式哈希#include <stdio.h>#include <assert.h>#include <stdlib.h>#define HASHSIZE 12typedef struct Node{ int data; struct Node* next;}Node, * PNode;typedef struct Head{ //struct Node *arr[HASHSIZE];//指针数组(存放指针的数组)原创 2021-08-20 18:44:57 · 206 阅读 · 0 评论 -
基础数据结构10:链式队列,用两个栈实现一个队列
1.对头节点结构体设计2.链式队列#pragma once typedef int ELEM_TYPE;typedef struct Node{ ELEM_TYPE data;//数据域 struct Node* next;//指针域}Node, * PNode;//我们对头结点重新设计其结构体typedef struct Head{ struct Node* front;//一直指向第一个节点 struct Node* rear;//一直指向最后一个节点 //int原创 2021-08-14 11:37:06 · 151 阅读 · 0 评论 -
基础数据结构9:双向循环链表
1.双向循环链表#pragma oncetypedef int ELEM_TYPE;typedef struct DCList{ ELEM_TYPE data;//数据域 struct DCList* next;//后继指针 struct DCList* prior;//前驱指针}DCList, *PDClist;//双向循环链表可执行的操作//初始化void Init_dclist(PDClist pl);//头插bool Insert_head(PDClist p原创 2021-08-14 09:51:37 · 68 阅读 · 0 评论 -
基础数据结构8:栈和队列
1.栈原创 2021-08-07 16:59:35 · 85 阅读 · 0 评论 -
基础数据结构7:上机题
//1 2#include<stdio.h>#include<assert.h>#include<stdlib.h>typedef struct Day{ int year; int month; int days;}Day;int Get_days(Day* Pday)//传递结构体指针或者结构体普通变量都可以{ assert(Pday != NULL); int dayofMonth[12] = { 31,28,31,30,31,30,.原创 2021-08-05 09:54:07 · 88 阅读 · 0 评论 -
基础数据结构6:单链表的逆置等等一些问题的处理
//怕段单链表是否存在环,如果存在,则返回打环的第一个节点#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<string.h>#include"list(7_17).h"PNode IsLoop(PNode plist){ assert(plist != NULL); if (plist == NULL) return NULL; Node* fast = pli原创 2021-07-31 11:09:17 · 166 阅读 · 0 评论 -
基础数据结构5:循环链表
1.原创 2021-07-25 15:15:52 · 89 阅读 · 0 评论 -
基础数据结构4:双向链表及循环链表
union B//不占空间(上面有名字,下边没有)结构体定义 { int data;//数据域 int length; };2.双向链表dlist.h#pragma once#include<stdio.h>#include<assert.h>#include<malloc.h>#include"Dlist(7_17).h"typedef int ELEMTYPE;typedef struct DNode{ int data;...原创 2021-07-24 18:28:11 · 171 阅读 · 0 评论 -
基础数据结构3:单链表
1.单链表list.h#pragma oncetypedef int ELEMTYPE;typedef struct Node{ ELEMTYPE data;//数据域:保存数据本身 struct Node* next;//指针域:指向下一个数据节点的地址}Node,*PNode;//增删改查//初始化void Init_list(Node*plist);//Node*==PNode//头插bool Insert_head(PNode plist,int val);原创 2021-07-23 23:59:40 · 106 阅读 · 0 评论 -
基础数据结构2:实现不定长顺序表
1.实现不定长顺序表DSQlist.h#pragma once#define INIT_SIZE 10typedef struct DSQlist{ int* data;//数据域:迎来承接malloc申请的空间 int length;//有效长度个数 int listsize;//总的空间大小(总的格子数字)}DSQlist, * PDSQlist;//关于顺序表的增删改查void Init_list(PDSQlist ps);//用来初始化顺序表bool Insert(原创 2021-07-23 16:01:07 · 129 阅读 · 0 评论 -
基础数据结构1:实现定长顺序表
1.时间复杂度简单计算#include<stdio.h>//时间复杂度for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++) { arr[i][j]=1; }}//O(n)=n+n^2+n^2=2n^2+nfor (int i = 0; i < n; i++){ for (int j = i; j < n; j++) { arr[i][j] = 1; }}//O(n)=原创 2021-07-22 17:25:10 · 113 阅读 · 0 评论