#include <stdio.h>
#include <stdlib.h>

#define HashSize 11

struct HashTableNode{

    int data;
    struct HashTableNode* pnext;
}HashArray[HashSize];
typedef struct HashTableNode *pnode;

void InitHashTable(){

    int i = 0;
    for(i = 0; i < HashSize; i++)
        HashArray[i].data = i;
}

pnode HashTailNode(int i){
    pnode p = &HashArray[i];
    while(p->pnext != NULL)
        p = p->pnext;
    return p;
}

int HashFunc(int data){
    int idata = data%11;
    return idata;
}

void createNode(){

    int idata,i;
    int n;
    
    puts("please input nums you will input:");
    scanf("%d",&n);
    while(n != 0){
        puts("please input the data:");
        scanf("%d",&idata);
        i = HashFunc(idata);
        pnode p = HashTailNode(i);
        pnode pnew = (pnode)malloc(sizeof(struct HashTableNode));

        pnew->data = idata;
        p->pnext = pnew;
        pnew->pnext = NULL;    
        n--;
    }
}


void TrverseHashTable(){
    int i = 0;
    for(i = 0; i < HashSize; i++){
        pnode p = &HashArray[i];
        printf("index%d:  ",HashArray[i]);
        while(p != NULL){
            if(p->pnext != NULL){
                p = p->pnext;
                printf("%4d -->",p->data);
            }
            else 
                break;
        }
        puts("(nil)");
    }
}

int serach(int n){

    int i = HashFunc(n);
    pnode p = &HashArray[i];
    int j = 1;
    if(p->pnext == NULL)
        return 0;
    p = p->pnext;
    while(p != NULL){
        if(n == p->data)
            break;
        p = p->pnext;
        j++;
    }
    if(p == NULL)
        return 0;
    printf("%d %d\n",i,j);
}

int main()
{
    InitHashTable();
    createNode();
    TrverseHashTable();
    serach(15);
    return 0;
}
posted on 2012-08-16 12:50  fisher2012  阅读(297)  评论(0编辑  收藏  举报