#include<iostream>usingnamespace std;#define DEFAULT_SIZE 16typedefstruct _ListNode
{struct _ListNode *next;//链表实现,指向下一个节点int key;//索引void*data;//数据}ListNode;typedef ListNode*List;typedef ListNode*Element;typedefstruct _HashTable //哈希表{int TableSize;//大小
List *Thelists;}HashTable;//根据key计算索引,定位Hash桶的位置intHash(int key,int TableSize){return(key%TableSize);}//初始化哈希表
HashTable *InitHash(int TableSize){int i =0;
HashTable *hTable =NULL;if(TableSize <=0){
TableSize = DEFAULT_SIZE;}//堆区申请内存
hTable =(HashTable *)malloc(sizeof(HashTable));if(NULL== hTable){printf("HashTable malloc error.\n");returnNULL;}
hTable->TableSize = TableSize;//为Hash桶分配内存空间,其为一个指针数组
hTable->Thelists =(List *)malloc(sizeof(List)*TableSize);if(NULL== hTable->Thelists){printf("HashTable malloc error.\n");free(hTable);returnNULL;}//为Hash桶对应的指针数组初始化链表节点for(i =0; i < TableSize; i++){//动态分配空间
hTable->Thelists[i]=(ListNode *)malloc(sizeof(ListNode));if(NULL== hTable->Thelists[i]){printf("HashTable malloc error.\n");free(hTable->Thelists);free(hTable);returnNULL;}else{//数据全部设为0memset(hTable->Thelists[i],0,sizeof(ListNode));}}return hTable;}//从哈希表中根据键值查找元素
Element Find(HashTable *HashTable,int key){int i =0;
List L =NULL;
Element e =NULL;//得到哈希桶的下标
i =Hash(key, HashTable->TableSize);//头节点
L = HashTable->Thelists[i];
e = L->next;//从0开始查找键值等于key的元素while(e !=NULL&& e->key != key){
e = e->next;}return e;}//哈希表插入元素,元素为键值对voidInsert(HashTable *HashTable,int key,void*value){
Element e =NULL, tmp =NULL;
List L =NULL;
e =Find(HashTable, key);if(NULL== e){
tmp =(Element)malloc(sizeof(ListNode));;if(NULL== tmp){printf("malloc error\n");return;}
L = HashTable->Thelists[Hash(key, HashTable->TableSize)];//前插法
tmp->data = value;
tmp->key = key;
tmp->next = L->next;
L->next = tmp;}else{printf("the key already exist\n");}}//哈希表删除元素,元素为键值对voidDelete(HashTable *HashTable,int key){
Element e =NULL, last =NULL;
List L =NULL;int i =Hash(key, HashTable->TableSize);
L = HashTable->Thelists[i];
last = L;
e = L->next;while(e !=NULL&& e->key != key){//last记录删除节点的前一个节点
last = e;
e = e->next;}if(e)//如果键值存在{
last->next = e->next;free(e);}}//哈希表元素中提取数据void*Retrieve(Element e){return e ? e->data :NULL;}//销毁哈希表voidDestory(HashTable *HashTable){int i =0;
List L =NULL;
Element cur =NULL, next =NULL;for(i =0; i < HashTable->TableSize; i++){
L = HashTable->Thelists[i];
cur = L->next;while(cur !=NULL){
next = cur->next;free(cur);
cur = next;}free(L);}free(HashTable->Thelists);free(HashTable);}intmain(){char*elems[]={"小王","小李","小帅"};int i =0;
HashTable *HashTable =NULL;
HashTable =InitHash(31);Insert(HashTable,1, elems[0]);Insert(HashTable,2, elems[1]);Insert(HashTable,3, elems[2]);Delete(HashTable,1);for(i =0; i <4; i++){
Element e =Find(HashTable, i);if(e){printf("%s\n",(constchar*)Retrieve(e));}else{printf("Not found [key:%d]\n", i);}}Destory(HashTable);system("pause");return0;}
图
#include<iostream>#include<queue>usingnamespace std;#define Maxize 1024typedefstruct _EdgeNode //与节点连接的边的定义{int adjvex;//邻接的顶点int weight;//权重struct _EdgeNode *next;//下一条变}EdgeNode;typedefstruct _VertexNode //顶点节点{char data;//节点数据struct _EdgeNode *first;//指向邻接第一条边}VertexNode,AdjList;typedefstruct _AdjListGraph //图{
AdjList *adjlist;int vex;//顶点数int edge;//变数}AdjListGraph;bool visited[Maxize];//全局数组,记录节点是否已被访问intLocation(AdjListGraph &G,char c);//图的初始化voidInit(AdjListGraph &G){
G.adjlist =new AdjList[Maxize];
G.edge =0;
G.vex =0;for(int i =0; i < Maxize; i++){
visited[i]=false;}}//图的创建voidCreate(AdjListGraph &G){
cout <<"请输入改图的顶点数以及边数:"<< endl;
cin >> G.vex >> G.edge;
cout <<"请输入相关顶点:"<< endl;for(int i =0; i < G.vex; i++){
cin >> G.adjlist[i].data;
G.adjlist[i].first =NULL;}char v1 =0, v2 =0;//保存输入的顶点字符int i1, i2;//保存顶点再数组中的下标
cout <<"请输入相关联边的顶点:"<< endl;for(int i =0; i < G.edge; i++){
cin >> v1 >> v2;
i1 =Location(G, v1);
i2 =Location(G, v2);if(i1 !=-1&& i2 !=-1)//寻找到位置{
EdgeNode *temp =new EdgeNode;
temp->adjvex = i2;
temp->next = G.adjlist[i1].first;
G.adjlist[i1].first = temp;}}}//通过顶点对应的字符寻找顶点在图中的邻节点intLocation(AdjListGraph &G,char c){for(int i =0; i < G.edge; i++){if(G.adjlist[i].data == c){return i;}}return-1;}//对图上的顶点进行深度遍历voidDFS(AdjListGraph &G,int v){int next =-1;if(visited[v])return;
cout << G.adjlist[v].data <<" ";
visited[v]=true;
EdgeNode *temp = G.adjlist[v].first;while(temp){
next = temp->adjvex;
temp = temp->next;if(visited[next]==false){DFS(G, next);}}}//对所有节点进行深度遍历voidDFS_Main(AdjListGraph &G){for(int i =0; i < G.vex; i++){if(visited[i]==false){DFS(G, i);}}for(int i =0; i < Maxize; i++){
visited[i]=false;}}//对图上的顶点进行广度遍历voidBFS(AdjListGraph &G,int v){
queue<int>q;int cur =-1;int index;
q.push(v);while(!q.empty()){
cur = q.front();if(visited[cur]==false){
cout << G.adjlist[cur].data <<" ";
visited[cur]=true;}
q.pop();
EdgeNode *temp = G.adjlist[cur].first;while(temp !=NULL){
index = temp->adjvex;
temp = temp->next;
q.push(index);}}}//对所有节点进行广度遍历voidBFS_Main(AdjListGraph &G){for(int i =0; i < G.vex; i++){if(visited[i]==false){BFS(G, i);}}for(int i =0; i < Maxize; i++){
visited[i]=false;}}intmain(){
AdjListGraph G;Init(G);Create(G);DFS_Main(G);
cout << endl;BFS_Main(G);system("pause");return0;}