![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构复习
SWEENEY_HE
hold on
展开
-
7-49 打印学生选课清单 (25 分)
1、邻接表将学生作为结点,边表就是课程,但是所有学生信息是在最后输入的,不好处理。最后沿着这个思路,只能硬着头皮把课程当作结点,边表为各个课程。后续统计还要比较字符串查找边表。时间复杂度O(E+V),但是选课总数需要先输出,又要先遍统计一遍,非常不优雅。2、学生姓名映射地址开始有这个思路,但是不想用定义好的集合和哈希,不优雅。后来参考网上代码,一个非常优雅的方式:利用高维数组,将学生名各个字符作为1个维度,从而间学生名映射成唯一一个不冲突的地址。/*** Copyright(c).原创 2021-02-08 12:14:51 · 479 阅读 · 0 评论 -
7-6 列出连通集 (25 分)
/*** Copyright(c)* All rights reserved.* Author : Sweeney He* Date : 2021-02-06-09.38.37* Description : DFS\BFS*/#include <iostream>#include <stdlib.h>#include <cstring>#include<queue>using namespace std;//图typedef s.原创 2021-02-06 11:36:05 · 124 阅读 · 0 评论 -
7-50 畅通工程之局部最小花费问题 (35 分)
kruskal算法,选边法重点:利用并查集记录、判断连通分量/*** Copyright(c)* All rights reserved.* Author : Sweeney He* Date : 2021-02-05-11.33.17* Description : 算法*/#include <iostream>#include <stdlib.h>#include <cstring>#include<algorithm>#.原创 2021-02-05 17:57:46 · 238 阅读 · 0 评论 -
7-52 两个有序链表序列的交集 (20 分)
#include <iostream>#include <stdlib.h>#include <cstring>#define maxsize 1000000using namespace std;//记录上次查找最后位置int in = 0;//在s2序列中查找s1的值,由于s2有序,实际上只扫描了1~2个元素,时间复杂度只有O(n)bool hasElem(int*s,int e,int n){ for(int i = in ; i<.原创 2021-02-04 15:49:41 · 567 阅读 · 0 评论 -
7-51 两个有序链表序列的合并 (20 分)
C++ STL list实现 迭代器+摘除头遍历#include <iostream>#include <stdlib.h>#include <cstring>#include<list>using namespace std;int main(){ list<int> l1,l2,l3; int e; while(true){ cin>>e; if(..原创 2021-02-04 16:28:27 · 146 阅读 · 0 评论 -
7-53 两个有序序列的中位数 (25分)
两个长n序列合并后的中位数,必为合并后序列中的{n-1,n+1}中的1个,注意2n必定为偶数,所以不用考虑第n个题意为(n+1)/2向下取整,则取小者#include <iostream>#include <stdlib.h>#include <cstring>using namespace std;//获取中位数int getMidNum(int a[],int b[],int n){ int i = 0,j = 0,z = 1; .原创 2021-02-02 16:09:28 · 216 阅读 · 0 评论 -
7-5 堆中的路径 (25分)
注意本题必须边插入边调整#include <iostream>#include <stdlib.h>#include <cstring>using namespace std;typedef struct heapNode{ //堆首指针 int *heap; //数组长度 int len;}heap,*Heap;//建立一个堆Heap buildHeap(int n);//插入元素并调整void ...原创 2021-02-02 14:46:33 · 158 阅读 · 0 评论 -
7-4是否同一棵二叉搜索树 (25分)
!指针害死人#include <iostream>#include <stdlib.h>#include <cstring>#define ElementType int#define maxsize 12using namespace std;typedef struct Bstree{ ElementType data; struct Bstree *left; struct Bstree *right;}bst,*BS原创 2021-02-01 15:35:26 · 163 阅读 · 0 评论 -
7-2 一元多项式的乘法与加法运算 (20分)
纯模拟法:模拟手工求法#include<stdio.h>#include<stdlib.h>#include<iostream>#define Maxsize 1001using namespace std;typedef struct Mul{ //指数 int index[Maxsize]; //系数 int coefficient[Maxsize]; //项数 int n;}Mul,*Mul..原创 2021-01-24 15:42:01 · 285 阅读 · 0 评论 -
7-1 最大子列和问题(20分)
1.暴力思路:枚举子序列的起点和终点暴力法的优化思路集中在子序列求和/**** 最初版想法,暴力枚举与暴力求和时间复杂度:O(n^3), 后来想到打表优化求和 时间复杂度降到O(n^2) 空间复杂度O(n)****/#include<stdio.h>#include<iostream>#define Maxsize 100005using namespace std;//s[t]:从0到t-1序列的和long long s[M.原创 2021-01-23 16:04:07 · 159 阅读 · 0 评论 -
6-12 二叉搜索树的操作集 (30分)
/** 头文件、宏定义与自测主函数**/#include<stdio.h>#include<stdlib.h>#include<iostream>#include<stack>#define MAXSIZE 11#define ERROR -1#define NotFound 0;using namespace std;typedef int ElementType;typedef struct TNode *Posi.原创 2021-01-23 00:40:51 · 414 阅读 · 0 评论 -
6-11 先序输出叶结点 (15分)
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<stack>#define ElementType int#define MAXSIZE 11#define ERROR -1#define NotFound 0typedef struct TNode *Position;typedef Position BinTree;struct TNode{ El.原创 2021-01-21 22:26:58 · 356 阅读 · 0 评论 -
6-10 二分查找 (20分)
//二分查找,非递归Position BinarySearch( List L, ElementType X ){ int l = 1; int h = L->Last; int mid; while(l<=h){ mid = (h+l)/2; //范围变成小半部分l~mid-1 if(L->Data[mid]>X) h = mid-1; //范围变成大半部分.原创 2021-01-21 21:40:35 · 124 阅读 · 0 评论 -
6-9 二叉树的遍历 (25分)
1.递归版#include<stdio.h>#include<stdlib.h>#include<iostream>#define char ElementType#define MAXSIZE 5#define ERROR -1typedef struct TNode *Position;typedef Position BinTree;struct TNode{ ElementType Data; BinTree Left;.原创 2021-01-21 17:53:08 · 307 阅读 · 1 评论 -
6-8 求二叉树高度 (20分)
1.递归法/*** 递归思想:max(left,right)+1**/int GetHeight(BinTree BT){ //走到尽头 if(!BT) return 0; int left = GetHeight(BT->Left); int right = GetHeight(BT->Right); return (left>right?left:right)+1;}2.层次遍历思想/** 层原创 2021-01-20 17:42:13 · 368 阅读 · 1 评论 -
6-7 在一个数组中实现两个堆栈 (20分)
//创建栈Stack CreateStack( int MaxSize ){ Stack _stack = (Stack)malloc(sizeof(struct SNode)); _stack->MaxSize = MaxSize; _stack->Data = (int *)malloc(MaxSize*sizeof(int)); //指向当前栈顶元素 _stack->Top1 = -1; //指向栈顶元素 _stack-&g.原创 2021-01-20 16:53:20 · 137 阅读 · 0 评论 -
6-3 求链式表的表长 (10分)
int Length( List L ){ int len = 0; while(L){ len++; L = L->Next; } return len; }原创 2021-01-19 22:37:48 · 110 阅读 · 0 评论 -
6-2 顺序表操作集 (20分)
注意细节//创建并返回一个空的线性表;List MakeEmpty(){ List t = (List)malloc(sizeof(struct LNode)); t->Last = -1; return t;}//返回线性表中X的位置。若找不到则返回ERROR;Position Find( List L, ElementType X ){ int i = 0; for(;i<=L->Last;i++){ if(L-.原创 2021-01-19 22:39:49 · 125 阅读 · 0 评论 -
6-1 单链表逆转 (20分)
注意该题没有头结点1.头插法逆置注意没有头节点即可//逆置链表无头节点List Reverse(List L){ if(!L) return NULL; //自己创建一个头节点 List head = (List)malloc(sizeof(Node)); //必须给头节点下一个赋值空,否则逆置后最后一个结点不为空导致输出无法终止 head->Next = NULL; //保存L的下一个结点 List next;.原创 2021-01-19 16:51:26 · 338 阅读 · 0 评论