数据结构SWUSTOJ
~|Bernard|
人在考研 有文章代码出错谨请见谅研后再与各位大佬进行指正交流
展开
-
数据结构之SWUSTOJ956: 约瑟夫问题的实现
题目:代码:#include<iostream>using namespace std;typedef struct SList{ int data; struct SList* next;}SL;void SLInit(SL** ps){ *ps = (SL*)malloc(sizeof(SL)); (*ps)->next = NULL;}//开辟头结点void SLCreate(SL** ps,int n){ SL* head = *ps..原创 2022-04-23 22:06:20 · 1215 阅读 · 1 评论 -
数据结构之SWUSTOJ1027: 舞伴问题
题目:思路:采用循环队列的思想,队首元素出队列,然后再进入队尾。代码:#include<iostream>#include<string.h>using namespace std;typedef struct SList{ char data; struct SList* next;}SL;typedef struct Queue{ SL* front; SL* rear;}QU;QU* QueueCreate(char* arr.原创 2022-04-23 21:00:19 · 681 阅读 · 1 评论 -
数据结构之SWUSTOJ963: 小偷的背包
题目:思路:此题就是将题目所给的数据进行入栈,大家可以从题目给的例子中发现,这是一个升序序列,入栈过后最大的数就在栈顶,每一次取出来的都是栈中最大的,每一次进行相加,如果加到大于s,那么就舍弃这个栈顶元素,出栈一次,继续循环加下面的元素,直到栈为空为止。代码:#include<iostream>using namespace std;typedef struct Stack{ int* data; int top; int capacity;}ST;/.原创 2022-04-22 20:53:54 · 499 阅读 · 0 评论 -
数据结构之SWUSTOJ942: 逆置顺序表
题目:代码:#include<iostream>using namespace std;typedef struct SList{ char* data; int size; int capacity;}SL;//建立一个动态顺序表结构体void SLInit(SL* ps){ ps->data = NULL; ps->size = ps->capacity = 0;}//初始化动态顺序表void SLCreate(SL* ps){原创 2022-04-22 20:23:15 · 339 阅读 · 1 评论 -
数据结构之SWUSTOJ1042: 中缀表达式转换为后缀表达式
题目:思路:此题题目表述很不清晰,并且测试用例给得太少了,非常不好去寻找思路,做起来可能比较让人头疼,下面将思路图解画出来理解一下。代码:#include<iostream>using namespace std;#include<string.h>typedef struct Stack{ char* data; int top;//记录栈顶位置 int capacity;//记录容量大小}ST;void StackIni.原创 2022-04-22 00:49:33 · 2210 阅读 · 0 评论 -
数据结构之SWUSTOJ1043: 利用栈完成后缀表达式的计算
题目:思路:首先一看题目,这题读题分析了大半个小时一点思路也没有,看了解析过后大致明白了题目意思,下面图解说明一下。代码:#include<iostream>using namespace std;typedef struct Stack{ char* data; int top; int capacity;}ST;void STInit(ST* ps){ ps->data = NULL; ps->top = 0; ps->.原创 2022-04-19 23:29:30 · 731 阅读 · 0 评论 -
数据结构之SWUSTOJ1046: 链栈基本操作的实现
题目:思路:链式栈的实现要注意,一定要区分好栈顶元素在头上,栈底在尾。头结点是栈顶元素,插入数据的时候需要去破坏头结点。代码:#include<iostream>using namespace std;typedef struct Stack{ int data; struct Stack* next;}ST;//链表结构体的创建void StackInit(ST** ps){ (*ps) = (ST*)malloc(sizeof(ST));.原创 2022-04-18 21:05:05 · 696 阅读 · 2 评论 -
数据结构之SWUSTOJ1039: 单链表中信息的分类
题目:代码:#include<iostream>#include<string.h>#include<stdio.h>using namespace std;typedef struct SList{ char data; struct SList* next;}SL;//单链表结构体的创建void SListInit(SL** ps){ (*ps) = (SL*)malloc(sizeof(SL)); (*ps)->next原创 2022-04-18 19:54:06 · 256 阅读 · 0 评论 -
数据结构之SWUSTOJ1231: 寻找出现次数最多的数
题目:思路:相信看到这道题大家第一个想到的应该都是暴力求解的方法,去套两层循环求解。这样的方法是不靠谱的,时间复杂度达到了O(N),效率非常低。那么我们这里采用一种用空间换取时间复杂度的思路。这里要利用一下计数排序的思想:代码:#include<iostream>using namespace std;int main(){ int n = 0; cin >> n; int arr[10001] = { 0 };//题目中有说明数.原创 2022-04-17 12:33:26 · 2043 阅读 · 4 评论 -
数据结构之SWUSTOJ977: 统计利用先序遍历创建的二叉树中的空链域个数
题目:代码:#include<iostream>using namespace std;typedef struct BinaryTree{ char data; struct BinaryTree* leftchild; struct BinaryTree* rightchild;}BT;void BinaryTreePreCreate(BT*& root)//注意要传引用创建二叉树{ char a; cin >> a; if (a =原创 2022-04-17 11:36:33 · 457 阅读 · 0 评论 -
数据结构之SWUSTOJ976: 统计利用先序遍历创建的二叉树的度为1的结点个数
题目:思路一:度为1的结点个数:要么左子树为空,右子树不为空;或者右子树为空,左子树不为空;采用这种思想不断地向下迭代。代码:#include<iostream>using namespace std;typedef struct BinaryTree{ char data; struct BinaryTree* leftchild; struct BinaryTree* rightchild;}BT;void BinaryTreePreCreate(BT.原创 2022-04-17 11:19:24 · 1138 阅读 · 0 评论 -
数据结构之SWUSTOJ972: 统计利用先序遍历创建的二叉树的宽度
题目:思路:通过上述图解,我们首先需要先序遍历创建好二叉树,然后我们需要求出这个二叉树的层数k,然后通过递归到最后一层的时候来计算宽度大小代码:#include<iostream>using namespace std;typedef struct BinaryTree{ char data; struct BinaryTree* leftchild; struct BinaryTree* rightchild;}BT;//创建二叉树的结构体void .原创 2022-04-17 10:56:09 · 1557 阅读 · 0 评论 -
数据结构之SWUSTOJ975: 统计利用先序遍历创建的二叉树的度为2的结点个数
题目:思路一:采用二叉树的性质来解决这个题!对任何一颗二叉树,度为0的结点数永远比度为2的结点数多一个代码一:#include<iostream>using namespace std;typedef struct BinaryTree{ char data; struct BinaryTree* left; struct BinaryTree* right;}BT;void BinaryTreeCreate(BT*& ps)//二叉树创建原创 2022-04-16 18:21:00 · 400 阅读 · 4 评论 -
数据结构之SWUSTOJ973: 统计利用先序遍历创建的二叉树叶结点的个数
题目:代码:#include<iostream>using namespace std;typedef struct BinaryTree{ char data; struct BinaryTree* left; struct BinaryTree* right;}BT;void BinaryTreeCreate(BT*& ps)//二叉树创建{ char a; cin >> a; if (a == '#') ps = NU.原创 2022-04-16 17:32:45 · 855 阅读 · 0 评论 -
数据结构之971: 统计利用先序遍历创建的二叉树的深度
题目:代码:#include<iostream>using namespace std;typedef struct BinaryTree{ char data; struct BinaryTree* left; struct BinaryTree* right;}BT;void BinaryTreeCreate(BT*& ps){ char a; cin >> a; if (a == '#') ps = NULL; el.原创 2022-04-16 17:17:07 · 2357 阅读 · 0 评论 -
数据结构之SWUSTOJ978: 输出利用先序遍历创建的二叉树的中序遍历序列 and SWUSTOJ979: 输出利用先序遍历创建的二叉树的后序遍历序列
题目:代码:#include<iostream>using namespace std;typedef struct Binarynode{ char data; struct Binarynode *left; struct Binarynode *right;}BT;//建立二叉树结构体void Create(BT* &ps)//采用传参传引用的方式{ char x; scanf("%c", &x); if (x == '#')//.原创 2022-04-16 16:57:51 · 661 阅读 · 0 评论 -
数据结构之SWUSTOJ965循环队列and Leetcode622设计循环队列。对循环队列的一点思考。(数组和链表两种方式实现循环队列)
题目:思路:代码:622. 设计循环队列 - 力扣(LeetCode) (leetcode-cn.com)https://leetcode-cn.com/problems/design-circular-queue/循环队列是队列的一种 顺序 存储结构。 循环队列的引入,目的是为了克服 假溢出时大量移动数据元素 。 在循环队列中,队列长度为n,存储位置从0到n-1编号,以rear指示实际的队尾元素,现要在此队列中插入一个新元素,新元素的位置是 rear= (rear+原创 2022-04-16 16:13:46 · 1362 阅读 · 5 评论 -
数据结构之SWUSTOJ962: 括号匹配问题 and LeetCode20有效的括号
题目:思路:代码:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stdbool.h>typedef struct Stack{ char* data; int top;//栈顶元素坐标 int capacity;//栈的容量}ST;void STInit(ST* ps){ ps->data = NULL; ps->top原创 2022-04-10 18:09:11 · 1914 阅读 · 0 评论 -
数据结构之SWUSTOJ1028: 特定字符序列的判断
题目:此题关键思路:代码:#include<stdio.h>#include<stdlib.h>#include<stdbool.h>typedef struct Stack{ char* data; int top;//栈顶元素坐标 int capacity;//栈的容量}ST;void STInit(ST* ps){ ps->data = NULL; ps->top = 0;//ps->top=.原创 2022-04-10 17:21:52 · 1278 阅读 · 0 评论 -
数据结构之SWUSTOJ1044: 顺序栈基本操作的实现
题目:代码:#include<stdio.h>#include<stdlib.h>typedef struct Stack{ int* data; int top;//栈顶元素坐标 int capacity;//栈的容量}ST;void STInit(ST* ps){ ps->data = NULL; ps->top = 0;//ps->top=-1; //初始化时top给0,意味着top指向栈顶数据的下一个 //初始化.原创 2022-04-10 16:12:35 · 746 阅读 · 0 评论 -
数据结构之SWUSTOJ961: 进制转换问题
题目:代码:#include<stdio.h>#include<stdlib.h>typedef struct Stack{ int* data; int top;//栈顶 int capacity;//栈的容量}ST;void SLInit(ST* ps){ ps->data = NULL; ps->capacity = 0; ps->top = 0;//ps->top=-1 //初始化时top给0,意味着top指向栈原创 2022-04-10 15:49:17 · 1414 阅读 · 0 评论 -
SWUSTOJ1040: 一元多项式加法运算的实现
题目:思路:在做这个题之前呢,首先得看懂理解整个题的题意:或者说最后一步打印之前再加一个删除底数为0的节点操作再来打印也是可以的:代码一:#include<stdio.h>#include<stdlib.h>typedef struct SList{ int data; int index; struct SList* next;}SL;void SLInit(SL** ps)...原创 2022-04-07 11:49:28 · 560 阅读 · 0 评论 -
数据结构之SWUSTOJ1036: 寻找整数序列的主元素
题目:代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SList{ int data[N]; int size;}SL;void SLInit(SL* ps){ ps = (SL*)malloc(sizeof(SL)); ps->size = 0;}//顺序表初始化void SLCreate(SL* ps, int n){ ps->siz原创 2022-04-06 23:18:44 · 672 阅读 · 0 评论 -
数据结构之SWUSTOJ1035: 定位顺序表中的最大和最小值
题目:代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SList{ int data[N]; int size;}SL;void SLInit(SL* ps){ ps = (SL*)malloc(sizeof(SL)); ps->size = 0;}//顺序表初始化void SLCreate(SL* ps, int n){ ps->s原创 2022-04-06 21:27:49 · 1484 阅读 · 0 评论 -
数据结构之SWUSTOJ1045: 集合的交运算的实现
题目:代码:#include<stdio.h>#include<stdlib.h>typedef struct SList{ int data; struct SList* next;}SL;void SLInit(SL** ps){ *ps = (SL*)malloc(sizeof(SL)); (*ps)->next = NULL;}//链表初始化void SLCreate(SL** ps, int n){ SL* cur = .原创 2022-04-06 21:11:54 · 616 阅读 · 0 评论 -
数据结构之SWUSTOJ1037: 集合的并运算的实现
题目:代码:#include<stdio.h>#include<stdlib.h>typedef struct SList{ int data; struct SList* next;}SL;void SLInit(SL** ps){ *ps = (SL*)malloc(sizeof(SL)); (*ps)->next = NULL;}void SLCreate(SL** ps, int n){ SL* cur = *ps; i.原创 2022-04-04 09:18:34 · 213 阅读 · 0 评论 -
数据结构之SWUSTOJ1038: 顺序表中重复数据的删除
题目:思路:这个题目相对来说主要思路在于SLPop这个函数的设计上。如果找到了等于m的数,那么需要分情况来讨论:第一种情况就是普通情况,也就是在中间找到等于m的数,那么之需要把每个数依次向前挪动一个位置即可,并且这是的size要减少一个。第二种情况就是在末尾的时候找到等于m的数,这时就不能够再去把后面的数移动在前面来,因为这样就会造成数组访问越界,只需要把size较少一个即可。还需要注意的一个问题:当每一次找到等于m的数时,我们都完成了后数的移动到前面的过程,那么这.原创 2022-04-04 09:16:10 · 798 阅读 · 0 评论 -
数据结构之SWUSTOJ1103: 删除顺序表中指定区间的数据
题目:思路一:利用一个新的顺序表来存储不在这个区间内的数据,然后返回打印。这个思路相对来说时间复杂度为O(N),空间复杂度近似于O(N),采取了用空间换取时间的做法,在这题里面相对较为明智,OJ也让过,但是这题OJ也没有规定时间复杂度和空间复杂度代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SList{ int data[N]; int size;}SL;v原创 2022-03-30 22:51:38 · 1059 阅读 · 0 评论 -
数据结构之SWUSTOJ1102: 顺序表上数据的划分问题的实现
题目:思路:代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SList{ int data[N]; int size;}SL;void SLInit(SL* ps){ ps = (SL*)malloc(sizeof(SL)); ps->size = 0;}void SLCreate(SL* ps, int n){ ps-&g..原创 2022-03-30 22:07:37 · 678 阅读 · 0 评论 -
数据结构之SWUSTOJ1101: 顺序表中数据的循环移动
题目:解题思路:代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SList{ int data[N]; int size;}SL;void SLInit(SL* ps){ ps = (SL*)malloc(sizeof(SL)); ps->size = 0;}void SLCreate(SL* ps,int n){...原创 2022-03-29 22:46:28 · 675 阅读 · 0 评论 -
数据结构之SWUSTOJ960: 双向链表的操作问题
题目:解析:此题时一个关于带头双向链表的问题,并且加入了选择排序的知识。原创 2022-03-29 13:10:10 · 969 阅读 · 1 评论 -
数据结构之SWUSTOJ957: 逆置单链表
题目:代码:#include<stdio.h>#include<stdlib.h>typedef struct SList{ char data; struct SList* next;}SL;void SListInit(SL** ps){ *ps = (SL*)malloc(sizeof(SL)); (*ps)->next = NULL;}void SListCreate(SL** ps, int n){...原创 2022-03-28 18:09:20 · 1134 阅读 · 0 评论 -
数据结构之SWUSTOJ953: 单链表的删除操作的实现
题目:解题思路:SListPop函数的设计需要尤其注意,首先需要画图画出三种情况来讨论一下边界问题。特别是头结点的情况,处理尾节点时循环边界的选择上,然后就是删除中间值的情况,并且在最后进行一个返回,这里的返回也同样需要注意,返回之前要提前存一下头结点,遍历的过程中指针位置会发生变化,如果不把头结点先存起来,返回会出问题代码:#include<stdio.h>#include<stdlib.h>#include<stdbool.h>t原创 2022-03-27 23:59:29 · 990 阅读 · 0 评论 -
数据结构之SWUSTOJ955: 单链表上查找算法的实现
题目:代码:#include<stdio.h>#include<stdlib.h>typedef struct SList{ int data; struct SList* next;}SL;void SListInit(SL** ps){ *ps = (SL*)malloc(sizeof(SL)); (*ps)->next = NULL;}//建立一个哨兵位头结点void SListCreate(SL** ps, ...原创 2022-03-27 22:22:09 · 1347 阅读 · 0 评论 -
数据结构之SWUSTOJ954: 单链表的链接
题目:解析:这个题两个数据类型不一样,可能大家会疑惑这样两种数据类型不一样的链表怎么才能够链接在一起呢,其实我们只需要将数据类型置为char即可,并不需要设计两个结构体,并且在输入和输出时要注意用%c,因为%c也可以用于输入输出整数的代码:#include<stdio.h>#include<stdlib.h>typedef struct SLListChar{ char data; struct SLListChar* next;}SL...原创 2022-03-27 21:08:20 · 1131 阅读 · 0 评论 -
数据结构之SWUSTOJ952: 单链表的插入操作的实现
题目:解题思路:如果对指针掌握得不好的建议可以看看下面两篇文章,找找灵感:C语言进阶指针详解完整版(数组指针,指针数组的辨析,函数指针)_YRedd22的博客-CSDN博客https://blog.csdn.net/weixin_56054625/article/details/122633094?spm=1001.2014.3001.5502数据结构之链表-2022-3-6_YRedd22的博客-CSDN博客https://blog.csdn.net/weixin_5605462.原创 2022-03-27 19:52:56 · 686 阅读 · 0 评论 -
数据结构之SWUSTOJ943: 顺序表插入操作的实现
题目:解题思路:代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SequeceList{ int a[N]; int size;}SL;void SLInit(SL* ps){ ps = (SL*)malloc(sizeof(SL)); ps->size = 0;}void SLCreate(SL* ps){...原创 2022-03-26 19:01:48 · 1283 阅读 · 0 评论 -
数据结构 SWUST OJ941 有序顺序表的合并操作的实现
题目:首先看看代码:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct SequeceList{ int a[N]; int size;}SL;void MergeOperation(SL* inputA, SL* inputB){ SL Output; SL* output = &Output; output->size ...原创 2022-03-26 17:08:55 · 1738 阅读 · 0 评论