Last active
January 23, 2022 15:08
-
-
Save Alafun/b4456a6714027ad2fc01ef6d5c4c75e2 to your computer and use it in GitHub Desktop.
顺序表C++实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************** | |
* To DO: | |
* - [x] 插入 | |
* - [x] 打印插入结果 | |
* - [x] 删除 | |
* - [x] 打印删除结果 | |
* - [x] 查找 | |
* - [x] 打印查找结果 | |
******************/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#define MAXSIZE 50 | |
#define INITSIZE 100 | |
/************************** | |
* 定义ElemType | |
***************************/ | |
typedef int ElemType; | |
/************************** | |
* 定义SqlList结构体 | |
***************************/ | |
typedef struct { | |
ElemType data[MAXSIZE]; | |
int len; | |
} SqList; | |
/************************** | |
* 定义打印列表元素函数 | |
***************************/ | |
void elemPrint(SqList& L) { | |
printf("列表 元素 依次 为:"); | |
for (int i = 0; i < L.len; i++){ | |
printf("%4d", L.data[i]); | |
} | |
printf("\n"); | |
} | |
/************************** | |
* 定义列表插入函数 | |
* i: 插入的位置 position | |
* e: 待插的元素 element | |
***************************/ | |
bool listInsert(SqList& L, int i, ElemType e) { | |
if (i<1 || i>L.len + 1 || L.len >= MAXSIZE){ | |
return false; | |
} | |
for (int j = L.len; j >= i; j--){ | |
L.data[j] = L.data[j - 1]; | |
} | |
L.data[i - 1] = e; | |
L.len++; | |
} | |
/***************************************** | |
* 定义列表删除函数 | |
* i: 删除的位置 position | |
* e: 待删的元素 element | |
******************************************/ | |
bool listDelete(SqList& L, int i,ElemType e) { | |
if (i<1||i>L.len){ | |
return false; | |
} | |
e = L.data[i - 1]; | |
for (int j = i; j < L.len; j++) { | |
L.data[j - 1] = L.data[j]; | |
} | |
L.len--; | |
return true; | |
} | |
/***************************************** | |
* 定义元素查找函数 | |
* i: 查找的位置 position | |
* e: 待查的元素 element | |
******************************************/ | |
int elemSearch(SqList L, ElemType e) { | |
int i; | |
for (i = 0; i < L.len; i++) { | |
if (L.data[i] == e) { | |
return i + 1; | |
} | |
} | |
return 0; | |
} | |
int main() { | |
SqList L; | |
/* 接收 return value */ | |
bool ret; | |
ElemType del; | |
/* 初始data数值有四个原始值 */ | |
L.data[0] = 1; | |
L.data[1] = 2; | |
L.data[2] = 3; | |
L.data[3] = 4; | |
L.len = 4; | |
/* 测试listInsert函数 */ | |
ret = listInsert(L, 2, 60); | |
listInsert(L, 2, 60); | |
listInsert(L, 2, 60); | |
listInsert(L, 2, 60); | |
/* 打印插入结果 */ | |
if (ret) { | |
printf("First Insert SUCCESS!\n"); | |
elemPrint(L); | |
}else{ | |
printf("BIG failure\n"); | |
} | |
/* 测试listDelete函数 */ | |
ret = listDelete(L, 2, 60); | |
listDelete(L, 2, 60); | |
listDelete(L, 2, 60); | |
listDelete(L, 2, 60); | |
/* 打印删除结果 */ | |
if (ret) { | |
printf("First Delete SUCCESS!\n"); | |
elemPrint(L); | |
} | |
else { | |
printf("BIG failure\n"); | |
} | |
/* 测试elemSearch函数 */ | |
ret = elemSearch(L, 60); | |
/* 打印查找结果 */ | |
if (ret) { | |
printf("Search SUCCESS!\n"); | |
printf("Position is %d",ret); | |
} | |
else { | |
printf("BIG failure\n"); | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define MAXSIZE 50 | |
typedef int ElemType; | |
typedef struct { | |
ElemType data[MAXSIZE]; | |
int len; | |
} SeqList; | |
// print all element of the list | |
void printList(SeqList &L) { | |
for (int i = 0; i < L.len; i++) { | |
printf("%3d", L.data[i]); | |
} | |
printf("\n"); | |
} | |
// insert element | |
void insertElem(SeqList &L, int position, ElemType elem) { | |
if (position < 1 || position > L.len + 1 || L.len >= MAXSIZE) { | |
printf("false\n"); | |
} else { | |
for (int i = L.len; i >= position; i--) { | |
L.data[i] = L.data[i - 1]; | |
} | |
L.data[position - 1] = elem; | |
L.len++; | |
printList(L); | |
} | |
} | |
// delete element | |
void deleteElem(SeqList &L, int position, ElemType elem) { | |
if (position < 1 || position > L.len) { | |
printf("false\n"); | |
} else { | |
elem = L.data[position - 1]; | |
for (int i = position; i < L.len; i++) { | |
L.data[i - 1] = L.data[i]; | |
} | |
L.len--; | |
printList(L); | |
} | |
} | |
int main() { | |
// init list | |
SeqList L; | |
L.data[0] = 1; | |
L.data[1] = 2; | |
L.data[2] = 3; | |
L.len = 3; | |
// element waiting for insert | |
ElemType elem; | |
scanf("%d", &elem); | |
insertElem(L, 2, elem); | |
// position waiting for delete | |
int position; | |
scanf("%d", &position); | |
deleteElem(L, position, elem); | |
system("pause"); | |
return 0; | |
} |
Author
Alafun
commented
Jan 23, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment