zoukankan      html  css  js  c++  java
  • hash表建立 很久没写数据结构了

    /**
    auhtor:lx
    date 4.9 2011
    brief hash table
    */

    #include
    <stdio.h>
    #include
    <stdlib.h>
    #include
    <stdbool.h>
    #define hash_num 5

    typedef
    char Bool;

    struct hash_node
    {
    int date; /*结点值*/
    struct hash_node *next; /*下一个节点*/
    Bool found;
    };

    struct hash_node* hash_table[hash_num]; /*存储hash table 所有信息*/

    void hash_table_found( int value )
    {
    int hash;
    hash
    = value % hash_num;

    /* 建立结点 */
    struct hash_node* node = (struct hash_node*)malloc( sizeof( struct hash_node) );
    node
    ->date = value;
    node
    ->next = NULL;
    node
    ->found = 't';

    if ( hash_table[ hash ]->found == 'f' )
    {
    hash_table[ hash ]
    ->date = value;
    hash_table[ hash ]
    ->next = NULL;
    hash_table[ hash ]
    ->found = 't';
    }
    else
    {
    node
    ->next = hash_table[ hash ];
    hash_table[ hash ]
    = node;
    }

    }
    void hash_table_output()
    {
    int i = 0;

    for ( ; i < hash_num; i++ )
    {
    if ( hash_table[i]->found == 't' )
    {
    while ( hash_table[i] != NULL )
    {
    printf(
    "%d\t", hash_table[i]->date );
    hash_table[i]
    = hash_table[i]->next;
    }

    printf(
    "\n" );
    }
    }
    }

    int main( void )
    {
    /*要放入hash表的数据*/
    int a[10] = { 0, 0, 400, 20, 12, 450, 2222, 30, 1 , 200 };

    int j;
    for ( j = 0; j < hash_num; j++ )
    {
    hash_table[j]
    = ( struct hash_node* )malloc( sizeof( struct hash_node ) );
    hash_table[j]
    ->date = 0;
    hash_table[j]
    ->next = NULL;
    hash_table[j]
    ->found = 'f';

    }

    int i = 0;
    for ( ; i < 10; i++ )
    {
    hash_table_found( a[i] );
    }

    hash_table_output();
    }
  • 相关阅读:
    MySQL中IS NULL、IS NOT NULL、!=不能用索引?胡扯!
    市值TOP10,人类进化及中美坐标
    倒序切片
    对list进行切片
    Python之定义可变参数
    Python之递归函数
    Python之“可变”的tuple
    Python之创建单元素tuple
    Python中Unicode字符串
    Pycharm配置autopep8让Python代码更符合pep8规范
  • 原文地址:https://www.cnblogs.com/lxgeek/p/2010573.html
Copyright © 2011-2022 走看看