数据结构与算法
grey_orange
一只在不断努力并想成为大佬的小白~
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
2.25 图_BFS
2️⃣5️⃣ 图_BFS#include <iostream>#include <stack>#include <queue>using namespace std;#define MAX_VERTS 20class Vertex //顶点{public: Vertex(char lab): Label(lab) , wasVisited(0) {}public: bool wasVisited; char Label;};class Gr原创 2020-06-11 01:10:32 · 186 阅读 · 0 评论 -
2.24 图_DFS
2️⃣4️⃣ 图_DFS#include <iostream>#include <stack>using namespace std;#define MAX_VERTS 20class Vertex //顶点{public: Vertex(char lab): Label(lab) , wasVisited(0) {}public: bool wasVisited; char Label;};class Graph{public: Graph();原创 2020-06-11 01:07:41 · 166 阅读 · 0 评论 -
2.23 邻接表
2️⃣3️⃣ 邻接表???? 第一次接触类模板,迭代器,很新奇,希望以后能够慢慢理解,慢慢化为己用。#include <iostream>#include <list>using namespace std;class Vertex{public: char Label; Vertex(char lab):Label(lab) {}};ostream& operator << (ostream& out, const Vertex原创 2020-06-11 01:05:11 · 161 阅读 · 0 评论 -
2.22 邻接矩阵
2️⃣2️⃣ 邻接矩阵#include <iostream>using namespace std;#define MAX_VERTS 20class Vertex //顶点{public: Vertex(char lab): Label(lab) {}private: char Label;};class Graph{public: Graph(); ~Graph(); void addVertex(char lab); void addEdge(int原创 2020-06-10 22:59:08 · 166 阅读 · 0 评论 -
2.21 堆排序
2️⃣1️⃣ 堆排序???? 因为上节课学了堆,堆排序就是很简单的把数据放进堆,然后再取出来#include <iostream>#include <vector>using namespace std;class MaxHeap{private: int* heapArray; int maxSize; int currentSize; void trickleUp(int); //向上渗透 void trickleDown(int);//向下渗透pu原创 2020-06-10 22:06:22 · 137 阅读 · 0 评论 -
2.20 堆(完全二叉树)
2️⃣0️⃣ 堆(完全二叉树)???? 相比上一节的红黑树,这个代码好理解多了~#include <iostream>using namespace std;class MaxHeap{private: int* heapArray; int maxSize; int currentSize; void trickleUp(int); //向上渗透 void trickleDown(int);//向下渗透public: MaxHeap(int mx=10); ~M原创 2020-06-10 22:03:32 · 179 阅读 · 0 评论 -
2.19 红黑树
1️⃣9️⃣ 红黑树???? 不是个正确的代码,目前还没有能力把它改到正确,因为自己没能完全理解,以后学得更多了之后,再来改进吧。#include <iostream>using namespace std;class RedBlackTree;class RedBlackNode;class RedBlackNode{ friend class RedBlackTree;public: int data; RedBlackNode* left; RedBlackNod原创 2020-06-10 21:56:05 · 126 阅读 · 0 评论 -
2.18 二叉查找树
1️⃣8️⃣ 二叉查找树???? 有空把Delete和中序、前序、后序遍历加进来。#include <iostream>using namespace std;class BST;class BstNode{ friend class BST;private: int data; BstNode* LeftChild; BstNode* RightChild; void display(int i);public: int getdata(){return data;原创 2020-06-10 21:54:14 · 116 阅读 · 0 评论 -
2.17 基数排序
1️⃣7️⃣ 基数排序❔ radixsort函数可以理解,但是要自己敲出来有点困难,还要多多复习~#include <iostream>#include <vector>#include <list>using namespace std;int maxdigit(vector<int> &a){ int d = 1; int p = 10; for(int i=0; i<a.size(); i++){ while(a[i原创 2020-06-03 22:43:16 · 126 阅读 · 0 评论 -
2.16 链式队列
1️⃣6️⃣ 链式队列#include <iostream>using namespace std;class ListQueue{private: struct Node{ int data; Node* next; Node(int d, Node* n = 0):data(d), next(n) {} }; Node* Listfront; Node* Listback;public: ListQueue(); ~ListQueue(); bool I原创 2020-06-03 13:42:53 · 134 阅读 · 0 评论 -
2.15 链式栈
1️⃣5️⃣ 链式栈#include <iostream>using namespace std;class ListStack{private: struct Node{ int data; Node* next; Node(int d, Node* n = 0):data(d), next(n) {} }; Node* top;public: ListStack():top(0){} ~ListStack(){MakeEmpty();} bool IsEm原创 2020-06-03 13:42:15 · 136 阅读 · 0 评论 -
2.14 双向链表
1️⃣4️⃣ 双向链表#include <iostream>using namespace std;class DoubleList{private: struct DoubleNode{ DoubleNode* previous; int data; DoubleNode* next; DoubleNode(){} DoubleNode(int d){ data = d; previous = next = 0; } }; DoubleNod原创 2020-06-03 12:04:20 · 166 阅读 · 0 评论 -
2.13 循环链表
1️⃣3️⃣ 循环链表❓ 不知道为什么循环链表不能用析构函数,写了就出错。#include <iostream>using namespace std;class List{private: struct Node{ int data; Node* next; Node(){} Node(int d){ data = d; next = 0; } }; Node* head;public: List(); void Insert(const原创 2020-06-03 12:01:10 · 168 阅读 · 0 评论 -
2.12 链表
1️⃣2️⃣ 链表???? 反转感觉很难,要多加练习#include <iostream>using namespace std;class List{private: struct Node{ int data; Node* next; Node(int d){ data = d; next = 0; } }; Node* head;public: List(); ~List(); void Insert(const int i); vo原创 2020-06-03 12:00:37 · 147 阅读 · 0 评论 -
2.11 顺序队列
1️⃣1️⃣ 顺序队列#include <iostream>using namespace std;class Queue{private: int* queue; int front; int rear; int capacity;public: Queue(int quequeCapacity=10); bool IsEmpty()const; int Front()const; int Rear()const; void Push(const int i);原创 2020-06-02 12:41:08 · 184 阅读 · 0 评论 -
2.10 顺序栈
1️⃣0️⃣ 顺序栈#include <iostream>using namespace std;class mystack{private: int* Stack; int top; int capacity;public: mystack(int stackcapacity=10); ~mystack(); bool IsEmpty() const; int Top() const; void Push(const int i); void Pop();};原创 2020-06-02 12:40:21 · 169 阅读 · 0 评论 -
2.9 归并排序
9️⃣ 归并排序#include <iostream>#include <vector>using namespace std;void Merge(vector<int> &a, int left, int mid, int right){ if(left<mid&&mid<right){ Merge(a,left,(left+mid)/2,mid); Merge(a,mid,(mid+right)/2,right原创 2020-06-02 01:10:00 · 195 阅读 · 0 评论 -
2.8 快速排序
8️⃣ 快速排序#include <iostream>#include <vector>using namespace std;void Quick(vector<int> &a, int left, int right){ int key = a[left]; int i = left, j = right; while(i<j){ while(i<j&&a[j]>=key) j--; while(i&l原创 2020-06-02 01:07:59 · 153 阅读 · 0 评论 -
2.7 插入排序
7️⃣ 插入排序#include <iostream>#include <vector>using namespace std;void InsertSort(vector<int> &a){ int in, out, temp; for (out=1; out!=a.size(); out++){ in = out; temp = a[out]; while(a[in-1]>temp&&in>0){ a原创 2020-06-02 01:07:44 · 152 阅读 · 0 评论 -
2.6 Permutation
6️⃣ Permutation#include <iostream>#include <cstring>using namespace std;void Permutations(char* pa, int left, int right){ if(left==right) cout<<pa<<endl; else{ for(int i=left; i<right; i++){ swap(pa[left],pa[i]);原创 2020-06-02 01:04:38 · 225 阅读 · 0 评论 -
2.5 折半查找
5️⃣ 折半查找⭕️非递归#include <iostream>#include <vector>using namespace std;int Binary (const vector<int> &a, int left, int right, int x){ while (left<=right){ int center = (left + right) / 2; if (x==a[center]) return center;原创 2020-06-02 01:04:22 · 172 阅读 · 0 评论 -
2.4 顺序查找
4️⃣ 顺序查找#include <iostream>#include <vector>using namespace std;int SequentialSearch (const vector<int> &a, int x){ for (int i=0; i!= a.size(); i++) if(a[i]==x) return i; return -1;}int main(){ vector<int> a{3,6原创 2020-06-02 01:02:21 · 131 阅读 · 0 评论 -
2.3 SelectSort
3️⃣ SelectSort#include <iostream>#include <vector>using namespace std;void SelectSort(vector<int> &a){ int i, j, min; for (i=0; i!= a.size()-1; i++){ min = i; for (j=i; j!= a.size(); j++) if(a[j]< a[min]) min = j;原创 2020-06-02 01:02:04 · 161 阅读 · 0 评论 -
2.2 BubbleSort
2️⃣ BubbleSort#include <iostream>#include <vector>using namespace std;void BubbleSort(vector<int> &a){ int i, j; for (i=0; i!= a.size()-1; i++) for (j=0; j!= a.size()-i-1; j++) if(a[j]> a[j+1]) swap(a[j], a[j+1]);}原创 2020-06-02 00:59:41 · 202 阅读 · 0 评论 -
2.1 swap
1️⃣ swap#include <iostream>using namespace std;void swap (int &a, int &b){ int temp = a; a = b; b = temp;}int main(){ int x = 1, y = 2; swap(x, y); cout << x << y <<endl; return 0;}原创 2020-06-02 00:59:09 · 118 阅读 · 0 评论 -
4
4️⃣P8、9、10 给定N个整数的序列,求最大子列和【暴力法】【分治法】【⭐️动态规划】????⚖写在前面的总结:第一次接触分治法,理解尚可,代码难写,主要是自己现在对于递归不太熟练,理解的不够透彻;动态规划是第一次知道这个名词,但是这道题的动态规划方法理解起来并不困难,代码也挺简单的。同时也是第一次真正接触vector,真正地在代码里去使用它,竟然连简单的怎么初始化都不会,看来还要多学习。⭕️暴力法#include <iostream>#include <vector&原创 2020-05-30 22:27:04 · 122 阅读 · 0 评论 -
3
3️⃣P5 例1:选择排序算法????⚖这是第三次使用vector,发现如果在函数内需要改变数组中的值,传入数组的不能再像以前一样是const的vector数组了。#include <iostream>#include <vector>using namespace std;void selectionsort(vector<int> &a){ int i, j, min; for (i=0; i<a.size()-1; i++){ m原创 2020-05-30 22:26:24 · 382 阅读 · 0 评论 -
2
2️⃣P3 例3:写程序计算给定多项式在给定点x处的值f(x)=a0+a1x+…+an-1x(n-1)+anxn????⚖这道题是第二次使用vector,得出一点小经验是,和以前的a[n]形式的数组相比,vector的a.size()函数计算出的数组真实长度相当于以前定义数组时候的a[n]中的n,而不是之后调用数组的n。当调用数组的时候,长度为n的数组的最后一个元素的下标是n-1,即a[n-1],即a[a.size()-1]。⭕️普通:递归求指数+for循环#include <iostream原创 2020-05-30 22:25:56 · 723 阅读 · 0 评论 -
1
1️⃣P2 例2:写程序实现一个函数PrintN,使得传入一个正整数为N的参数后,能顺序打印从1到N的全部正整数⭕️循环#include <iostream>using namespace std;void PrintN (int N){ int i; for (i=1; i<=N; i++) cout << i << " ";}int main(){ int N; cin >> N; PrintN(N)原创 2020-05-30 22:22:08 · 644 阅读 · 0 评论
分享