图
#ifndef __GRAPH_H__
#define __GRAPH_H__
#define N 5
typedef char datatype;
//定义图的结构体类型
typedef struct
{
datatype data[N];//存放结点数据
int rel[N][N];//存放关系数组
}Graph,*GraphPointer;
//创建
GraphPointer graph_create();
//添加关系
int add_rel(GraphPointer G,datatype v1,datatype v2);
//输出图
void graph_show(GraphPointer G);
//定义函数给定顶点返回该顶点的下标
int get_pos(GraphPointer G,datatype v);
#endif
#include<stdio.h>
#include<stdlib.h>
#include"graph.h"
//创建
GraphPointer graph_create()
{
GraphPointer G = (GraphPointer)malloc(sizeof(Graph));
if(NULL==G)
{
printf("创建失败\n");
return NULL;
}
//初始化数据数组
for(int i=0;i<N;i++)
{
G->data[i]='A'+i;
}
//初始化关系数组
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
G->rel[i][j]=0;
}
}
printf("创建成功\n");
return G;
}
//定义函数给定顶点返回该顶点的下标
int get_pos(GraphPointer G,datatype v)
{
for(int i=0;i<N;i++)
{
if(G->data[i]==v)
{
return i;
}
}
return -1;
}
//添加关系
int add_rel(GraphPointer G,datatype v1,datatype v2)
{
int pos1=get_pos(G,v1);
int pos2=get_pos(G,v2);
if(pos1<0||pos2<0)
{
printf("关系建立失败\n");
return -1;
}
//将对应关系设置为1
G->rel[pos1][pos2]=G->rel[pos2][pos1]=1;
printf("关系建立成功\n");
return 0;
}
//输出图
void graph_show(GraphPointer G)
{
//判断逻辑
if(NULL==G)
{
printf("输出失败\n");
return ;
}
//输出第一行
printf("\t");
for(int i=0;i<N;i++)
{
printf("%c\t",G->data[i]);
}
putchar(10);
//输出关系
for(int i=0;i<N;i++)
{
printf("%c\t",G->data[i]);
for(int j=0;j<N;j++)
{
printf("%d\t",G->rel[i][j]);
}
printf("\n");
}
printf("输出成功\n");
}
#include<stdio.h>
#include<stdlib.h>
#include"graph.h"
int main(int argc, const char *argv[])
{
GraphPointer G=graph_create();
if(NULL==G)
{
return -1;
}
//调用输出函数
graph_show(G);
//调用关系建立函数
add_rel(G,'A','B');
add_rel(G,'A','C');
add_rel(G,'A','D');
add_rel(G,'B','C');
add_rel(G,'D','E');
//调用输出函数
graph_show(G);
return 0;
}
哈希
#ifndef __HASH_H__
#define __HASH_H__
typedef int datatype;
//定义结点类型
typedef struct Node
{
datatype data;//数据域
// int index;//存放数据在原数组位置
struct Node *next;//指针域
}Node,*NodePointer;
//初始化哈希表
void hash_init(NodePointer *hash,int n);
//将元素存入哈希表中
void hash_insert(NodePointer *hash,int n,int key);
//遍历哈希表
void hash_show(NodePointer hash[],int n);
//哈希查找
int hash_search(NodePointer *hash,int n,int key);
#endif
#include<stdio.h>
#include<stdlib.h>
#include"hash.h"
//初始化哈希表
void hash_init(NodePointer *hash,int n)
{
for(int i=0;i<n;i++)
{
hash[i]=NULL;
}
printf("初始化哈希成功\n");
}
//将元素存入哈希表中
void hash_insert(NodePointer *hash,int n,int key)
{
//通过哈希函数将锁定key所在的哈希表中的位置
int pos=key%n;//key所在的应该是hash[pos]这条链表
//申请结点封装数据
NodePointer P=(NodePointer)malloc(sizeof(Node));
if(NULL==P)
{
printf("空间申请失败\n");
return ;
}
P->data=key;
P->next=NULL;
//将封装好的结点插入到hash[pos]这条链表中
P->next=hash[pos];
hash[pos]=P;
}
//遍历哈希表
void hash_show(NodePointer *hash,int n)
{
for(int i=0;i<n;i++)
{
printf("%d: ",i);
//定义遍历指针,遍历下标为i的链表
NodePointer q=hash[i];
while(q)
{
printf("%d->",q->data);
q=q->next;
}
printf("NULL\n");
}
}
//哈希查找
int hash_search(NodePointer *hash,int n,int key)
{
//根据要查找的值,确定其所在的链表
int pos=key%n;
//定义遍历指针,从当前链表的第一个结点出发
NodePointer q = hash[pos];
while(q&&q->data!=key)
{
q=q->next;
}
//对q进行判断
if(q==NULL)
{
printf("查找失败\n");
return -1;
}else
{
printf("查找成功\n");
return 0;
}
}
#include<stdio.h>
#include"hash.h"
int main(int argc, const char *argv[])
{
int arr[10]={3,5,18,55,67,22,35,29,40,88};
NodePointer hash[13];//定义哈希表,数组除以%75,的最大素数
//调用初始化哈希表
hash_init(hash,13);
//将所有元素存入哈希表中
for(int i=0;i<10;i++)
{
hash_insert(hash,13,arr[i]);
}
//调用遍历函数
hash_show(hash,13);
//调用哈希查找函数
hash_search(hash,13,22);
return 0;
}