Hash表——The Hash table
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "list.h"
#define NAMESIZE 24
 #define HASHSIZE 256
typedef LIST HASH;
typedef struct stuinfo{
  int id;
  char name[NAMESIZE];
  int math;
 }DATA;
static void print_s(const void *data)
 {
  const DATA *stup = data;
  printf("%d %s %d\n", 
    stup->id,
    stup->name,
    stup->math);
 }
static int IdCmp(const void *key, const void *data)
 {
  const int *id = key;
  const DATA *stup = data;
return (*id - stup->id);
 }
 static int NameCmp(const void *key, const void *data)
 {
  const char *name = key;
  const DATA *stup = data;
return strcmp(name, stup->name);
 }
int hash_init(HASH *hash, int size)
 {
  int i, j;
for(i = 0; i < HASHSIZE; i++)
  {
   hash[i] = ListCreate(size);
   if(NULL == hash[i])
   {
    for(j = 0; j < i; j++)
     ListDispose(hash[j]);
    return -1;
   }
  }
  return 0;
 }
int getnum(int nu)
 {
  return (nu % HASHSIZE);
 }
int hash_insert(HASH *hash, const DATA *data)
 {
  int i = getnum(data->id);
return ListInsert(hash[i], data, HEADINSERT);
 }
void hash_display(HASH *hash)
 {
  int i;
  int count = 0;
for(i = 0; i < HASHSIZE; i++)
  {
   count = 0;
   PtrNode p = hash[i]->head.next;
   for(; p != &hash[i]->head; p = p->next, count++);
   printf("%d ", count);
  }
  printf("\n");
 }
void hash_dispose(HASH *hash)
 {
  int i;
  for(i = 0; i < HASHSIZE; i++)
  {
   ListDispose(hash[i]);
   hash[i] = NULL;
  }
 }
void *hash_find(HASH *hash, int id)
 {
  int i = getnum(id);
  
  return ListFind(hash[i], &id, IdCmp);
 }
int main()
 {
  int i;
  int id = 5;
  char name[NAMESIZE] = "stu3";
  DATA stu, *stup;
  HASH hash[HASHSIZE] = {0};
hash_init(hash, sizeof(DATA));
srand(time(NULL));
  for(i = 0; i < 9999; i++)
  {
   id = stu.id = rand();
   snprintf(stu.name,NAMESIZE,
     "stu%d", i + 1);
   stu.math = rand() % 100;
hash_insert(hash, &stu);
  }
  stup = hash_find(hash, id);
  if(stup == NULL)
   printf("not find\n");
  else
   print_s(stup);
hash_display(hash);
  hash_dispose(hash);
  
  return 0;
 }
 -----------------------------------------------------------
#ifndef _LIST_H__
 #define _LIST_H__
#define HEADINSERT 1
 #define TAILINSERT  2
struct listnode;
 struct headnode;
 typedef struct headnode *LIST;
 typedef struct listnode *PtrNode;
typedef void print(const void *);
 typedef int cmp(const void *, const void *);
LIST ListCreate(int);
 int ListInsert(LIST, const void *, int);
 void *ListFind(LIST, const void *, cmp *);
 int ListDelete(LIST, const void *, cmp *);
 int ListFetch(LIST, const void *, cmp *, void *);
 void ListDisplay(LIST, print *);
 void ListDispose(LIST);
struct listnode{
  void *data;
  struct listnode *prev;
  struct listnode *next;
 };
struct headnode{
  int size;
  struct listnode head;
 };
#endif
 ---------------------------------------------------------------------
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "list.h"
LIST ListCreate(int size)
 {
  LIST handle = malloc(sizeof(*handle));
  if(NULL == handle)
   return NULL;
handle->size = size;
  handle->head.data = NULL;
  handle->head.prev = handle->head.next = &handle->head;
return handle;
 }
int ListInsert(LIST l, const void *data, int mode)
 {
  PtrNode cur = malloc(sizeof(*cur));
  if(NULL == cur)
   return -1;
  cur->data = malloc(l->size);
  if(NULL == cur->data)
  {
   free(cur);
   return -2;
  }
memcpy(cur->data, data, l->size);
if(mode == HEADINSERT)
  {
   cur->next = l->head.next;
   cur->prev = &l->head;
  }
  else if(mode == TAILINSERT)
  {
   cur->next = &l->head;
   cur->prev = l->head.prev;
  }
  else
  {
   free(cur->data);
   free(cur);
   return -3;
  }
  cur->prev->next = cur;
  cur->next->prev = cur;
  return 0;
 }
static PtrNode find(LIST l, const void *key, cmp *funp)
 {
  PtrNode p = l->head.next;
for(;p != &l->head && funp(key, p->data); p = p->next);
return p;
 }
void *ListFind(LIST l, const void *key, cmp *funp)
 {
  return find(l, key, funp)->data;
 }
int ListDelete(LIST l, const void *key, cmp *funp)
 {
  PtrNode p = find(l, key, funp);
  if(p == &l->head)
   return -1;
p->prev->next = p->next;
  p->next->prev = p->prev;
  //p->prev = p->next = NULL;
  
  free(p->data);
  free(p);
  //p = NULL;
  return 0;
 }
int ListFetch(LIST l, const void *key, cmp * funp, void *data)
 {
  PtrNode p = find(l, key, funp);
  if(p == &l->head)
   return -1;
memcpy(data, p->data, l->size);
  p->prev->next = p->next;
  p->next->prev = p->prev;
  //p->prev = p->next = NULL;
  
  free(p->data);
  free(p);
  //p = NULL;
  return 0;
 }
void ListDisplay(LIST l, print *funp)
 {
  PtrNode p = l->head.next;
while(p != &l->head)
  {
   funp(p->data);
   p = p->next;
  }
 }
void ListDispose(LIST l)
 {
  PtrNode p = l->head.next;
  PtrNode q;
while(p != &l->head)
  {
   q = p;
   p = p->next;
free(q->data);
   free(q);
  }
  free(l);
 }
Hash表——The Hash table的更多相关文章
- Hash表(hash table ,又名散列表)
		
直接进去主题好了. 什么是哈希表? 哈希表(Hash table,也叫散列表),是根据key而直接进行访问的数据结构.也就是说,它通过把key映射到表中一个位置来访问记录,以加快查找的速度.这个映射函 ...
 - hash-1.hash表和hash算法
		
1.hash表 哈希表,也叫散列表,是根据关键码(Key)而直接访问的数据结构,也就是它把Key映射到表中一个位置来访问记录,即,把key计算成hashcode,把hashcode存到表中.这个把ke ...
 - hash表、hash算法
		
概念: 散列表(Hash table.也叫哈希表),是依据关键码值(Key value)而直接进行訪问的数据结构. 也就是说,它通过把关键码值映射到表中一个位置来訪问记录,以加快查找的速度.这个映射函 ...
 - Hash表及hash算法的分析
		
Hash表中的一些原理/概念,及根据这些原理/概念: 一. Hash表概念 二. Hash构造函数的方法,及适用范围 三. Hash处理冲突方法,各自特征 四. ...
 - Hash表的扩容(转载)
		
Hash表(Hash Table) hash表实际上由size个的桶组成一个桶数组table[0...size-1] . 当一个对象经过哈希之后.得到一个对应的value , 于是我们把这个对象放 ...
 - php 数据结构 hash表
		
hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...
 - 【数据结构】Hash表简介及leetcode两数之和python实现
		
文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...
 - NGINX(三)HASH表
		
前言 nginx的hash表有几种不同的种类, 不过都是以ngx_hash_t为基础的, ngx_hash_t是最普通的hash表, 冲突采用的是链地址法, 不过这里冲突的元素不是一个链表, 而是一个 ...
 - 【数据结构】非常有用的hash表
		
这篇博客的目的是让尚未学会hash表的朋友们对hash表有一个直观的理解,并且能根据本文定义出属于自己的第一个hash表,但算不上研究文,没有深究概念和成功案例. 什么是has ...
 
随机推荐
- IS---InstallShield第二天
			
在Setup.rul中,新增OnBegin函数 STRING str1,spath,szApplicationPath,szApplicationCmdLine,szCmdLine;function ...
 - 帝国cms7.0修改默认搜索模版中的分页[!--show.page--]
			
修改默认搜索模版的分页是在e/class/connect.php下 搜索下function page1就是我们要修改的分页了 下面贴上我修改后的分页 //前台分页 function page1($nu ...
 - 2016022610 - redis列表命令集合
			
参考网址:http://www.yiibai.com/redis/redis_lists.html Redis列表是简单的字符串列表,排序插入顺序.您可以在头部或列表的尾部Redis的列表添加元素.列 ...
 - 阿里云ECS服务器配置ubuntu安装openfire服务器
			
最近搞了一台阿里云的ECS服务器,因为搞活动半年免费,所以就申请了一台,过两天就批准下来,顺便多花了1百多RMB买了固定IP.总体说来还是挺值的,觉得一个人用挺浪费,分享出来跟大家一起玩玩. 搞台服务 ...
 - NSURLSession -- 实际开发中运用
			
NSURLSession实际请求 iOS9使用http请求方法: 在工程info.plist文件内添加NSAppTransportSecurity键,类型为dictionary 在NSAppTrans ...
 - Noah的学习笔记之Python篇:命令行解析
			
Noah的学习笔记之Python篇: 1.装饰器 2.函数“可变长参数” 3.命令行解析 注:本文全原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) ...
 - javascript日用代码集合(一)
			
获取url参数 function get_url_param(name){ var reg = new RegExp("(^|&)" + name + "=([^ ...
 - [BZOJ 1053] [HAOI 2007] 反素数ant
			
题目链接:BZOJ 1053 想一想就会发现,题目让求的 1 到 n 中最大的反素数,其实就是 1 到 n 中因数个数最多的数.(当有多于一个的数的因数个数都为最大值时,取最小的一个) 考虑:对于一个 ...
 - 在Eclipse中安装spket插件
			
spket是一个开发JavaScript和Ext等的开发工具,它可以 是独立的IDE,也可以作为 Eclipse的插件使用,下面介绍如何在Eclipse中安装spket插件, 1.首先上 官网 htt ...
 - QT5.1.1中MinGW4.8的环境变量配置
			
1.右击“我的电脑”图标,在弹出的菜单上选择“属性(R)”菜单项. 2.选择“高级”选项卡.点击“环境变量”按钮. 3.点击“新建(W)”按钮,新建环境变量:MINGW_HOME,变量值为MinGW的 ...