数据结构与算法(六)查找
- 静态查找表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 20
typedef struct
{
int key;
}ElemType;
typedef struct
{
ElemType r[MAX];
int length;
}STable;
STable* initList(int n)
{
STable* st = (STable*)malloc(sizeof(STable));
if (st == NULL)
{
exit(-1);
}
for (int i = 0; i <= n; ++i)
{
st->r[i].key = 0;
}
st->length = n;
return st;//返回指针变量一定要写,如果只在初始化函数里面初始化,出了这个函数指针就已经不存在了,
//会报没有初始化局部变量的问题
}
void CreateList(STable* st, int n)
{
int i;
for (i = 1; i <= n; ++i)
{
printf("请输入第%d个元素:",i);
scanf_s("%d",&(st->r[i].key));
}
}
int SeqSearch(STable* st, int key)//顺序查找
{
//int i;要比较两次,需要改进
//for (i = st.length; st.r[i].key != key && i >= 1; --i);
//if (i > 0)
// return i;
//else
// return 0;
int i;
st->r[0].key = key;
for (i = st->length; st->r[i].key != key; --i);
return i;
}
int BinSearch(STable* st, int key)//二分查找循环版
{
int low = 1;
int high = st->length;
while (low <= high)
{
int mid = (low + high) / 2;
if (st->r[mid].key == key)
return mid;
else if (key < st->r[mid].key)
high = mid - 1;
else
low = mid + 1;
}
return 0;
}
int Bin_Search(STable* st, int low, int high,int key)//二分查找递归版
{
int mid;
if (low > high)
return 0;
mid = (low + high) / 2;
if (key == st->r[mid].key)
return mid;
else if (key < st->r[mid].key)
{
high = mid - 1;
Bin_Search(st,low,high,key);
}
else
{
low = mid + 1;
Bin_Search(st, low, high, key);
}
}
int main()
{
int n;
printf("请输入元素个数:");
scanf_s("%d", &n);
STable* st= initList(n);
CreateList(st,n);
//printf("用顺序查找法查找的结果是:%d\n", SeqSearch(st, 2));
printf("用折半查找法查找的结果是:%d\n", BinSearch(st, 6));
printf("用折半查找法(递归)查找的结果是:%d\n", Bin_Search(st, 1, n, 6));
return 0;
}
- 动态查找表
/*二叉排序树的实现:左小右大*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct
{
int key;
int otherKey;
}ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode* lchild, * rchild;
}BSTNode,*BSTree;
BSTree* initTree(int n) //补充图里面所说的用数组创造图并且释放内存
{
int i;
BSTree* node = (BSTree*)malloc(sizeof(BSTree)*n);
for (i = 0; i < n; ++i)
{
node[i] = (BSTree)malloc(sizeof(BSTNode));
if (node[i] == NULL)
{
exit(-1);
}
printf("请输入第%d个节点的元素: ",i+1);
scanf_s(" %d",&(node[i]->data.key));
}
node[4]->lchild = node[1];//此处根据图的情况写
node[4]->rchild = node[6];
node[1]->lchild = node[0];
node[1]->rchild = node[3];
node[0]->lchild = NULL;
node[0]->rchild = NULL;
node[3]->lchild = node[2];
node[3]->rchild = NULL;
node[6]->lchild = node[5];
node[6]->rchild = node[8];
node[5]->lchild = NULL;
node[5]->rchild = NULL;
node[8]->lchild = node[7];
node[8]->rchild = NULL;
node[2]->lchild = NULL;
node[2]->rchild = NULL;
node[7]->lchild = NULL;
node[7]->rchild = NULL;
return node;
}
void InsertBST(BSTree* bs, int key)
{
BSTree s;
if (*bs == NULL)
{
s = (BSTree)malloc(sizeof(BSTNode));
s->data.key = key;
s->lchild = NULL;
s->rchild = NULL;
*bs = s;
}
else
{
if (key < (*bs)->data.key)
InsertBST(&(*bs)->lchild,key);
else if(key > (*bs)->data.key)
InsertBST(&(*bs)->rchild, key);
}
}
void CreateBST(BSTree* bs)
{
int key;
*bs = NULL;
printf("请输入元素: ");
scanf_s("%d",&key);
while (key != -1)//记得输入-1用来结束
{
InsertBST(bs,key);
printf("请输入元素: ");//输入:6 15 39 58 67 76 80 88 97进行验证
scanf_s("%d",&key);
}
}
void InOrder(BSTree bs)
{
if (bs == NULL)
return;
InOrder(bs->lchild);
printf("%d\t",bs->data.key);
InOrder(bs->rchild);
}
BSTree SearchBST(BSTree bs, int key)
{
if (!bs || bs->data.key == key)
return bs;
else if (key < bs->data.key)
return SearchBST(bs->lchild, key);
else
return SearchBST(bs->rchild,key);
}
BSTree DelBST(BSTree bs, int key)
{
BSTree p, f, s, q;
p = bs;
f = NULL;
while (p)
{
if (p->data.key == key)
break;
f = p;
if (p->data.key > key)
p = p->lchild;
else
p = p->rchild;
};
if (p == NULL)
return bs;
if (p->lchild && p->rchild)
{
q = p;
s = p->lchild;
while (s->rchild)
{
q = s;
s = s->rchild;
}
p->data.key = s->data.key;
if (q != p)
q->lchild = s->rchild;
else
q->lchild = s->lchild;
free(s);
}
else
{
if (!p->rchild)
{
q = p;
p = p->lchild;
}
else
{
q = p;
p = p->rchild;
}
if (!f)
bs = p;
else
if (q == f->lchild)
f->lchild = p;
else
f->rchild = p;
free(q);
}
return bs;
}
int main()
{
/*BSTree* node = initTree(9);//此段是适应于数组建立的图的方式
InOrder(node[4]);
printf("\n");
if (!SearchBST(node[4],6))
printf("找不到\n");
else
printf("能找到\n");
free(node);
node = NULL;*/
BSTree bs;
CreateBST(&bs);
InOrder(bs);
printf("\n");
InOrder(DelBST(bs, 15));//删除操作
printf("\n");
return 0;
}
- 哈希表
/*拉链法比较实用*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 6
typedef struct node
{
int data;
struct node* next;
}NODE,*PNODE;
typedef struct elem
{
int empty;
int data;
PNODE Nextelem;
}ElemType;
ElemType hs[MAX];
void initHash(ElemType* hs)
{
for (int i = 0; i < MAX; i++)
{
hs[i].empty = 1;
hs[i].data = NULL;
hs[i].Nextelem = NULL;
}
}
int H(int key)
{
return key % 2;
}
bool CreateHash()
{
int i = 0;
int n,m,num;
printf("您要输入的数字数: ");
scanf_s("%d",&n);
while (i < n)
{
printf("请您输入第%d个数字:",i+1);
scanf_s("%d",&num);
m = H(num);
if (hs[m].empty == 1)
{
hs[m].data = num;
hs[m].empty = 0;
}
if(hs[m].empty != 1)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (pNew == NULL)
exit(-1);
pNew->data = num;
pNew->next = hs[m].Nextelem;
hs[m].Nextelem = pNew;
}
i = i + 1;
}
}
void Search(int key)
{
int i = H(key);
PNODE p;
if (hs[i].data == key)
printf("能找到,在第%d项表上\n", i + 1);
else
{
p = hs[i].Nextelem;
while (p!= NULL)
{
if (p->data == key)
{
printf("能找到,在第%d项表的链子上\n",i+1);
break;
}
if(p->data!=key)
{
p = p->next;
}
}
if(p==NULL)
printf("找不到\n");
}
return;
}
int main()
{
initHash(hs);
CreateHash();
Search(2);
Search(4);
Search(3);
}