mirror of
https://github.com/huihut/interview.git
synced 2025-12-18 13:04:38 +03:00
改进数据结构代码
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
#define OVERFLOW -1
|
||||
#define SUCCESS 1
|
||||
#define UNSUCCESS 0
|
||||
|
||||
#define dataNum 5
|
||||
int i = 0;
|
||||
int dep = 0;
|
||||
@@ -17,33 +16,20 @@ char data[dataNum] = { 'A', 'B', 'C', 'D', 'E' };
|
||||
typedef int Status;
|
||||
typedef char TElemType;
|
||||
|
||||
// 二叉树结构
|
||||
typedef struct BiTNode
|
||||
{
|
||||
TElemType data;
|
||||
struct BiTNode *lchild, *rchild;
|
||||
}BiTNode, *BiTree;
|
||||
|
||||
void InitBiTree(BiTree &T); //创建一颗空二叉树
|
||||
BiTree MakeBiTree(TElemType e, BiTree L, BiTree R); //创建一颗二叉树T,其中根节点的值为e,L和R分别作为左子树和右子树
|
||||
void DestroyBiTree(BiTree &T); //销毁二叉树
|
||||
Status BiTreeEmpty(BiTree T); //对二叉树判空。若为空返回TRUE,否则FALSE
|
||||
Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R); //将一颗二叉树T分解成根、左子树、右子树三部分
|
||||
Status ReplaceLeft(BiTree &T, BiTree <); //替换左子树。若T非空,则用LT替换T的左子树,并用LT返回T的原有左子树
|
||||
Status ReplaceRight(BiTree &T, BiTree &RT); //替换右子树。若T非空,则用RT替换T的右子树,并用RT返回T的原有右子树
|
||||
|
||||
int Leaves(BiTree T);
|
||||
int Depth(BiTree T);
|
||||
|
||||
Status visit(TElemType e);
|
||||
void UnionBiTree(BiTree &Ttemp);
|
||||
|
||||
|
||||
//InitBiTree空二叉树是只有一个BiTree指针?还是有一个结点但结点域为空?
|
||||
// 初始化一个空树
|
||||
void InitBiTree(BiTree &T)
|
||||
{
|
||||
T = NULL;
|
||||
}
|
||||
|
||||
// 构建二叉树
|
||||
BiTree MakeBiTree(TElemType e, BiTree L, BiTree R)
|
||||
{
|
||||
BiTree t;
|
||||
@@ -55,34 +41,30 @@ BiTree MakeBiTree(TElemType e, BiTree L, BiTree R)
|
||||
return t;
|
||||
}
|
||||
|
||||
// 访问结点
|
||||
Status visit(TElemType e)
|
||||
{
|
||||
printf("%c", e);
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
||||
int Leaves(BiTree T) //对二叉树T求叶子结点数目
|
||||
// 对二叉树T求叶子结点数目
|
||||
int Leaves(BiTree T)
|
||||
{
|
||||
int l = 0, r = 0;
|
||||
|
||||
if (NULL == T) return 0;
|
||||
if (NULL == T->lchild && NULL == T->rchild) return 1;
|
||||
|
||||
//问题分解,2个子问题
|
||||
|
||||
|
||||
// 求左子树叶子数目
|
||||
l = Leaves(T->lchild);
|
||||
|
||||
// 求右子树叶子数目
|
||||
r = Leaves(T->rchild);
|
||||
|
||||
// 组合
|
||||
return r + l;
|
||||
}
|
||||
|
||||
int depTraverse(BiTree T) //层次遍历:dep是个全局变量,高度
|
||||
// 层次遍历:dep是个全局变量,高度
|
||||
int depTraverse(BiTree T)
|
||||
{
|
||||
if (NULL == T) return ERROR;
|
||||
|
||||
@@ -91,20 +73,19 @@ int depTraverse(BiTree T) //层次遍历:dep是个全局变量,高度
|
||||
return dep + 1;
|
||||
}
|
||||
|
||||
|
||||
void levTraverse(BiTree T, Status(*visit)(TElemType e), int lev) //高度遍历:lev是局部变量,层次
|
||||
// 高度遍历:lev是局部变量,层次
|
||||
void levTraverse(BiTree T, Status(*visit)(TElemType e), int lev)
|
||||
{
|
||||
if (NULL == T) return;
|
||||
|
||||
visit(T->data);
|
||||
printf("的层次是%d\n", lev);
|
||||
|
||||
levTraverse(T->lchild, visit, ++lev);
|
||||
levTraverse(T->rchild, visit, lev);
|
||||
|
||||
}
|
||||
|
||||
void InOrderTraverse(BiTree T, Status(*visit)(TElemType e), int &num) //num是个全局变量
|
||||
// num是个全局变量
|
||||
void InOrderTraverse(BiTree T, Status(*visit)(TElemType e), int &num)
|
||||
{
|
||||
if (NULL == T) return;
|
||||
visit(T->data);
|
||||
@@ -115,12 +96,14 @@ void InOrderTraverse(BiTree T, Status(*visit)(TElemType e), int &num) //num是
|
||||
InOrderTraverse(T->rchild, visit, num);
|
||||
}
|
||||
|
||||
// 二叉树判空
|
||||
Status BiTreeEmpty(BiTree T)
|
||||
{
|
||||
if (NULL == T) return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// 打断二叉树:置空二叉树的左右子树
|
||||
Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R)
|
||||
{
|
||||
if (NULL == T) return ERROR;
|
||||
@@ -131,6 +114,7 @@ Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R)
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 替换左子树
|
||||
Status ReplaceLeft(BiTree &T, BiTree <)
|
||||
{
|
||||
BiTree temp;
|
||||
@@ -141,6 +125,7 @@ Status ReplaceLeft(BiTree &T, BiTree <)
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 替换右子树
|
||||
Status ReplaceRight(BiTree &T, BiTree &RT)
|
||||
{
|
||||
BiTree temp;
|
||||
@@ -151,6 +136,7 @@ Status ReplaceRight(BiTree &T, BiTree &RT)
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 合并二叉树
|
||||
void UnionBiTree(BiTree &Ttemp)
|
||||
{
|
||||
BiTree L = NULL, R = NULL;
|
||||
@@ -160,10 +146,8 @@ void UnionBiTree(BiTree &Ttemp)
|
||||
ReplaceRight(Ttemp, R);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
BiTree T = NULL, Ttemp = NULL;
|
||||
|
||||
InitBiTree(T);
|
||||
@@ -178,7 +162,6 @@ int main()
|
||||
Ttemp = T->lchild;
|
||||
UnionBiTree(Ttemp);
|
||||
|
||||
|
||||
Status(*visit1)(TElemType);
|
||||
visit1 = visit;
|
||||
int num = 0;
|
||||
@@ -192,5 +175,6 @@ int main()
|
||||
|
||||
printf("高度是 %d\n", depTraverse(T));
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -6,14 +6,17 @@
|
||||
#define OVERFLOW -1
|
||||
#define OK 1
|
||||
#define ERROR -1
|
||||
#define MAXNUM 9999 // 用于初始化哈希表的记录 key
|
||||
|
||||
typedef int Status;
|
||||
typedef int KeyType;
|
||||
|
||||
// 哈希表中的记录类型
|
||||
typedef struct {
|
||||
KeyType key;
|
||||
}RcdType;
|
||||
|
||||
// 哈希表类型
|
||||
typedef struct {
|
||||
RcdType *rcd;
|
||||
int size;
|
||||
@@ -21,28 +24,37 @@ typedef struct{
|
||||
int *tag;
|
||||
}HashTable;
|
||||
|
||||
// 哈希表每次重建增长后的大小
|
||||
int hashsize[] = { 11, 31, 61, 127, 251, 503 };
|
||||
int index = 0;
|
||||
|
||||
// 初始哈希表
|
||||
Status InitHashTable(HashTable &H, int size) {
|
||||
int i;
|
||||
H.rcd = (RcdType *)malloc(sizeof(RcdType)*size);
|
||||
H.tag = (int *)malloc(sizeof(int)*size);
|
||||
if (NULL == H.rcd || NULL == H.tag) return OVERFLOW;
|
||||
for (i = 0; i< size; i++) H.tag[i] = 0;
|
||||
KeyType maxNum = MAXNUM;
|
||||
for (i = 0; i < size; i++) {
|
||||
H.tag[i] = 0;
|
||||
H.rcd[i].key = maxNum;
|
||||
}
|
||||
H.size = size;
|
||||
H.count = 0;
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 哈希函数:除留余数法
|
||||
int Hash(KeyType key, int m) {
|
||||
return (3 * key) % m;
|
||||
}
|
||||
|
||||
void collision(int &p, int m){ //线性探测
|
||||
// 处理哈希冲突:线性探测
|
||||
void collision(int &p, int m) {
|
||||
p = (p + 1) % m;
|
||||
}
|
||||
|
||||
// 在哈希表中查询
|
||||
Status SearchHash(HashTable H, KeyType key, int &p, int &c) {
|
||||
p = Hash(key, H.size);
|
||||
int h = p;
|
||||
@@ -53,10 +65,10 @@ Status SearchHash(HashTable H, KeyType key, int &p, int &c) {
|
||||
|
||||
if (1 == H.tag[p] && key == H.rcd[p].key) return SUCCESS;
|
||||
else return UNSUCCESS;
|
||||
|
||||
}
|
||||
|
||||
void printHash(HashTable H) //打印哈希表
|
||||
//打印哈希表
|
||||
void printHash(HashTable H)
|
||||
{
|
||||
int i;
|
||||
printf("key : ");
|
||||
@@ -69,9 +81,10 @@ void printHash(HashTable H) //打印哈希表
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
Status InsertHash(HashTable &H, KeyType key); //对函数的声明
|
||||
// 函数声明:插入哈希表
|
||||
Status InsertHash(HashTable &H, KeyType key);
|
||||
|
||||
//重构
|
||||
// 重建哈希表
|
||||
Status recreateHash(HashTable &H) {
|
||||
RcdType *orcd;
|
||||
int *otag, osize, i;
|
||||
@@ -86,8 +99,10 @@ Status recreateHash(HashTable &H){
|
||||
InsertHash(H, orcd[i].key);
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 插入哈希表
|
||||
Status InsertHash(HashTable &H, KeyType key) {
|
||||
int p, c;
|
||||
if (UNSUCCESS == SearchHash(H, key, p, c)) { //没有相同key
|
||||
@@ -103,20 +118,19 @@ Status InsertHash(HashTable &H, KeyType key){
|
||||
return UNSUCCESS;
|
||||
}
|
||||
|
||||
// 删除哈希表
|
||||
Status DeleteHash(HashTable &H, KeyType key) {
|
||||
int p, c;
|
||||
if (SUCCESS == SearchHash(H, key, p, c)) {
|
||||
//删除代码
|
||||
H.tag[p] = -1;
|
||||
H.count--;
|
||||
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
else return UNSUCCESS;
|
||||
}
|
||||
|
||||
void main()
|
||||
int main()
|
||||
{
|
||||
printf("-----哈希表-----\n");
|
||||
HashTable H;
|
||||
@@ -124,7 +138,6 @@ void main()
|
||||
int size = 11;
|
||||
KeyType array[8] = { 22, 41, 53, 46, 30, 13, 12, 67 };
|
||||
KeyType key;
|
||||
RcdType e;
|
||||
|
||||
//初始化哈希表
|
||||
printf("初始化哈希表\n");
|
||||
@@ -139,7 +152,7 @@ void main()
|
||||
}
|
||||
|
||||
//删除哈希表
|
||||
printf("删除哈希表\n");
|
||||
printf("删除哈希表中key为12的元素\n");
|
||||
int p, c;
|
||||
if (SUCCESS == DeleteHash(H, 12)) {
|
||||
printf("删除成功,此时哈希表为:\n");
|
||||
@@ -147,15 +160,18 @@ void main()
|
||||
}
|
||||
|
||||
//查询哈希表
|
||||
printf("查询哈希表\n");
|
||||
printf("查询哈希表中key为67的元素\n");
|
||||
if (SUCCESS == SearchHash(H, 67, p, c)) printf("查询成功\n");
|
||||
|
||||
//再次插入,测试哈希表的重构
|
||||
printf("再次插入,测试哈希表的重构:\n");
|
||||
//再次插入,测试哈希表的重建
|
||||
printf("再次插入,测试哈希表的重建:\n");
|
||||
KeyType array1[8] = { 27, 47, 57, 47, 37, 17, 93, 67 };
|
||||
for (i = 0; i <= 7; i++) {
|
||||
key = array1[i];
|
||||
InsertHash(H, key);
|
||||
printHash(H);
|
||||
}
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -29,18 +29,6 @@ typedef struct LNode {
|
||||
struct LNode *next;
|
||||
} LNode, *LinkList;
|
||||
|
||||
Status InitList_L(LinkList &L);
|
||||
Status DestroyList_L(LinkList &L);
|
||||
Status ClearList_L(LinkList &L);
|
||||
Status ListEmpty_L(LinkList L);
|
||||
int ListLength_L(LinkList L);
|
||||
LNode* Search_L(LinkList L, ElemType e);
|
||||
LNode* NextElem_L(LNode *p);
|
||||
Status InsertAfter_L(LNode *p, LNode *q);
|
||||
Status DeleteAfter_L(LNode *p, ElemType &e);
|
||||
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e));
|
||||
|
||||
|
||||
//创建包含n个元素的链表L,元素值存储在data数组中
|
||||
Status create(LinkList &L, ElemType *data, int n) {
|
||||
LNode *p, *q;
|
||||
@@ -96,6 +84,7 @@ Status DeQueue_LQ(LinkList &L, ElemType &e) {
|
||||
//遍历调用
|
||||
Status visit(ElemType e) {
|
||||
printf("%d\t", e);
|
||||
return OK;
|
||||
}
|
||||
|
||||
//遍历单链表
|
||||
@@ -136,6 +125,7 @@ int main() {
|
||||
DeQueue_LQ(L, e);
|
||||
printf("出链表的元素为:%d\n", e);
|
||||
printf("此时链表中元素为:\n");
|
||||
|
||||
//遍历单链表
|
||||
ListTraverse_L(L, visit);
|
||||
|
||||
@@ -144,9 +134,11 @@ int main() {
|
||||
EnQueue_LQ(L, e);
|
||||
printf("入链表的元素为:%d\n", e);
|
||||
printf("此时链表中元素为:\n");
|
||||
|
||||
//遍历单链表
|
||||
ListTraverse_L(L, visit);
|
||||
printf("\n");
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -29,18 +29,6 @@ typedef struct LNode {
|
||||
struct LNode *next;
|
||||
} LNode, *LinkList;
|
||||
|
||||
Status InitList_L(LinkList &L);
|
||||
Status DestroyList_L(LinkList &L);
|
||||
Status ClearList_L(LinkList &L);
|
||||
Status ListEmpty_L(LinkList L);
|
||||
int ListLength_L(LinkList L);
|
||||
LNode* Search_L(LinkList L, ElemType e);
|
||||
LNode* NextElem_L(LNode *p);
|
||||
Status InsertAfter_L(LNode *p, LNode *q);
|
||||
Status DeleteAfter_L(LNode *p, ElemType &e);
|
||||
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e));
|
||||
|
||||
|
||||
//创建包含n个元素的链表L,元素值存储在data数组中
|
||||
Status create(LinkList &L, ElemType *data, int n) {
|
||||
LNode *p, *q;
|
||||
@@ -150,6 +138,7 @@ int main() {
|
||||
DeQueue_LQ(L, e);
|
||||
printf("出链表的元素为:%d\n", e);
|
||||
printf("此时链表中元素为:\n");
|
||||
|
||||
//遍历单链表
|
||||
ListTraverse_L(L, visit);
|
||||
|
||||
@@ -158,9 +147,11 @@ int main() {
|
||||
EnQueue_LQ(L, e);
|
||||
printf("入链表的元素为:%d\n", e);
|
||||
printf("此时链表中元素为:\n");
|
||||
|
||||
//遍历单链表
|
||||
ListTraverse_L(L, visit);
|
||||
printf("\n");
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -121,12 +121,14 @@ private:
|
||||
return false;
|
||||
}
|
||||
return delete_child(p->leftTree, data);
|
||||
} else if(p->value < data){
|
||||
}
|
||||
else if (p->value < data) {
|
||||
if (p->rightTree == NIL) {
|
||||
return false;
|
||||
}
|
||||
return delete_child(p->rightTree, data);
|
||||
} else if(p->value == data){
|
||||
}
|
||||
else if (p->value == data) {
|
||||
if (p->rightTree == NIL) {
|
||||
delete_one_child(p);
|
||||
return true;
|
||||
@@ -136,7 +138,8 @@ private:
|
||||
delete_one_child(smallest);
|
||||
|
||||
return true;
|
||||
}else{
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -159,7 +162,8 @@ private:
|
||||
|
||||
if (p->parent->leftTree == p) {
|
||||
p->parent->leftTree = child;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p->parent->rightTree = child;
|
||||
}
|
||||
child->parent = p->parent;
|
||||
@@ -167,7 +171,8 @@ private:
|
||||
if (p->color == BLACK) {
|
||||
if (child->color == RED) {
|
||||
child->color = BLACK;
|
||||
} else
|
||||
}
|
||||
else
|
||||
delete_case(child);
|
||||
}
|
||||
|
||||
@@ -191,18 +196,21 @@ private:
|
||||
&& p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) {
|
||||
p->sibling()->color = RED;
|
||||
delete_case(p->parent);
|
||||
} else if(p->parent->color == RED && p->sibling()->color == BLACK
|
||||
}
|
||||
else if (p->parent->color == RED && p->sibling()->color == BLACK
|
||||
&& p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) {
|
||||
p->sibling()->color = RED;
|
||||
p->parent->color = BLACK;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (p->sibling()->color == BLACK) {
|
||||
if (p == p->parent->leftTree && p->sibling()->leftTree->color == RED
|
||||
&& p->sibling()->rightTree->color == BLACK) {
|
||||
p->sibling()->color = RED;
|
||||
p->sibling()->leftTree->color = BLACK;
|
||||
rotate_right(p->sibling()->leftTree);
|
||||
} else if(p == p->parent->rightTree && p->sibling()->leftTree->color == BLACK
|
||||
}
|
||||
else if (p == p->parent->rightTree && p->sibling()->leftTree->color == BLACK
|
||||
&& p->sibling()->rightTree->color == RED) {
|
||||
p->sibling()->color = RED;
|
||||
p->sibling()->rightTree->color = BLACK;
|
||||
@@ -214,7 +222,8 @@ private:
|
||||
if (p == p->parent->leftTree) {
|
||||
p->sibling()->rightTree->color = BLACK;
|
||||
rotate_left(p->sibling());
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p->sibling()->leftTree->color = BLACK;
|
||||
rotate_right(p->sibling());
|
||||
}
|
||||
@@ -233,7 +242,8 @@ private:
|
||||
p->leftTree = tmp;
|
||||
insert_case(tmp);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (p->rightTree != NIL)
|
||||
insert(p->rightTree, data);
|
||||
else {
|
||||
@@ -258,22 +268,26 @@ private:
|
||||
p->parent->color = p->uncle()->color = BLACK;
|
||||
p->grandparent()->color = RED;
|
||||
insert_case(p->grandparent());
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (p->parent->rightTree == p && p->grandparent()->leftTree == p->parent) {
|
||||
rotate_left(p);
|
||||
rotate_right(p);
|
||||
p->color = BLACK;
|
||||
p->leftTree->color = p->rightTree->color = RED;
|
||||
} else if(p->parent->leftTree == p && p->grandparent()->rightTree == p->parent) {
|
||||
}
|
||||
else if (p->parent->leftTree == p && p->grandparent()->rightTree == p->parent) {
|
||||
rotate_right(p);
|
||||
rotate_left(p);
|
||||
p->color = BLACK;
|
||||
p->leftTree->color = p->rightTree->color = RED;
|
||||
} else if(p->parent->leftTree == p && p->grandparent()->leftTree == p->parent) {
|
||||
}
|
||||
else if (p->parent->leftTree == p && p->grandparent()->leftTree == p->parent) {
|
||||
p->parent->color = BLACK;
|
||||
p->grandparent()->color = RED;
|
||||
rotate_right(p->parent);
|
||||
} else if(p->parent->rightTree == p && p->grandparent()->rightTree == p->parent) {
|
||||
}
|
||||
else if (p->parent->rightTree == p && p->grandparent()->rightTree == p->parent) {
|
||||
p->parent->color = BLACK;
|
||||
p->grandparent()->color = RED;
|
||||
rotate_left(p->parent);
|
||||
@@ -317,7 +331,8 @@ public:
|
||||
root->color = BLACK;
|
||||
root->leftTree = root->rightTree = NIL;
|
||||
root->value = x;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
insert(root, x);
|
||||
}
|
||||
}
|
||||
@@ -328,3 +343,35 @@ public:
|
||||
private:
|
||||
Node *root, *NIL;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "---【红黑树】---" << endl;
|
||||
// 创建红黑树
|
||||
bst tree;
|
||||
|
||||
// 插入元素
|
||||
tree.insert(2);
|
||||
tree.insert(9);
|
||||
tree.insert(-10);
|
||||
tree.insert(0);
|
||||
tree.insert(33);
|
||||
tree.insert(-19);
|
||||
|
||||
// 顺序打印红黑树
|
||||
cout << "插入元素后的红黑树:" << endl;
|
||||
tree.inorder();
|
||||
|
||||
// 删除元素
|
||||
tree.delete_value(2);
|
||||
|
||||
// 顺序打印红黑树
|
||||
cout << "删除元素 2 后的红黑树:" << endl;
|
||||
tree.inorder();
|
||||
|
||||
// 析构
|
||||
tree.~bst();
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -32,18 +32,6 @@ typedef struct {
|
||||
int increment;
|
||||
} SqList;
|
||||
|
||||
Status InitList_Sq(SqList &L, int size, int inc); //初始化顺序表L
|
||||
Status DestroyList_Sq(SqList &L); //销毁顺序表L
|
||||
Status ClearList_Sq(SqList &L); //将顺序表L清空
|
||||
Status ListEmpty_Sq(SqList L); //若顺序表L为空表,则返回TRUE,否则FALSE
|
||||
int ListLength_Sq(SqList L); //返回顺序表L中元素个数
|
||||
Status GetElem_Sq(SqList L, int i, ElemType &e); //用e返回顺序表L中第i个元素的值
|
||||
int Search_Sq(SqList L, ElemType e); //在顺序表L顺序查找元素e,成功时返回该元素在表中第一次出现的位置,否则返回-1
|
||||
Status ListTraverse_Sq(SqList L, Status(*visit)(ElemType e)); //遍历顺序表L,依次对每个元素调用函数visit()
|
||||
Status PutElem_Sq(SqList &L, int i, ElemType e); //将顺序表L中第i个元素赋值为e
|
||||
Status Append_Sq(SqList &L, ElemType e); //在顺序表L表尾添加元素e
|
||||
Status DeleteLast_Sq(SqList &L, ElemType &e); //删除顺序表L的表尾元素,并用参数e返回其值
|
||||
|
||||
//初始化顺序表L
|
||||
Status InitList_Sq(SqList &L, int size, int inc) {
|
||||
L.elem = (ElemType *)malloc(size * sizeof(ElemType));
|
||||
@@ -96,6 +84,7 @@ int Search_Sq(SqList L, ElemType e) {
|
||||
//遍历调用
|
||||
Status visit(ElemType e) {
|
||||
printf("%d\t", e);
|
||||
return OK;
|
||||
}
|
||||
|
||||
//遍历顺序表L,依次对每个元素调用函数visit()
|
||||
@@ -188,5 +177,6 @@ int main() {
|
||||
if (OK == DestroyList_Sq(L)) printf("销毁成功\n");
|
||||
else printf("销毁失败\n");
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -32,15 +32,6 @@ typedef struct {
|
||||
int increment;
|
||||
} SqSrack;
|
||||
|
||||
//函数声明
|
||||
Status InitStack_Sq(SqSrack &S, int size, int inc); //初始化顺序栈
|
||||
Status DestroyStack_Sq(SqSrack &S); //销毁顺序栈
|
||||
Status StackEmpty_Sq(SqSrack S); //判断S是否空,若空则返回TRUE,否则返回FALSE
|
||||
void ClearStack_Sq(SqSrack &S); //清空栈S
|
||||
Status Push_Sq(SqSrack &S, ElemType e); //元素e压入栈S
|
||||
Status Pop_Sq(SqSrack &S, ElemType &e); //栈S的栈顶元素出栈,并用e返回
|
||||
Status GetTop_Sq(SqSrack S, ElemType &e); //取栈S的栈顶元素,并用e返回
|
||||
|
||||
//初始化顺序栈
|
||||
Status InitStack_Sq(SqSrack &S, int size, int inc) {
|
||||
S.elem = (ElemType *)malloc(size * sizeof(ElemType));
|
||||
@@ -155,5 +146,6 @@ int main() {
|
||||
ClearStack_Sq(S);
|
||||
printf("已清空栈S\n");
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@@ -1649,6 +1649,8 @@ typedef struct BiTNode
|
||||
|
||||
#### 红黑树
|
||||
|
||||
[RedBlackTree.cpp](DataStructure/RedBlackTree.cpp)
|
||||
|
||||
##### 红黑树的特征是什么?
|
||||
|
||||
1. 节点是红色或黑色。
|
||||
|
||||
@@ -1662,6 +1662,8 @@ typedef struct BiTNode
|
||||
|
||||
#### 红黑树
|
||||
|
||||
[RedBlackTree.cpp](DataStructure/RedBlackTree.cpp)
|
||||
|
||||
##### 红黑树的特征是什么?
|
||||
|
||||
1. 节点是红色或黑色。
|
||||
|
||||
Reference in New Issue
Block a user