Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from KimYangOfCat/dev
Browse files Browse the repository at this point in the history
Dev update
  • Loading branch information
kimYang authored Aug 16, 2020
2 parents 7793374 + f8d4e36 commit 9133afe
Show file tree
Hide file tree
Showing 19 changed files with 489 additions and 383 deletions.
63 changes: 32 additions & 31 deletions DataStructure/DS_0_Introduction/DS_0_0_love.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,77 +10,78 @@
//下面四种函数的时间复杂度值得分析一二

//逐步递增型爱你
void LoveYou0(int n){
int i=1;
while (i<=n){
printf("I love you %d \n",i);
void LoveYou0(int n) {
int i = 1;
while (i <= n) {
printf("I love you %d \n", i);
i++;
}
printf("I love you more than %d\n",n);
printf("I love you more than %d\n", n);
}

//嵌套循环型爱你
void loveYou1(int n){
int i=1;
while (i<=n){
void loveYou1(int n) {
int i = 1;
while (i <= n) {
i++;
printf("I love you %d\n",i);
for (int j = 1; j <n ; j++) {
printf("I love you %d\n", i);
for (int j = 1; j < n; j++) {
printf("I love you too\n");
}
}
printf("I love you more than %d\n",n);
printf("I love you more than %d\n", n);
}

//指数递增型爱你
void loveYou2(int n){
int i=1;
while (i<=n){
printf("I love you %d\n",i);
i=i*2;
void loveYou2(int n) {
int i = 1;
while (i <= n) {
printf("I love you %d\n", i);
i = i * 2;
}
printf("I love you more than %d\n ",n);
printf("I love you more than %d\n ", n);
}

//搜索数字型爱你
void loveYou3(int flag[],int n){
void loveYou3(int flag[], int n) {
printf("I Am kim\n");
for (int i = 0; i < n; i++) {
//我觉这里应该是数组长度
if (flag[i]==n){
printf("I love you %d\n",n);
if (flag[i] == n) {
printf("I love you %d\n", n);
break;//找到之后就跳出循环
}
}
}

//递归型爱你
void loveYou4(int n){
int a,b,c;
if (n>1){
loveYou4(n-1);
void loveYou4(int n) {
int a, b, c;
if (n > 1) {
loveYou4(n - 1);
}
printf("I love you %d\n",n);
printf("I love you %d\n", n);
}//递归调用会带来多余的内存开销

/**实现模块**/

/**测试模块**/

//测试函数
void Test(){
void testModule() {
LoveYou0(30);
loveYou1(30);
loveYou2(30);

int array[5]={2,10,100,1000,10000};
int array[5] = {2, 10, 100, 1000, 10000};
//声明一个数组并初始化
loveYou3(array,10);
loveYou3(array, 10);
loveYou4(4);

}

/**测试模块**/

int main(){
Test();
int main() {
testModule();
return 0;
}
163 changes: 89 additions & 74 deletions DataStructure/DS_1_LinearList/DS_1_0_SqList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,75 @@
/**定义模块**/

#include <stdio.h>

#define MaxSize 10

typedef struct {
int data[MaxSize];
int length;
}SqList;
} SqList;

//函数声明
void PrintSqList(SqList L);
void InitList(SqList &L); //初始化
bool Empty(SqList L); //判空
bool ListInsert(SqList &L, int i, int e); //插入
bool ListDelete(SqList &L, int i, int &e); //删除
int GetElem(SqList L, int i); //按位查找
int LocateElem(SqList L, int e); //按值查找
bool LocateChangeElem(SqList &L, int e, int em); //按值修改
bool getChangeElem(SqList &L, int i, int em); //按位修改

void PrintSqList(SqList L); //打印函数
void testModule(); //测试模块

/**定义模块**/

/**实现模块**/

//初始化
void InitList(SqList &L){
void InitList(SqList &L) {
for (int i = 0; i < MaxSize; i++) {
L.data[i]=0;//将所有元素的初始值默认设置为0
L.data[i] = 0;//将所有元素的初始值默认设置为0
//这一步其实可以省略,但是省略之后,有可能受到内存中"脏数据"的影响
}
L.length=0;
L.length = 0;

}

//判空
bool Empty(SqList L){
return (L.length==0);
bool Empty(SqList L) {
return (L.length == 0);
}

//插入
bool ListInsert(SqList &L,int i,int e){
bool ListInsert(SqList &L, int i, int e) {
//判断插入的位置是否合法,
if (i<1||i>L.length+1)
if (i < 1 || i > L.length + 1)
return false;
//判断表是否存满了
if (L.length>=MaxSize)
if (L.length >= MaxSize)
return false;

//后面的元素后移
for (int j = L.length; j >=i ; j--) {
L.data[j]=L.data[j-1];
for (int j = L.length; j >= i; j--) {
L.data[j] = L.data[j - 1];
}
L.data[i-1]=e;
L.data[i - 1] = e;
L.length++;
return true;
}

//删除
bool ListDelete(SqList &L,int i,int &e){
bool ListDelete(SqList &L, int i, int &e) {
//判断i的位置是否合法
if(i<0||i>L.length){
if (i < 0 || i > L.length) {
return false;
}
//取出将要被删除的数
e=L.data[i-1];
e = L.data[i - 1];
//将其后的数据前移
for (int j = i; j <=L.length ; j++) {
L.data[j-1]=L.data[j];
for (int j = i; j <= L.length; j++) {
L.data[j - 1] = L.data[j];
}
//线性表长度减一
L.length--;
Expand All @@ -73,19 +85,19 @@ bool ListDelete(SqList &L,int i,int &e){

//查找
//按位查找
int GetElem(SqList L,int i){
int GetElem(SqList L, int i) {
//判断是否越界
if (i<0||i>L.length)
if (i < 0 || i > L.length)
return -1;
return L.data[i-1];
return L.data[i - 1];
}

//按值查找
int LocateElem(SqList L,int e){
int LocateElem(SqList L, int e) {
//循环出查找
for (int i = 0; i <L.length ; i++) {
if (L.data[i]==e)
return i+1; //返回位序
for (int i = 0; i < L.length; i++) {
if (L.data[i] == e)
return i + 1; //返回位序
}
return -1;
}
Expand All @@ -94,50 +106,30 @@ int LocateElem(SqList L,int e){
//先查找后改值
//由此分为两种方式,先按位查找后改值;或先按值查找后改值
//先按值查找后改值
bool LocateChangeElem(SqList &L,int e,int em){
bool LocateChangeElem(SqList &L, int e, int em) {
//按值查找得到位序
int bitOrder =LocateElem(L,e);
int bitOrder = LocateElem(L, e);
//改值
if (bitOrder !=-1){
L.data[bitOrder]=em;
if (bitOrder != -1) {
L.data[bitOrder] = em;
return true;
} else{
} else {
return false;
}
}

//先按位序查找后改值
bool getChangeElem(SqList &L,int i,int em){
bool getChangeElem(SqList &L, int i, int em) {
//注意由于是改值涉及修改原数据,所以需要用引用传递的方式
//给的位序,首先判断i是否合法
if(i<0||i>=L.length)return false;
if (i < 0 || i >= L.length)return false;

//由于是用数组实现的方式,可以直接利用i查找
L.data[i]=em;
L.data[i] = em;
return true;

}

//Change Test
void ChangeTest(SqList &L,int e,int em1,int i, int em2){
//change 修改元数据用引用的方式
printf("开始测试【改】\n"
"第一种方式先按值查找后改值\n");
if (LocateChangeElem(L,e,em1)){
printf("第一种先按值查找后改值成功啦,改变后的值如下:\n");
PrintSqList(L);
} else{
printf("第一种先按值查找后改值失败了,再检查一下吧!\n");
}
printf("第二种先按位序查找后改值\n");
if (getChangeElem(L,i,em2)){
printf("第二种先按位序查找后改值的方式成功啦,改变后的值如下:\n");
PrintSqList(L);
} else{
printf("第二种先按位序查找后改值的方式失败了,再检查一下吧!\n");
}


}

//销毁
//由于静态分配方式是通过声明数组的方式实现的,故不需要手动销毁SqList表,在使用完成之后,系统会自动删除数据并回收数据空间
Expand All @@ -148,62 +140,85 @@ void ChangeTest(SqList &L,int e,int em1,int i, int em2){

//测试
//打印整个顺序表
void PrintSqList(SqList L){
void PrintSqList(SqList L) {
//循环打印
printf("开始打印顺序表\n");
for (int i = 0; i < L.length ; i++) {
printf("Data[%d]==%d\n",i,L.data[i]);
for (int i = 0; i < L.length; i++) {
printf("Data[%d]==%d\n", i, L.data[i]);
}
printf("打印结束!\n");
}

//测试函数
void Test(){
void testModule() {
SqList L;
InitList(L);

// 初试化一些值
L.data[0]=1;
L.data[1]=2;
L.data[2]=3;
L.length=3;
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 3;
L.length = 3;

//插入操作
if (ListInsert(L,2,3)){
if (ListInsert(L, 2, 3)) {
printf("插入成功了\n");
} else{
} else {
printf("插入失败了,i的位置不合法,请检查\n");
}

//删除操作
int e=-1;
if (ListDelete(L,2,e)){
printf("删除成功!删除的值是:%d\n",e);
} else{
int e = -1;
if (ListDelete(L, 2, e)) {
printf("删除成功!删除的值是:%d\n", e);
} else {
printf("删除失败,请检查位序是否正确\n");
}

//数组当前长度
printf("数组当前长度是多少?%d\n",L.length);
printf("数组当前长度是多少?%d\n", L.length);

//查找第一个元素是什么?
printf("第一个元素是什么?\n %d\n",GetElem(L,1));
printf("第一个元素是什么?\n %d\n", GetElem(L, 1));

//查找值为3的元素在什么位置
printf("第一个值为3的元素在什么位置?\n %d \n",LocateElem(L,3));
printf("第一个值为3的元素在什么位置?\n %d \n", LocateElem(L, 3));

//打印输出
PrintSqList(L);

//测试改模块功能是否正常
ChangeTest(L,2,6,1,7);
int e1 = 2;
int em1 = 6;
int i = 1;
int em2 = 7;
printf("开始测试【改】\n"
"第一种方式先按值查找后改值\n");
if (LocateChangeElem(L, e1, em1)) {
printf("第一种先按值查找后改值成功啦,改变后的值如下:\n");
PrintSqList(L);
} else {
printf("第一种先按值查找后改值失败了,再检查一下吧!\n");
}
printf("第二种先按位序查找后改值\n");
if (getChangeElem(L, i, em2)) {
printf("第二种先按位序查找后改值的方式成功啦,改变后的值如下:\n");
PrintSqList(L);
} else {
printf("第二种先按位序查找后改值的方式失败了,再检查一下吧!\n");
}
if (Empty(L)) {
printf("顺序表为空!\n");
} else {
printf("顺序表非空!\n");
}

//打印输出
PrintSqList(L);
}
/**测试模块**/
//主函数
int main(){
Test();
int main() {
testModule();
return 0;
}
Loading

0 comments on commit 9133afe

Please sign in to comment.