1.图形结构
// head.c
#ifndef __HEAD_H__
#define __HEAD_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
// head.c
#include <stdio.h>
#include <stdlib.h>
#include "head.h"
GraphPointer graph_create()
{
GraphPointer G = (GraphPointer)malloc(sizeof(Graph));
if(NULL == G)
{
printf("create失败\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 add_rel(GraphPointer G,datatype v1,datatype v2)
{
int pos1 = get_pos(G,v1);
int pos2 = get_pos(G,v2);
if(NULL == G || pos1 == -1 || pos2 == -1)
{
printf("add失败\n");
return -1;
}
G->rel[pos1][pos2] = 1;
G->rel[pos2][pos1] = 1;
return 0;
}
void graph_show(GraphPointer G)
{
if(NULL == G)
{
printf("show失败\n");
return;
}
putchar('\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]);
}
putchar(10);
}
}
int get_pos(GraphPointer G,datatype v)
{
if(NULL == G)
{
printf("pos失败\n");
return -1;
}
for(int i = 0;i < N;i++)
{
if(v == G->data[i])
{
return i;
}
}
return -1;
}
// main.c
#include <stdio.h>
#include "head.h"
int main(int argc,const char *argv[])
{
GraphPointer G = graph_create();
if(NULL == G)
{
printf("create失败\n");
return -1;
}
add_rel(G,'B','C');
add_rel(G,'A','D');
graph_show(G);
return 0;
}
2.hash表
// heah.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define MAX 13
typedef struct Node
{
int data;
struct Node *next;
}Node,*NodePtr;
NodePtr *hash_create();
void hash_insert(NodePtr *hash,int e);
void hash_show(NodePtr *hash);
void hash_search(NodePtr *hash,int num);
#endif
// head.c
#include<stdio.h>
#include<stdlib.h>
#include"head.h"
NodePtr *hash_create()
{
NodePtr *hash = (NodePtr *)malloc(MAX*sizeof(NodePtr));
if(NULL == hash)
{
printf("create失败\n");
return NULL;
}
for (int i = 0; i < MAX; i++)
{
hash[i] = NULL;
}
return hash;
}
void hash_insert(NodePtr *hash,int e)
{
NodePtr N = (NodePtr)malloc(sizeof(Node));
if(NULL == N || NULL == hash)
{
printf("insert失败\n");
return;
}
N->data = e;
N->next = NULL;
int pos = e % MAX;
N->next = hash[pos];
hash[pos] = N;
}
void hash_show(NodePtr *hash)
{
if(NULL == hash)
{
printf("show失败\n");
return;
}
for (int i = 0; i < MAX; i++)
{
NodePtr p = hash[i];
printf("%d:",i);
while (p)
{
printf("\t%d",p->data);
p = p->next;
}
putchar(10);
}
}
void hash_search(NodePtr *hash,int num)
{
if(hash == NULL)
{
printf("search失败\n");
return;
}
int pos = num % MAX;
NodePtr p = hash[pos];
while (NULL != p)
{
if(p->data == num)
{
printf("可以找到\n");
return;
}
p = p->next;
}
printf("找不到\n");
}
// main.c
#include <stdio.h>
#include "head.h"
#include <stdlib.h>
int main(int argc,const char *argv[])
{
int arr[10] = {3,5,18,55,67,22,35,29,40,88};
NodePtr *hash = hash_create();
if(NULL == hash)
{
printf("创建失败\n");
return -1;
}
for (int i = 0; i < 10; i++)
{
hash_insert(hash,arr[i]);
}
hash_show(hash);
hash_search(hash,10);
return 0;
}