- 博客(29)
- 收藏
- 关注
原创 红黑树的创建与介绍!!!(C++)
红黑树: 1.空树(红黑树可以是空树) 2.非空:二叉搜索树 + 给节点增加颜色(红色和黑色) 红黑树通过其性质的限制可以保证最长节点数不超过最短节点数的2倍 红黑树的性质: 1.红黑树中的每个节点都有颜色,不是红色就是黑色 2.根节点的颜色必须是黑色 3.红色节点的孩子节点必须是黑色节点(不能有两个连续的红色节点) 4.每条路径的黑色节点数必须相等 5.所有“叶子节点”的颜色必须是黑色(可忽略) 注: 本红黑树是模拟实现STL底层的红黑树 在创建红黑树是我们先来看三种特殊的情况 上面是插入到左子树
2021-04-04 12:21:23
1122
2
原创 二叉平衡树的建立于实现!!!(AVL树)
红黑树其实是二叉搜索树的进阶,就是在二叉搜索树的基础上添加了平衡因子的概念,平衡因子就是左右子树的高度差,红黑树中所有结点的平衡因子的绝对值都不超过1 如果插入右子树的内侧和左子树的原理一样,但是是先右旋后左旋 #pragma once #include <iostream> using namespace std; template<class T> struct AVLNode { AVLNode(const T& data = T()) :val_(
2021-03-31 21:09:23
993
2
原创 二叉树非递归后序遍历(C++)
void _End(Node* Proot) { Node* cur = Proot; Node* prev = nullptr; stack<Node*> s; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->left_; } Node* top = s.top(); //右树不为空 if (nullptr != top->
2021-03-29 19:43:33
156
1
原创 二叉树非递归中序遍历(C++)
void _InOrder(Node* Proot) { /*if (nullptr == Proot) return;*/ Node* cur = Proot; stack<Node*> s; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->left_; } Node* top = s.top(); cout <<
2021-03-29 19:42:49
260
1
原创 二叉树非递归先序遍历(C++)
void _Prev(Node* Proot) { Node* cur = Proot; stack<Node*> s; while (cur || !s.empty()) { while (cur) { cout << cur->val_ << " "; s.push(cur); cur = cur->left_; } Node* top = s.top(); if (nullptr
2021-03-29 19:42:05
128
原创 二叉搜索树的创建与删除!!!!!!(C++)
#pragma once #include <iostream> #include <stack> template<class T> struct BSTNode { BSTNode(const T& val = T()) :val_(val), left_(nullptr), right_(nullptr) { } T val_; BSTNode<T>* left_; BSTNode<T>* right_; }
2021-03-29 19:39:47
242
2
原创 数组中相加和为0的三元组
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int>> ret; //如果数组元素小于3个,则直接返回 if(num.size() < 3) return ret; //先给整个数组排序 so
2020-11-28 22:20:41
329
原创 二叉树中和为某一值的路径
class Solution { public: //先定义两个全局变量 //ret用来存放符合条件的路径 vector<vector<int>> ret; //v用来存放每一条路径 vector<int> v; void path(TreeNode* root, int num) { if(root == nullptr) return; v.pus
2020-11-27 18:45:23
102
原创 子数组的最大累加和问题
int maxsumofSubarray(vector<int>& arr) { // max用来保存当前子数组的最大值 int sum = 0; int max = 0; //循环arr数组,e为每个元素的值 for(auto e: arr) { //sum用来保存每段子数组的总和 //如果小于0则没有必要再加上前面的值,直接从下一个重新开始计
2020-11-26 13:29:21
147
1
原创 删除链表的倒数第n个结点
ListNode* removeNthFromEnd(ListNode* head, int n) { //判空 if(head == nullptr) return head; ListNode* node = nullptr; //定义快慢指针 ListNode* slow = head; ListNode* fast = head; //先让fast先向后移动n步
2020-11-25 23:11:28
127
1
原创 二叉树的简单操作(C语言实现)
//bintree.h #ifndef _BINTREE_H_ #define _BINTREE_H_ #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<assert.h> #pragma warning(disable:4996) typedef char ElemType; typedef struct BintreeNode { ElemType val; st
2020-10-15 21:08:23
206
5
原创 单链表的增删查改(C语言实现)
//common.h #ifndef _COMMON_H_ #define _COMMON_H_ #include <stdio.h> #include <windows.h> #include <stdbool.h> #include <assert.h> #include <vld.h> #pragma warning(disable:4996) #endif _COMMON_H_ //slist.h #ifndef _SLIST
2020-07-28 19:47:02
289
原创 双链表的增删查改(C语言实现)
//"common.h" #ifndef _COMMON_H_ #define _COMMON_H_ #include <stdio.h> #include <windows.h> #include <stdbool.h> #include <assert.h> #include <vld.h> #pragma warning(disable:4996) #endif _COMMON_H_ //"dclist.h" #ifndef _D
2020-07-25 20:00:40
204
原创 顺序表的增删查改(C语言实现)
"common.h" #ifndef _COMMON_H_ #define _COMMON_H_ #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <assert.h> #include <stdbool.h> #include <vld.h>//检查内存泄漏 #pragma warning(disable:4996) #define Elem
2020-07-21 18:42:52
344
原创 通讯录(C语言实现)
//多文件实现 //test.h #ifndef _TEST_H_ #define _TEST_H_ #include <stdio.h> #include <windows.h> #include <assert.h> #include <string.h> #pragma warning(disable:4996) #define MAX 100 typedef struct People//人的信息,这里只写了两个,如有需要,可自行添加 {
2020-06-30 18:23:34
367
1
原创 模拟实现strlen(C语言实现)
#include <stdio.h> #include <windows.h> #include <assert.h> int my_strlen(const char *arr) { int count = 0;//计数 while (*arr++){ count++; } return count; } int main() { char arr[] = "hello world"; int num = my_strlen(arr); print
2020-06-10 17:42:28
175
原创 模拟实现strcpy(C语言实现)
#include <stdio.h> #include <windows.h> #include <assert.h> char* my_strcpy(char *str, const char *arr) { char *ret = str; assert(str && arr);//断言 while (*str++ = *arr++){ ; } return ret; } int main() { char arr[] = "hel
2020-06-10 17:27:32
238
原创 模拟实现strcat(C语言实现)
#include <stdio.h> #include <windows.h> #include <assert.h> char* my_strcat(char arr[], const char *str) { char *arr1 = arr; assert(arr && str);//断言 while (*arr1){ arr1++;//先使被拼接的字符串指向‘\0’ } while (*arr1++ = *str++){//把要拼接
2020-06-10 10:35:33
253
原创 模拟实现库函数strcpy(C语言实现)
#include <stdio.h> #include <windows.h> void My_strcpy(char *p, const char *str) { char *pt = p;//使p的指向保持不变 if (p == NULL && str == NULL){//防止指针为空 return; } while (*pt++ = *str++)//一一拷贝 { ; } } int main() { char *str = "hel
2020-05-16 21:42:14
248
原创 喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水(C语言实现)。
#include <stdio.h> #include <windows.h> int main() { int num = 0;//汽水的总数 int empty = 0;//空瓶的数量 int count = 0;//换取的汽水数 int money = 20;//钱的总额 num = 20 / 1;//汽水1元1瓶 empty = num; while(empty > 1){ count = empty / 2;//两个空瓶换一瓶饮料 num +=
2020-05-16 20:15:42
423
原创 求出0~100000之间的所有“水仙花数”并输出。“水仙花数”是指一个n位数,其各位数字的n次方之和确好等于该数本身,如:153=1^3+5^3+3^3,则153是一个“水仙花数”。(C语言实现)
#include <stdio.h> #include <windows.h> #pragma warning(disable:4996) int Num(int i) { int count = 0;//计数 while (i > 0){ count++; i /= 10; } return count; } int Is_shuixianhua(int i, int num) { int sum = 0; int j = 0; int m = i
2020-05-11 11:42:52
1055
原创 求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222(C语言实现)
#include <stdio.h> #include <windows.h> #pragma warning(disable:4996) void Sn(int a, int n) { int i = 0;; int num = a; int sum = 0;//算式和 for (i = 0; i < n; i++){ sum += num; if (n - 1 != i){//除了最后一个数字,其他数字后面都带加号 printf("%d + ",
2020-05-11 00:45:26
277
原创 写一个函数,可以逆序字符串的内容!(C语言实现)
我们可以定义两个指针,一个指向字符串的首位,一个指向字符串的末尾,互换内容,然后前面的指针向后加一,后面的指针向前减一,知道前面的大于或等于后面的停止。 例如:加粗的是指针指向要互换的位置 a b c d e f f b c d e a f e c d b a f e d c b a #include <stdio.h> #include <string.h> #include <windows.h> void reverse(char *arr) { char
2020-05-11 00:28:12
2989
原创 马走日问题,在5*5的棋盘中,马只能走日字。马从位置(0,0)处出发,把棋盘的每一格都走一次,且只走一次。找出所有路径。(C语言实现)
#include <stdio.h> #include <windows.h> #include <math.h> #pragma warning(disable:4996) #define N (5 * 5)//棋盘大小5 * 5 int row[N] = { 0 };//存放行标 int col[N] = { 0 };//存放列标,意思是从(1,1)开...
2020-05-07 10:30:47
2374
原创 递归解决八皇后问题(C语言实现)
#include <stdio.h> #include <windows.h> #include <math.h> #pragma warning(disable:4996) #define N 8 int col[N] = { 0 };//存放每一行皇后的列标 int num = 0;//一共有几种方法 void print() { int i = ...
2020-05-06 17:39:30
1285
1
原创 扫雷小游戏(C语言实现)
今天给大家介绍一下如何用C语言实现一个简易的扫雷游戏 首先,要实现10 * 10的扫雷游戏,我们要先定义两个12 * 12的二维数组,一个用来显示给玩家操作,一个用来存放随机产生的雷的位置, 为什么是12 * 12呢 0 | 0 | 0 1 | x | 0 1 | 0 | 0 我们以3 * 3来举例,0 表示没有雷,1 表示有雷,x 为玩家输入的扫雷的位置,它的值为0,那么它现在要返回周围有几个雷...
2020-04-24 00:26:03
309
2
原创 冒泡排序(C语言实现)
今天给大家介绍一下如何用C语言来实现冒泡排序,首先,我们来了解一下什么是冒泡排序,给大家举个例子。 冒泡排序: 随便定义一个数组,从第一个数开始,和后一个数比较大小,如果后一个数更大,那么用后一个数再跟它后面的数比较,如果后一个数小于第一个数,则交换它们的位置,然后继续和后面的数比较,直到最后一个数,所以最后一个数是最大的,此时算是第一轮结束,然后继续比较,最后一个数除外,以此类推。 1 ,3,8...
2020-04-21 23:09:32
1274
1
原创 三子棋小游戏!!!(C语言实现)
今天给大家介绍一下如何用C语言实现三子棋小游戏,首先,我们来介绍一下三子棋的游戏规则。 规则:游戏分为玩家与电脑两方,双方执颜色不同的棋子在3*3的棋盘上下棋,每次每方只能落一子,哪方先连成一条线哪方获胜,可以是直线,竖线,对角线均可。 头文件 #ifndef _TEST_H_ #define _TEST_H_ #pragma warning(disable:4996) #include &l...
2020-04-21 22:41:20
284
原创 自我介绍
自我介绍 大家好,今天是我来优快云这个大家庭的第一天,这也是我在优快云写的第一篇博客,有点小激动,嘿嘿。 希望自己在这里能够更好的提升自我,学习更多有关编程的知识,在今后的日子里,我每周都至少抽出一部分的时间投入到编程中,完善自己的技术,分享自己的一些心得体会,和大家共同进步,争取在以后进入自己理想的公司。 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展...
2020-03-25 23:04:37
239
1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅