改进数据结构代码

This commit is contained in:
huihut 2019-06-04 23:18:46 +08:00
parent cd16b94024
commit 27dd18fbb8
9 changed files with 935 additions and 919 deletions

View File

@ -1,5 +1,5 @@
#include<stdio.h> #include <stdio.h>
#include<stdlib.h> #include <stdlib.h>
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0
@ -8,7 +8,6 @@
#define OVERFLOW -1 #define OVERFLOW -1
#define SUCCESS 1 #define SUCCESS 1
#define UNSUCCESS 0 #define UNSUCCESS 0
#define dataNum 5 #define dataNum 5
int i = 0; int i = 0;
int dep = 0; int dep = 0;
@ -17,180 +16,165 @@ char data[dataNum] = { 'A', 'B', 'C', 'D', 'E' };
typedef int Status; typedef int Status;
typedef char TElemType; typedef char TElemType;
// 二叉树结构
typedef struct BiTNode typedef struct BiTNode
{ {
TElemType data; TElemType data;
struct BiTNode *lchild, *rchild; struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree; }BiTNode, *BiTree;
void InitBiTree(BiTree &T); //创建一颗空二叉树 // 初始化一个空树
BiTree MakeBiTree(TElemType e, BiTree L, BiTree R); //创建一颗二叉树T其中根节点的值为eL和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 &LT); //替换左子树。若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) void InitBiTree(BiTree &T)
{ {
T = NULL; T = NULL;
} }
// 构建二叉树
BiTree MakeBiTree(TElemType e, BiTree L, BiTree R) BiTree MakeBiTree(TElemType e, BiTree L, BiTree R)
{ {
BiTree t; BiTree t;
t = (BiTree)malloc(sizeof(BiTNode)); t = (BiTree)malloc(sizeof(BiTNode));
if (NULL == t) return NULL; if (NULL == t) return NULL;
t->data = e; t->data = e;
t->lchild = L; t->lchild = L;
t->rchild = R; t->rchild = R;
return t; return t;
} }
// 访问结点
Status visit(TElemType e) Status visit(TElemType e)
{ {
printf("%c", e); printf("%c", e);
return OK; return OK;
} }
// 对二叉树T求叶子结点数目
int Leaves(BiTree T) //对二叉树T求叶子结点数目 int Leaves(BiTree T)
{ {
int l = 0, r = 0; int l = 0, r = 0;
if (NULL == T) return 0;
if (NULL == T->lchild && NULL == T->rchild) return 1;
if (NULL == T) return 0; // 求左子树叶子数目
if (NULL == T->lchild && NULL == T->rchild) return 1; l = Leaves(T->lchild);
// 求右子树叶子数目
//问题分解2个子问题 r = Leaves(T->rchild);
// 组合
return r + l;
//求左子树叶子数目
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; if (NULL == T) return ERROR;
dep = (depTraverse(T->lchild) > depTraverse(T->rchild)) ? depTraverse(T->lchild) : depTraverse(T->rchild); dep = (depTraverse(T->lchild) > depTraverse(T->rchild)) ? depTraverse(T->lchild) : depTraverse(T->rchild);
return dep + 1; return dep + 1;
} }
// 高度遍历lev是局部变量层次
void levTraverse(BiTree T, Status(*visit)(TElemType e), int lev) //高度遍历lev是局部变量层次 void levTraverse(BiTree T, Status(*visit)(TElemType e), int lev)
{ {
if (NULL == T) return; if (NULL == T) return;
visit(T->data);
visit(T->data); printf("的层次是%d\n", lev);
printf("的层次是%d\n", lev);
levTraverse(T->lchild, visit, ++lev);
levTraverse(T->rchild, visit, 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; if (NULL == T) return;
visit(T->data); visit(T->data);
if (NULL == T->lchild && NULL == T->rchild) { printf("是叶子结点"); num++; } if (NULL == T->lchild && NULL == T->rchild) { printf("是叶子结点"); num++; }
else printf("不是叶子结点"); else printf("不是叶子结点");
printf("\n"); printf("\n");
InOrderTraverse(T->lchild, visit, num); InOrderTraverse(T->lchild, visit, num);
InOrderTraverse(T->rchild, visit, num); InOrderTraverse(T->rchild, visit, num);
} }
// 二叉树判空
Status BiTreeEmpty(BiTree T) Status BiTreeEmpty(BiTree T)
{ {
if (NULL == T) return TRUE; if (NULL == T) return TRUE;
return FALSE; return FALSE;
} }
// 打断二叉树:置空二叉树的左右子树
Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R) Status BreakBiTree(BiTree &T, BiTree &L, BiTree &R)
{ {
if (NULL == T) return ERROR; if (NULL == T) return ERROR;
L = T->lchild; L = T->lchild;
R = T->rchild; R = T->rchild;
T->lchild = NULL; T->lchild = NULL;
T->rchild = NULL; T->rchild = NULL;
return OK; return OK;
} }
// 替换左子树
Status ReplaceLeft(BiTree &T, BiTree &LT) Status ReplaceLeft(BiTree &T, BiTree &LT)
{ {
BiTree temp; BiTree temp;
if (NULL == T) return ERROR; if (NULL == T) return ERROR;
temp = T->lchild; temp = T->lchild;
T->lchild = LT; T->lchild = LT;
LT = temp; LT = temp;
return OK; return OK;
} }
// 替换右子树
Status ReplaceRight(BiTree &T, BiTree &RT) Status ReplaceRight(BiTree &T, BiTree &RT)
{ {
BiTree temp; BiTree temp;
if (NULL == T) return ERROR; if (NULL == T) return ERROR;
temp = T->rchild; temp = T->rchild;
T->rchild = RT; T->rchild = RT;
RT = temp; RT = temp;
return OK; return OK;
} }
// 合并二叉树
void UnionBiTree(BiTree &Ttemp) void UnionBiTree(BiTree &Ttemp)
{ {
BiTree L = NULL, R = NULL; BiTree L = NULL, R = NULL;
L = MakeBiTree(data[i++], NULL, NULL); L = MakeBiTree(data[i++], NULL, NULL);
R = MakeBiTree(data[i++], NULL, NULL); R = MakeBiTree(data[i++], NULL, NULL);
ReplaceLeft(Ttemp, L); ReplaceLeft(Ttemp, L);
ReplaceRight(Ttemp, R); ReplaceRight(Ttemp, R);
} }
int main() int main()
{ {
BiTree T = NULL, Ttemp = NULL;
BiTree T = NULL, Ttemp = NULL; InitBiTree(T);
if (TRUE == BiTreeEmpty(T)) printf("初始化T为空\n");
else printf("初始化T不为空\n");
InitBiTree(T); T = MakeBiTree(data[i++], NULL, NULL);
if (TRUE == BiTreeEmpty(T)) printf("初始化T为空\n");
else printf("初始化T不为空\n");
T = MakeBiTree(data[i++], NULL, NULL); Ttemp = T;
UnionBiTree(Ttemp);
Ttemp = T; Ttemp = T->lchild;
UnionBiTree(Ttemp); UnionBiTree(Ttemp);
Ttemp = T->lchild; Status(*visit1)(TElemType);
UnionBiTree(Ttemp); visit1 = visit;
int num = 0;
InOrderTraverse(T, visit1, num);
printf("叶子结点是 %d\n", num);
printf("叶子结点是 %d\n", Leaves(T));
Status(*visit1)(TElemType); int lev = 1;
visit1 = visit; levTraverse(T, visit1, lev);
int num = 0;
InOrderTraverse(T, visit1, num);
printf("叶子结点是 %d\n", num);
printf("叶子结点是 %d\n", Leaves(T)); printf("高度是 %d\n", depTraverse(T));
int lev = 1; getchar();
levTraverse(T, visit1, lev); return 0;
printf("高度是 %d\n", depTraverse(T));
return 0;
} }

View File

@ -6,156 +6,172 @@
#define OVERFLOW -1 #define OVERFLOW -1
#define OK 1 #define OK 1
#define ERROR -1 #define ERROR -1
#define MAXNUM 9999 // 用于初始化哈希表的记录 key
typedef int Status; typedef int Status;
typedef int KeyType; typedef int KeyType;
typedef struct{ // 哈希表中的记录类型
KeyType key; typedef struct {
KeyType key;
}RcdType; }RcdType;
typedef struct{ // 哈希表类型
RcdType *rcd; typedef struct {
int size; RcdType *rcd;
int count; int size;
int *tag; int count;
int *tag;
}HashTable; }HashTable;
// 哈希表每次重建增长后的大小
int hashsize[] = { 11, 31, 61, 127, 251, 503 }; int hashsize[] = { 11, 31, 61, 127, 251, 503 };
int index = 0; int index = 0;
Status InitHashTable(HashTable &H, int size){ // 初始哈希表
int i; Status InitHashTable(HashTable &H, int size) {
H.rcd = (RcdType *)malloc(sizeof(RcdType)*size); int i;
H.tag = (int *)malloc(sizeof(int)*size); H.rcd = (RcdType *)malloc(sizeof(RcdType)*size);
if (NULL == H.rcd || NULL == H.tag) return OVERFLOW; H.tag = (int *)malloc(sizeof(int)*size);
for (i = 0; i< size; i++) H.tag[i] = 0; if (NULL == H.rcd || NULL == H.tag) return OVERFLOW;
H.size = size; KeyType maxNum = MAXNUM;
H.count = 0; for (i = 0; i < size; i++) {
return OK; 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; int Hash(KeyType key, int m) {
return (3 * key) % m;
} }
void collision(int &p, int m){ //线性探测 // 处理哈希冲突:线性探测
p = (p + 1) % m; void collision(int &p, int m) {
p = (p + 1) % m;
} }
// 在哈希表中查询
Status SearchHash(HashTable H, KeyType key, int &p, int &c) { Status SearchHash(HashTable H, KeyType key, int &p, int &c) {
p = Hash(key, H.size); p = Hash(key, H.size);
int h = p; int h = p;
c = 0; c = 0;
while ((1 == H.tag[p] && H.rcd[p].key != key) || -1 == H.tag[p]){ while ((1 == H.tag[p] && H.rcd[p].key != key) || -1 == H.tag[p]) {
collision(p, H.size); c++; collision(p, H.size); c++;
} }
if (1 == H.tag[p] && key == H.rcd[p].key) return SUCCESS;
else return UNSUCCESS;
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; int i;
printf("key : "); printf("key : ");
for (i = 0; i < H.size; i++) for (i = 0; i < H.size; i++)
printf("%3d ", H.rcd[i].key); printf("%3d ", H.rcd[i].key);
printf("\n"); printf("\n");
printf("tag : "); printf("tag : ");
for (i = 0; i < H.size; i++) for (i = 0; i < H.size; i++)
printf("%3d ", H.tag[i]); printf("%3d ", H.tag[i]);
printf("\n\n"); printf("\n\n");
} }
Status InsertHash(HashTable &H, KeyType key); //对函数的声明 // 函数声明:插入哈希表
Status InsertHash(HashTable &H, KeyType key);
//重构 // 重建哈希表
Status recreateHash(HashTable &H){ Status recreateHash(HashTable &H) {
RcdType *orcd; RcdType *orcd;
int *otag, osize, i; int *otag, osize, i;
orcd = H.rcd; orcd = H.rcd;
otag = H.tag; otag = H.tag;
osize = H.size; osize = H.size;
InitHashTable(H, hashsize[index++]); InitHashTable(H, hashsize[index++]);
//把所有元素,按照新哈希函数放到新表中 //把所有元素,按照新哈希函数放到新表中
for (i = 0; i < osize; i++){ for (i = 0; i < osize; i++) {
if (1 == otag[i]){ if (1 == otag[i]) {
InsertHash(H, orcd[i].key); InsertHash(H, orcd[i].key);
} }
} }
return OK;
} }
Status InsertHash(HashTable &H, KeyType key){ // 插入哈希表
int p, c; Status InsertHash(HashTable &H, KeyType key) {
if (UNSUCCESS == SearchHash(H, key, p, c)){ //没有相同key int p, c;
if (c*1.0 / H.size < 0.5){ //冲突次数未达到上线 if (UNSUCCESS == SearchHash(H, key, p, c)) { //没有相同key
//插入代码 if (c*1.0 / H.size < 0.5) { //冲突次数未达到上线
H.rcd[p].key = key; //插入代码
H.tag[p] = 1; H.rcd[p].key = key;
H.count++; H.tag[p] = 1;
return SUCCESS; H.count++;
} return SUCCESS;
else recreateHash(H); //重构哈希表 }
} else recreateHash(H); //重构哈希表
return UNSUCCESS; }
return UNSUCCESS;
} }
Status DeleteHash(HashTable &H, KeyType key){ // 删除哈希表
int p, c; Status DeleteHash(HashTable &H, KeyType key) {
if (SUCCESS == SearchHash(H, key, p, c)){ int p, c;
//删除代码 if (SUCCESS == SearchHash(H, key, p, c)) {
H.tag[p] = -1; //删除代码
H.count--; H.tag[p] = -1;
H.count--;
return SUCCESS;
return SUCCESS; }
} else return UNSUCCESS;
else return UNSUCCESS;
} }
void main() int main()
{ {
printf("-----哈希表-----\n"); printf("-----哈希表-----\n");
HashTable H; HashTable H;
int i; int i;
int size = 11; int size = 11;
KeyType array[8] = { 22, 41, 53, 46, 30, 13, 12, 67 }; KeyType array[8] = { 22, 41, 53, 46, 30, 13, 12, 67 };
KeyType key; KeyType key;
RcdType e;
//初始化哈希表 //初始化哈希表
printf("初始化哈希表\n"); printf("初始化哈希表\n");
if (SUCCESS == InitHashTable(H, hashsize[index++])) printf("初始化成功\n"); if (SUCCESS == InitHashTable(H, hashsize[index++])) printf("初始化成功\n");
//插入哈希表 //插入哈希表
printf("插入哈希表\n"); printf("插入哈希表\n");
for (i = 0; i <= 7; i++){ for (i = 0; i <= 7; i++) {
key = array[i]; key = array[i];
InsertHash(H, key); InsertHash(H, key);
printHash(H); printHash(H);
} }
//删除哈希表 //删除哈希表
printf("删除哈希表\n"); printf("删除哈希表中key为12的元素\n");
int p, c; int p, c;
if (SUCCESS == DeleteHash(H, 12)) { if (SUCCESS == DeleteHash(H, 12)) {
printf("删除成功,此时哈希表为:\n"); printf("删除成功,此时哈希表为:\n");
printHash(H); printHash(H);
} }
//查询哈希表 //查询哈希表
printf("查询哈希表\n"); printf("查询哈希表中key为67的元素\n");
if (SUCCESS == SearchHash(H, 67, p, c)) printf("查询成功\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 }; KeyType array1[8] = { 27, 47, 57, 47, 37, 17, 93, 67 };
for (i = 0; i <= 7; i++){ for (i = 0; i <= 7; i++) {
key = array1[i]; key = array1[i];
InsertHash(H, key); InsertHash(H, key);
printHash(H); printHash(H);
} }
getchar();
return 0;
} }

View File

@ -25,128 +25,120 @@ typedef int ElemType;
//链表的类型 //链表的类型
typedef struct LNode { typedef struct LNode {
ElemType data; ElemType data;
struct LNode *next; struct LNode *next;
} LNode, *LinkList; } 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数组中 //创建包含n个元素的链表L元素值存储在data数组中
Status create(LinkList &L, ElemType *data, int n) { Status create(LinkList &L, ElemType *data, int n) {
LNode *p, *q; LNode *p, *q;
int i; int i;
if (n < 0) return ERROR; if (n < 0) return ERROR;
L = NULL; L = NULL;
p = L; p = L;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
q = (LNode *)malloc(sizeof(LNode)); q = (LNode *)malloc(sizeof(LNode));
if (NULL == q) return OVERFLOW; if (NULL == q) return OVERFLOW;
q->data = data[i]; q->data = data[i];
q->next = NULL; q->next = NULL;
if (NULL == p) L = q; if (NULL == p) L = q;
else p->next = q; else p->next = q;
p = q; p = q;
} }
return OK; return OK;
} }
//e从链表末尾入链表 //e从链表末尾入链表
Status EnQueue_LQ(LinkList &L, ElemType &e) { Status EnQueue_LQ(LinkList &L, ElemType &e) {
LinkList p, q; LinkList p, q;
if (NULL == (q = (LNode *)malloc(sizeof(LNode)))) return OVERFLOW; if (NULL == (q = (LNode *)malloc(sizeof(LNode)))) return OVERFLOW;
q->data = e; q->data = e;
q->next = NULL; q->next = NULL;
if (NULL == L) L = q; if (NULL == L) L = q;
else else
{ {
p = L; p = L;
while (p->next != NULL) while (p->next != NULL)
{ {
p = p->next; p = p->next;
} }
p->next = q; p->next = q;
} }
return OK; return OK;
} }
//从链表头节点出链表到e //从链表头节点出链表到e
Status DeQueue_LQ(LinkList &L, ElemType &e) { Status DeQueue_LQ(LinkList &L, ElemType &e) {
if (NULL == L) return ERROR; if (NULL == L) return ERROR;
LinkList p; LinkList p;
p = L; p = L;
e = p->data; e = p->data;
L = L->next; L = L->next;
free(p); free(p);
return OK; return OK;
} }
//遍历调用 //遍历调用
Status visit(ElemType e) { Status visit(ElemType e) {
printf("%d\t", e); printf("%d\t", e);
return OK;
} }
//遍历单链表 //遍历单链表
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e)) void ListTraverse_L(LinkList L, Status(*visit)(ElemType e))
{ {
if (NULL == L) return; if (NULL == L) return;
for (LinkList p = L; NULL != p; p = p -> next) { for (LinkList p = L; NULL != p; p = p->next) {
visit(p -> data); visit(p->data);
} }
} }
int main() { int main() {
int i; int i;
ElemType e, data[LONGTH] = { 1, 2, 3, 4, 5 }; ElemType e, data[LONGTH] = { 1, 2, 3, 4, 5 };
LinkList L; LinkList L;
//显示测试值 //显示测试值
printf("---【单链表】---\n"); printf("---【单链表】---\n");
printf("待测试元素为:\n"); printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) printf("%d\t", data[i]); for (i = 0; i < LONGTH; i++) printf("%d\t", data[i]);
printf("\n"); printf("\n");
//创建链表L //创建链表L
printf("创建链表L\n"); printf("创建链表L\n");
if (ERROR == create(L, data, LONGTH)) if (ERROR == create(L, data, LONGTH))
{ {
printf("创建链表L失败\n"); printf("创建链表L失败\n");
return -1; return -1;
} }
printf("成功创建包含%d个元素的链表L\n元素值存储在data数组中\n", LONGTH); printf("成功创建包含%d个元素的链表L\n元素值存储在data数组中\n", LONGTH);
//遍历单链表 //遍历单链表
printf("此时链表中元素为:\n"); printf("此时链表中元素为:\n");
ListTraverse_L(L, visit); ListTraverse_L(L, visit);
//从链表头节点出链表到e //从链表头节点出链表到e
printf("\n出链表到e\n"); printf("\n出链表到e\n");
DeQueue_LQ(L, e); DeQueue_LQ(L, e);
printf("出链表的元素为:%d\n", e); printf("出链表的元素为:%d\n", e);
printf("此时链表中元素为:\n"); printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
//e从链表末尾入链表 //遍历单链表
printf("\ne入链表\n"); ListTraverse_L(L, visit);
EnQueue_LQ(L, e);
printf("入链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
printf("\n");
return 0; //e从链表末尾入链表
printf("\ne入链表\n");
EnQueue_LQ(L, e);
printf("入链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
printf("\n");
getchar();
return 0;
} }

View File

@ -25,142 +25,133 @@ typedef int ElemType;
//链表的类型 //链表的类型
typedef struct LNode { typedef struct LNode {
ElemType data; ElemType data;
struct LNode *next; struct LNode *next;
} LNode, *LinkList; } 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数组中 //创建包含n个元素的链表L元素值存储在data数组中
Status create(LinkList &L, ElemType *data, int n) { Status create(LinkList &L, ElemType *data, int n) {
LNode *p, *q; LNode *p, *q;
int i; int i;
if (n < 0) return ERROR; if (n < 0) return ERROR;
p = L = NULL; p = L = NULL;
q = (LNode *)malloc(sizeof(LNode)); q = (LNode *)malloc(sizeof(LNode));
if (NULL == q) return OVERFLOW; if (NULL == q) return OVERFLOW;
q->next = NULL; q->next = NULL;
p = L = q; p = L = q;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
q = (LNode *)malloc(sizeof(LNode)); q = (LNode *)malloc(sizeof(LNode));
if (NULL == q) return OVERFLOW; if (NULL == q) return OVERFLOW;
q->data = data[i]; q->data = data[i];
q->next = NULL; q->next = NULL;
p->next = q; p->next = q;
p = q; p = q;
} }
return OK; return OK;
} }
//e从链表末尾入链表 //e从链表末尾入链表
Status EnQueue_LQ(LinkList &L, ElemType &e) { Status EnQueue_LQ(LinkList &L, ElemType &e) {
LinkList p, q; LinkList p, q;
if (NULL == (q = (LNode *)malloc(sizeof(LNode)))) return OVERFLOW; if (NULL == (q = (LNode *)malloc(sizeof(LNode)))) return OVERFLOW;
q->data = e; q->data = e;
q->next = NULL; q->next = NULL;
if (NULL == L) if (NULL == L)
{ {
L = (LNode *)malloc(sizeof(LNode)); L = (LNode *)malloc(sizeof(LNode));
if (NULL == L) return OVERFLOW; if (NULL == L) return OVERFLOW;
L -> next = q; L->next = q;
} }
else if (NULL == L->next) else if (NULL == L->next)
{ {
L -> next = q; L->next = q;
} }
else else
{ {
p = L; p = L;
while (p->next != NULL) while (p->next != NULL)
{ {
p = p->next; p = p->next;
} }
p->next = q; p->next = q;
} }
return OK; return OK;
} }
//从链表头节点出链表到e //从链表头节点出链表到e
Status DeQueue_LQ(LinkList &L, ElemType &e) { Status DeQueue_LQ(LinkList &L, ElemType &e) {
if (NULL == L || NULL == L->next) return ERROR; if (NULL == L || NULL == L->next) return ERROR;
LinkList p; LinkList p;
p = L->next; p = L->next;
e = p->data; e = p->data;
L->next = p->next; L->next = p->next;
free(p); free(p);
return OK; return OK;
} }
//遍历调用 //遍历调用
Status visit(ElemType e) { Status visit(ElemType e) {
printf("%d\t", e); printf("%d\t", e);
return OK; return OK;
} }
//遍历单链表 //遍历单链表
void ListTraverse_L(LinkList L, Status(*visit)(ElemType e)) void ListTraverse_L(LinkList L, Status(*visit)(ElemType e))
{ {
if (NULL == L || NULL == L->next) return; if (NULL == L || NULL == L->next) return;
for (LinkList p = L -> next; NULL != p; p = p -> next) { for (LinkList p = L->next; NULL != p; p = p->next) {
visit(p -> data); visit(p->data);
} }
} }
int main() { int main() {
int i; int i;
ElemType e, data[LONGTH] = { 1, 2, 3, 4, 5 }; ElemType e, data[LONGTH] = { 1, 2, 3, 4, 5 };
LinkList L; LinkList L;
//显示测试值 //显示测试值
printf("---【有头结点的单链表】---\n"); printf("---【有头结点的单链表】---\n");
printf("待测试元素为:\n"); printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) printf("%d\t", data[i]); for (i = 0; i < LONGTH; i++) printf("%d\t", data[i]);
printf("\n"); printf("\n");
//创建链表L //创建链表L
printf("创建链表L\n"); printf("创建链表L\n");
if (ERROR == create(L, data, LONGTH)) if (ERROR == create(L, data, LONGTH))
{ {
printf("创建链表L失败\n"); printf("创建链表L失败\n");
return -1; return -1;
} }
printf("成功创建包含1个头结点、%d个元素的链表L\n元素值存储在data数组中\n", LONGTH); printf("成功创建包含1个头结点、%d个元素的链表L\n元素值存储在data数组中\n", LONGTH);
//遍历单链表 //遍历单链表
printf("此时链表中元素为:\n"); printf("此时链表中元素为:\n");
ListTraverse_L(L, visit); ListTraverse_L(L, visit);
//从链表头节点出链表到e //从链表头节点出链表到e
printf("\n出链表到e\n"); printf("\n出链表到e\n");
DeQueue_LQ(L, e); DeQueue_LQ(L, e);
printf("出链表的元素为:%d\n", e); printf("出链表的元素为:%d\n", e);
printf("此时链表中元素为:\n"); printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
//e从链表末尾入链表 //遍历单链表
printf("\ne入链表\n"); ListTraverse_L(L, visit);
EnQueue_LQ(L, e);
printf("入链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
printf("\n");
return 0; //e从链表末尾入链表
printf("\ne入链表\n");
EnQueue_LQ(L, e);
printf("入链表的元素为:%d\n", e);
printf("此时链表中元素为:\n");
//遍历单链表
ListTraverse_L(L, visit);
printf("\n");
getchar();
return 0;
} }

View File

@ -7,324 +7,371 @@ using namespace std;
class bst { class bst {
private: private:
struct Node { struct Node {
int value; int value;
bool color; bool color;
Node *leftTree, *rightTree, *parent; Node *leftTree, *rightTree, *parent;
Node() : value(0), color(RED), leftTree(NULL), rightTree(NULL), parent(NULL) { } Node() : value(0), color(RED), leftTree(NULL), rightTree(NULL), parent(NULL) { }
Node* grandparent() { Node* grandparent() {
if(parent == NULL){ if (parent == NULL) {
return NULL; return NULL;
} }
return parent->parent; return parent->parent;
} }
Node* uncle() { Node* uncle() {
if(grandparent() == NULL) { if (grandparent() == NULL) {
return NULL; return NULL;
} }
if(parent == grandparent()->rightTree) if (parent == grandparent()->rightTree)
return grandparent()->leftTree; return grandparent()->leftTree;
else else
return grandparent()->rightTree; return grandparent()->rightTree;
} }
Node* sibling() { Node* sibling() {
if(parent->leftTree == this) if (parent->leftTree == this)
return parent->rightTree; return parent->rightTree;
else else
return parent->leftTree; return parent->leftTree;
} }
}; };
void rotate_right(Node *p){ void rotate_right(Node *p) {
Node *gp = p->grandparent(); Node *gp = p->grandparent();
Node *fa = p->parent; Node *fa = p->parent;
Node *y = p->rightTree; Node *y = p->rightTree;
fa->leftTree = y; fa->leftTree = y;
if(y != NIL) if (y != NIL)
y->parent = fa; y->parent = fa;
p->rightTree = fa; p->rightTree = fa;
fa->parent = p; fa->parent = p;
if(root == fa) if (root == fa)
root = p; root = p;
p->parent = gp; p->parent = gp;
if(gp != NULL){ if (gp != NULL) {
if(gp->leftTree == fa) if (gp->leftTree == fa)
gp->leftTree = p; gp->leftTree = p;
else else
gp->rightTree = p; gp->rightTree = p;
} }
} }
void rotate_left(Node *p){ void rotate_left(Node *p) {
if(p->parent == NULL){ if (p->parent == NULL) {
root = p; root = p;
return; return;
} }
Node *gp = p->grandparent(); Node *gp = p->grandparent();
Node *fa = p->parent; Node *fa = p->parent;
Node *y = p->leftTree; Node *y = p->leftTree;
fa->rightTree = y; fa->rightTree = y;
if(y != NIL) if (y != NIL)
y->parent = fa; y->parent = fa;
p->leftTree = fa; p->leftTree = fa;
fa->parent = p; fa->parent = p;
if(root == fa) if (root == fa)
root = p; root = p;
p->parent = gp; p->parent = gp;
if(gp != NULL){ if (gp != NULL) {
if(gp->leftTree == fa) if (gp->leftTree == fa)
gp->leftTree = p; gp->leftTree = p;
else else
gp->rightTree = p; gp->rightTree = p;
} }
} }
void inorder(Node *p){ void inorder(Node *p) {
if(p == NIL) if (p == NIL)
return; return;
if(p->leftTree) if (p->leftTree)
inorder(p->leftTree); inorder(p->leftTree);
cout << p->value << " "; cout << p->value << " ";
if(p->rightTree) if (p->rightTree)
inorder(p->rightTree); inorder(p->rightTree);
} }
string outputColor (bool color) { string outputColor(bool color) {
return color ? "BLACK" : "RED"; return color ? "BLACK" : "RED";
} }
Node* getSmallestChild(Node *p){ Node* getSmallestChild(Node *p) {
if(p->leftTree == NIL) if (p->leftTree == NIL)
return p; return p;
return getSmallestChild(p->leftTree); return getSmallestChild(p->leftTree);
} }
bool delete_child(Node *p, int data){ bool delete_child(Node *p, int data) {
if(p->value > data){ if (p->value > data) {
if(p->leftTree == NIL){ if (p->leftTree == NIL) {
return false; return false;
} }
return delete_child(p->leftTree, data); return delete_child(p->leftTree, data);
} else if(p->value < data){ }
if(p->rightTree == NIL){ else if (p->value < data) {
return false; if (p->rightTree == NIL) {
} return false;
return delete_child(p->rightTree, data); }
} else if(p->value == data){ return delete_child(p->rightTree, data);
if(p->rightTree == NIL){ }
delete_one_child (p); else if (p->value == data) {
return true; if (p->rightTree == NIL) {
} delete_one_child(p);
Node *smallest = getSmallestChild(p->rightTree); return true;
swap(p->value, smallest->value); }
delete_one_child (smallest); Node *smallest = getSmallestChild(p->rightTree);
swap(p->value, smallest->value);
delete_one_child(smallest);
return true; return true;
}else{ }
return false; else {
} return false;
} }
}
void delete_one_child(Node *p){ void delete_one_child(Node *p) {
Node *child = p->leftTree == NIL ? p->rightTree : p->leftTree; Node *child = p->leftTree == NIL ? p->rightTree : p->leftTree;
if(p->parent == NULL && p->leftTree == NIL && p->rightTree == NIL){ if (p->parent == NULL && p->leftTree == NIL && p->rightTree == NIL) {
p = NULL; p = NULL;
root = p; root = p;
return; return;
} }
if(p->parent == NULL){ if (p->parent == NULL) {
delete p; delete p;
child->parent = NULL; child->parent = NULL;
root = child; root = child;
root->color = BLACK; root->color = BLACK;
return; return;
} }
if(p->parent->leftTree == p){ if (p->parent->leftTree == p) {
p->parent->leftTree = child; p->parent->leftTree = child;
} else { }
p->parent->rightTree = child; else {
} p->parent->rightTree = child;
child->parent = p->parent; }
child->parent = p->parent;
if(p->color == BLACK){ if (p->color == BLACK) {
if(child->color == RED){ if (child->color == RED) {
child->color = BLACK; child->color = BLACK;
} else }
delete_case (child); else
} delete_case(child);
}
delete p; delete p;
} }
void delete_case(Node *p){ void delete_case(Node *p) {
if(p->parent == NULL){ if (p->parent == NULL) {
p->color = BLACK; p->color = BLACK;
return; return;
} }
if(p->sibling()->color == RED) { if (p->sibling()->color == RED) {
p->parent->color = RED; p->parent->color = RED;
p->sibling()->color = BLACK; p->sibling()->color = BLACK;
if(p == p->parent->leftTree) if (p == p->parent->leftTree)
rotate_left(p->sibling()); rotate_left(p->sibling());
else else
rotate_right(p->sibling()); rotate_right(p->sibling());
} }
if(p->parent->color == BLACK && p->sibling()->color == BLACK if (p->parent->color == BLACK && p->sibling()->color == BLACK
&& p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) { && p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) {
p->sibling()->color = RED; p->sibling()->color = RED;
delete_case(p->parent); delete_case(p->parent);
} else if(p->parent->color == RED && p->sibling()->color == BLACK }
&& p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) { else if (p->parent->color == RED && p->sibling()->color == BLACK
p->sibling()->color = RED; && p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) {
p->parent->color = BLACK; p->sibling()->color = RED;
} else { p->parent->color = BLACK;
if(p->sibling()->color == BLACK) { }
if(p == p->parent->leftTree && p->sibling()->leftTree->color == RED else {
&& p->sibling()->rightTree->color == BLACK) { if (p->sibling()->color == BLACK) {
p->sibling()->color = RED; if (p == p->parent->leftTree && p->sibling()->leftTree->color == RED
p->sibling()->leftTree->color = BLACK; && p->sibling()->rightTree->color == BLACK) {
rotate_right(p->sibling()->leftTree); p->sibling()->color = RED;
} else if(p == p->parent->rightTree && p->sibling()->leftTree->color == BLACK p->sibling()->leftTree->color = BLACK;
&& p->sibling()->rightTree->color == RED) { rotate_right(p->sibling()->leftTree);
p->sibling()->color = RED; }
p->sibling()->rightTree->color = BLACK; else if (p == p->parent->rightTree && p->sibling()->leftTree->color == BLACK
rotate_left(p->sibling()->rightTree); && p->sibling()->rightTree->color == RED) {
} p->sibling()->color = RED;
} p->sibling()->rightTree->color = BLACK;
p->sibling()->color = p->parent->color; rotate_left(p->sibling()->rightTree);
p->parent->color = BLACK; }
if(p == p->parent->leftTree){ }
p->sibling()->rightTree->color = BLACK; p->sibling()->color = p->parent->color;
rotate_left(p->sibling()); p->parent->color = BLACK;
} else { if (p == p->parent->leftTree) {
p->sibling()->leftTree->color = BLACK; p->sibling()->rightTree->color = BLACK;
rotate_right(p->sibling()); rotate_left(p->sibling());
} }
} else {
} p->sibling()->leftTree->color = BLACK;
rotate_right(p->sibling());
}
}
}
void insert(Node *p, int data){ void insert(Node *p, int data) {
if(p->value >= data){ if (p->value >= data) {
if(p->leftTree != NIL) if (p->leftTree != NIL)
insert(p->leftTree, data); insert(p->leftTree, data);
else { else {
Node *tmp = new Node(); Node *tmp = new Node();
tmp->value = data; tmp->value = data;
tmp->leftTree = tmp->rightTree = NIL; tmp->leftTree = tmp->rightTree = NIL;
tmp->parent = p; tmp->parent = p;
p->leftTree = tmp; p->leftTree = tmp;
insert_case (tmp); insert_case(tmp);
} }
} else { }
if(p->rightTree != NIL) else {
insert(p->rightTree, data); if (p->rightTree != NIL)
else { insert(p->rightTree, data);
Node *tmp = new Node(); else {
tmp->value = data; Node *tmp = new Node();
tmp->leftTree = tmp->rightTree = NIL; tmp->value = data;
tmp->parent = p; tmp->leftTree = tmp->rightTree = NIL;
p->rightTree = tmp; tmp->parent = p;
insert_case (tmp); p->rightTree = tmp;
} insert_case(tmp);
} }
} }
}
void insert_case(Node *p){ void insert_case(Node *p) {
if(p->parent == NULL){ if (p->parent == NULL) {
root = p; root = p;
p->color = BLACK; p->color = BLACK;
return; return;
} }
if(p->parent->color == RED){ if (p->parent->color == RED) {
if(p->uncle()->color == RED) { if (p->uncle()->color == RED) {
p->parent->color = p->uncle()->color = BLACK; p->parent->color = p->uncle()->color = BLACK;
p->grandparent()->color = RED; p->grandparent()->color = RED;
insert_case(p->grandparent()); insert_case(p->grandparent());
} else { }
if(p->parent->rightTree == p && p->grandparent()->leftTree == p->parent) { else {
rotate_left (p); if (p->parent->rightTree == p && p->grandparent()->leftTree == p->parent) {
rotate_right (p); rotate_left(p);
p->color = BLACK; rotate_right(p);
p->leftTree->color = p->rightTree->color = RED; p->color = BLACK;
} else if(p->parent->leftTree == p && p->grandparent()->rightTree == p->parent) { p->leftTree->color = p->rightTree->color = RED;
rotate_right (p); }
rotate_left (p); else if (p->parent->leftTree == p && p->grandparent()->rightTree == p->parent) {
p->color = BLACK; rotate_right(p);
p->leftTree->color = p->rightTree->color = RED; rotate_left(p);
} else if(p->parent->leftTree == p && p->grandparent()->leftTree == p->parent) { p->color = BLACK;
p->parent->color = BLACK; p->leftTree->color = p->rightTree->color = RED;
p->grandparent()->color = RED; }
rotate_right(p->parent); else if (p->parent->leftTree == p && p->grandparent()->leftTree == p->parent) {
} else if(p->parent->rightTree == p && p->grandparent()->rightTree == p->parent) { p->parent->color = BLACK;
p->parent->color = BLACK; p->grandparent()->color = RED;
p->grandparent()->color = RED; rotate_right(p->parent);
rotate_left(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);
}
}
}
}
void DeleteTree(Node *p){ void DeleteTree(Node *p) {
if(!p || p == NIL){ if (!p || p == NIL) {
return; return;
} }
DeleteTree(p->leftTree); DeleteTree(p->leftTree);
DeleteTree(p->rightTree); DeleteTree(p->rightTree);
delete p; delete p;
} }
public: public:
bst() { bst() {
NIL = new Node(); NIL = new Node();
NIL->color = BLACK; NIL->color = BLACK;
root = NULL; root = NULL;
} }
~bst() { ~bst() {
if (root) if (root)
DeleteTree (root); DeleteTree(root);
delete NIL; delete NIL;
} }
void inorder() { void inorder() {
if(root == NULL) if (root == NULL)
return; return;
inorder (root); inorder(root);
cout << endl; cout << endl;
} }
void insert (int x) { void insert(int x) {
if(root == NULL){ if (root == NULL) {
root = new Node(); root = new Node();
root->color = BLACK; root->color = BLACK;
root->leftTree = root->rightTree = NIL; root->leftTree = root->rightTree = NIL;
root->value = x; root->value = x;
} else { }
insert(root, x); else {
} insert(root, x);
} }
}
bool delete_value (int data) { bool delete_value(int data) {
return delete_child(root, data); return delete_child(root, data);
} }
private: private:
Node *root, *NIL; 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;
}

View File

@ -26,167 +26,157 @@ typedef int ElemType;
//顺序栈的类型 //顺序栈的类型
typedef struct { typedef struct {
ElemType *elem; ElemType *elem;
int length; int length;
int size; int size;
int increment; int increment;
} SqList; } 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 //初始化顺序表L
Status InitList_Sq(SqList &L, int size, int inc) { Status InitList_Sq(SqList &L, int size, int inc) {
L.elem = (ElemType *)malloc(size * sizeof(ElemType)); L.elem = (ElemType *)malloc(size * sizeof(ElemType));
if (NULL == L.elem) return OVERFLOW; if (NULL == L.elem) return OVERFLOW;
L.length = 0; L.length = 0;
L.size = size; L.size = size;
L.increment = inc; L.increment = inc;
return OK; return OK;
} }
//销毁顺序表L //销毁顺序表L
Status DestroyList_Sq(SqList &L) { Status DestroyList_Sq(SqList &L) {
free(L.elem); free(L.elem);
L.elem = NULL; L.elem = NULL;
return OK; return OK;
} }
//将顺序表L清空 //将顺序表L清空
Status ClearList_Sq(SqList &L) { Status ClearList_Sq(SqList &L) {
if (0 != L.length) L.length = 0; if (0 != L.length) L.length = 0;
return OK; return OK;
} }
//若顺序表L为空表则返回TRUE否则FALSE //若顺序表L为空表则返回TRUE否则FALSE
Status ListEmpty_Sq(SqList L) { Status ListEmpty_Sq(SqList L) {
if (0 == L.length) return TRUE; if (0 == L.length) return TRUE;
return FALSE; return FALSE;
} }
//返回顺序表L中元素个数 //返回顺序表L中元素个数
int ListLength_Sq(SqList L) { int ListLength_Sq(SqList L) {
return L.length; return L.length;
} }
// 用e返回顺序表L中第i个元素的值 // 用e返回顺序表L中第i个元素的值
Status GetElem_Sq(SqList L, int i, ElemType &e) { Status GetElem_Sq(SqList L, int i, ElemType &e) {
e = L.elem[--i]; e = L.elem[--i];
return OK; return OK;
} }
// 在顺序表L顺序查找元素e成功时返回该元素在表中第一次出现的位置否则返回 - 1 // 在顺序表L顺序查找元素e成功时返回该元素在表中第一次出现的位置否则返回 - 1
int Search_Sq(SqList L, ElemType e) { int Search_Sq(SqList L, ElemType e) {
int i = 0; int i = 0;
while (i < L.length && L.elem[i] != e) i++; while (i < L.length && L.elem[i] != e) i++;
if (i < L.length) return i; if (i < L.length) return i;
else return -1; else return -1;
} }
//遍历调用 //遍历调用
Status visit(ElemType e) { Status visit(ElemType e) {
printf("%d\t",e); printf("%d\t", e);
return OK;
} }
//遍历顺序表L依次对每个元素调用函数visit() //遍历顺序表L依次对每个元素调用函数visit()
Status ListTraverse_Sq(SqList L, Status(*visit)(ElemType e)) { Status ListTraverse_Sq(SqList L, Status(*visit)(ElemType e)) {
if (0 == L.length) return ERROR; if (0 == L.length) return ERROR;
for (int i = 0; i < L.length; i++) { for (int i = 0; i < L.length; i++) {
visit(L.elem[i]); visit(L.elem[i]);
} }
return OK; return OK;
} }
//将顺序表L中第i个元素赋值为e //将顺序表L中第i个元素赋值为e
Status PutElem_Sq(SqList &L, int i, ElemType e) { Status PutElem_Sq(SqList &L, int i, ElemType e) {
if (i > L.length) return ERROR; if (i > L.length) return ERROR;
e = L.elem[--i]; e = L.elem[--i];
return OK; return OK;
} }
//在顺序表L表尾添加元素e //在顺序表L表尾添加元素e
Status Append_Sq(SqList &L, ElemType e) { Status Append_Sq(SqList &L, ElemType e) {
if (L.length >= L.size) return ERROR; if (L.length >= L.size) return ERROR;
L.elem[L.length] = e; L.elem[L.length] = e;
L.length++; L.length++;
return OK; return OK;
} }
//删除顺序表L的表尾元素并用参数e返回其值 //删除顺序表L的表尾元素并用参数e返回其值
Status DeleteLast_Sq(SqList &L, ElemType &e) { Status DeleteLast_Sq(SqList &L, ElemType &e) {
if (0 == L.length) return ERROR; if (0 == L.length) return ERROR;
e = L.elem[L.length - 1]; e = L.elem[L.length - 1];
L.length--; L.length--;
return OK; return OK;
} }
int main() { int main() {
//定义表L //定义表L
SqList L; SqList L;
//定义测量值 //定义测量值
int size, increment, i; int size, increment, i;
//初始化测试值 //初始化测试值
size = LONGTH; size = LONGTH;
increment = LONGTH; increment = LONGTH;
ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 }; ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 };
//显示测试值 //显示测试值
printf("---【顺序栈】---\n"); printf("---【顺序栈】---\n");
printf("表L的size为%d\n表L的increment为%d\n", size, increment); printf("表L的size为%d\n表L的increment为%d\n", size, increment);
printf("待测试元素为:\n"); printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) { for (i = 0; i < LONGTH; i++) {
printf("%d\t", eArray[i]); printf("%d\t", eArray[i]);
} }
printf("\n"); printf("\n");
//初始化顺序表 //初始化顺序表
if (!InitList_Sq(L, size, increment)) { if (!InitList_Sq(L, size, increment)) {
printf("初始化顺序表失败\n"); printf("初始化顺序表失败\n");
exit(0); exit(0);
} }
printf("已初始化顺序表\n"); printf("已初始化顺序表\n");
//判空 //判空
if(TRUE == ListEmpty_Sq(L)) printf("此表为空表\n"); if (TRUE == ListEmpty_Sq(L)) printf("此表为空表\n");
else printf("此表不是空表\n"); else printf("此表不是空表\n");
//入表 //入表
printf("将待测元素入表:\n"); printf("将待测元素入表:\n");
for (i = 0; i < LONGTH; i++) { for (i = 0; i < LONGTH; i++) {
if(ERROR == Append_Sq(L, eArray[i])) printf("入表失败\n");; if (ERROR == Append_Sq(L, eArray[i])) printf("入表失败\n");;
} }
printf("入表成功\n"); printf("入表成功\n");
//遍历顺序表L //遍历顺序表L
printf("此时表内元素为:\n"); printf("此时表内元素为:\n");
ListTraverse_Sq(L, visit); ListTraverse_Sq(L, visit);
//出表 //出表
printf("\n将表尾元素入表到e\n"); printf("\n将表尾元素入表到e\n");
if (ERROR == DeleteLast_Sq(L, e)) printf("出表失败\n"); if (ERROR == DeleteLast_Sq(L, e)) printf("出表失败\n");
printf("出表成功\n出表元素为%d\n",e); printf("出表成功\n出表元素为%d\n", e);
//遍历顺序表L //遍历顺序表L
printf("此时表内元素为:\n"); printf("此时表内元素为:\n");
ListTraverse_Sq(L, visit); ListTraverse_Sq(L, visit);
//销毁顺序表 //销毁顺序表
printf("\n销毁顺序表\n"); printf("\n销毁顺序表\n");
if(OK == DestroyList_Sq(L)) printf("销毁成功\n"); if (OK == DestroyList_Sq(L)) printf("销毁成功\n");
else printf("销毁失败\n"); else printf("销毁失败\n");
return 0; getchar();
return 0;
} }

View File

@ -26,134 +26,126 @@ typedef int ElemType;
//顺序栈的类型 //顺序栈的类型
typedef struct { typedef struct {
ElemType *elem; ElemType *elem;
int top; int top;
int size; int size;
int increment; int increment;
} SqSrack; } 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) { Status InitStack_Sq(SqSrack &S, int size, int inc) {
S.elem = (ElemType *)malloc(size * sizeof(ElemType)); S.elem = (ElemType *)malloc(size * sizeof(ElemType));
if (NULL == S.elem) return OVERFLOW; if (NULL == S.elem) return OVERFLOW;
S.top = 0; S.top = 0;
S.size = size; S.size = size;
S.increment = inc; S.increment = inc;
return OK; return OK;
} }
//销毁顺序栈 //销毁顺序栈
Status DestroyStack_Sq(SqSrack &S) { Status DestroyStack_Sq(SqSrack &S) {
free(S.elem); free(S.elem);
S.elem = NULL; S.elem = NULL;
return OK; return OK;
} }
//判断S是否空若空则返回TRUE否则返回FALSE //判断S是否空若空则返回TRUE否则返回FALSE
Status StackEmpty_Sq(SqSrack S) { Status StackEmpty_Sq(SqSrack S) {
if (0 == S.top) return TRUE; if (0 == S.top) return TRUE;
return FALSE; return FALSE;
} }
//清空栈S //清空栈S
void ClearStack_Sq(SqSrack &S) { void ClearStack_Sq(SqSrack &S) {
if (0 == S.top) return; if (0 == S.top) return;
S.size = 0; S.size = 0;
S.top = 0; S.top = 0;
} }
//元素e压入栈S //元素e压入栈S
Status Push_Sq(SqSrack &S, ElemType e) { Status Push_Sq(SqSrack &S, ElemType e) {
ElemType *newbase; ElemType *newbase;
if (S.top >= S.size) { if (S.top >= S.size) {
newbase = (ElemType *)realloc(S.elem, (S.size + S.increment) * sizeof(ElemType)); newbase = (ElemType *)realloc(S.elem, (S.size + S.increment) * sizeof(ElemType));
if (NULL == newbase) return OVERFLOW; if (NULL == newbase) return OVERFLOW;
S.elem = newbase; S.elem = newbase;
S.size += S.increment; S.size += S.increment;
} }
S.elem[S.top++] = e; S.elem[S.top++] = e;
return OK; return OK;
} }
//取栈S的栈顶元素并用e返回 //取栈S的栈顶元素并用e返回
Status GetTop_Sq(SqSrack S, ElemType &e) { Status GetTop_Sq(SqSrack S, ElemType &e) {
if (0 == S.top) return ERROR; if (0 == S.top) return ERROR;
e = S.elem[S.top - 1]; e = S.elem[S.top - 1];
return e; return e;
} }
//栈S的栈顶元素出栈并用e返回 //栈S的栈顶元素出栈并用e返回
Status Pop_Sq(SqSrack &S, ElemType &e) { Status Pop_Sq(SqSrack &S, ElemType &e) {
if (0 == S.top) return ERROR; if (0 == S.top) return ERROR;
e = S.elem[S.top - 1]; e = S.elem[S.top - 1];
S.top--; S.top--;
return e; return e;
} }
int main() { int main() {
//定义栈S //定义栈S
SqSrack S; SqSrack S;
//定义测量值 //定义测量值
int size, increment, i; int size, increment, i;
//初始化测试值 //初始化测试值
size = LONGTH; size = LONGTH;
increment = LONGTH; increment = LONGTH;
ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 }; ElemType e, eArray[LONGTH] = { 1, 2, 3, 4, 5 };
//显示测试值 //显示测试值
printf("---【顺序栈】---\n"); printf("---【顺序栈】---\n");
printf("栈S的size为%d\n栈S的increment为%d\n", size, increment); printf("栈S的size为%d\n栈S的increment为%d\n", size, increment);
printf("待测试元素为:\n"); printf("待测试元素为:\n");
for (i = 0; i < LONGTH; i++) { for (i = 0; i < LONGTH; i++) {
printf("%d\t", eArray[i]); printf("%d\t", eArray[i]);
} }
printf("\n"); printf("\n");
//初始化顺序栈 //初始化顺序栈
if (!InitStack_Sq(S, size, increment)) { if (!InitStack_Sq(S, size, increment)) {
printf("初始化顺序栈失败\n"); printf("初始化顺序栈失败\n");
exit(0); exit(0);
} }
printf("已初始化顺序栈\n"); printf("已初始化顺序栈\n");
//入栈 //入栈
for (i = 0; i < S.size; i++) { for (i = 0; i < S.size; i++) {
if (!Push_Sq(S, eArray[i])) { if (!Push_Sq(S, eArray[i])) {
printf("%d入栈失败\n", eArray[i]); printf("%d入栈失败\n", eArray[i]);
exit(0); exit(0);
} }
} }
printf("已入栈\n"); printf("已入栈\n");
//判断非空 //判断非空
if(StackEmpty_Sq(S)) printf("S栈为空\n"); if (StackEmpty_Sq(S)) printf("S栈为空\n");
else printf("S栈非空\n"); else printf("S栈非空\n");
//取栈S的栈顶元素 //取栈S的栈顶元素
printf("栈S的栈顶元素为\n"); printf("栈S的栈顶元素为\n");
printf("%d\n", GetTop_Sq(S, e)); printf("%d\n", GetTop_Sq(S, e));
//栈S元素出栈 //栈S元素出栈
printf("栈S元素出栈为\n"); printf("栈S元素出栈为\n");
for (i = 0, e = 0; i < S.size; i++) { for (i = 0, e = 0; i < S.size; i++) {
printf("%d\t", Pop_Sq(S, e)); printf("%d\t", Pop_Sq(S, e));
} }
printf("\n"); printf("\n");
//清空栈S //清空栈S
ClearStack_Sq(S); ClearStack_Sq(S);
printf("已清空栈S\n"); printf("已清空栈S\n");
return 0; getchar();
return 0;
} }

View File

@ -1649,6 +1649,8 @@ typedef struct BiTNode
#### 红黑树 #### 红黑树
[RedBlackTree.cpp](DataStructure/RedBlackTree.cpp)
##### 红黑树的特征是什么? ##### 红黑树的特征是什么?
1. 节点是红色或黑色。 1. 节点是红色或黑色。

View File

@ -1662,6 +1662,8 @@ typedef struct BiTNode
#### 红黑树 #### 红黑树
[RedBlackTree.cpp](DataStructure/RedBlackTree.cpp)
##### 红黑树的特征是什么? ##### 红黑树的特征是什么?
1. 节点是红色或黑色。 1. 节点是红色或黑色。