Hash API

#include <iostream>
using namespace std;

#define size 100

struct node{

    int val;
    node *pre;
    node *next;

};

int index = 0;
node HashTable[size];
node HashPool[size];

node *getNewNode()
{
    return &HashPool[index++];
}

void init()
{
    index = 0;
    for(int i = 0; i < size; i++)
    {
        HashTable[i].next = &HashTable[i];
        HashTable[i].pre = &HashTable[i];
        HashTable[i].val = 0;
    }
}

void insert(int key,node *newNode)
{
    node *head = &HashTable[key];
    
    newNode->next = head ->next;
    newNode->pre = head;
    head ->next->pre = newNode;
    head->next = newNode;
    
}

node * searchNode(int key,node *newNode)
{
    node *x = HashTable[key].next;
    while(x != &HashTable[key]){
        x = x->next;
    }
    if(x != &HashTable[key])
        return x;
    else return NULL;
}

void deleteNode(node * node){
    node->pre->next = node->next;
    node->next->pre = node->pre;
}

int main()
{
    init();
    for(int i = 0; i < 100; i++)
    {
        int num = rand()%100+1;
        int key = num % 11;
        HashTable[key].val++;
        node *newNode = getNewNode();
        newNode->val = num;
        insert(key,newNode);
    }

    for(int i = 0; i < 100; i++)
    {
        if(HashTable[i].val != 0)
        {
            cout << i << " ";
            cout << HashTable[i].val << " : " ;
            node *head = (&HashTable[i]) -> next;
            while(head != &HashTable[i])
            {
                cout << head->val << "  ";
                head = head -> next;
            }
            cout << endl;
        }
    }
    return 0;
}

 

posted @ 2017-12-13 16:53  prog123  阅读(292)  评论(0编辑  收藏  举报