hash桶,按比较次数做删除操作

#include 
  
  
#include 
  
  
#include 
  
  

typedef struct node{
        int num;
        struct node *next;
        }node;
        
node *node_hash[5] = {0};

void init()
{
     int i = 0;
     int j = 0;
     for (i=0;i<5;i++)
     {
         for(j=0;j<7;j++)
         {
             node *tmp=(node*)malloc(sizeof(node));
             if(NULL != tmp)
             {
                 tmp->num = j;
                 tmp->next = node_hash[i];
                 node_hash[i] = tmp;
             }
         }
     }
}

void print()
{
     int i = 0;
     int j = 0;
     for (i=0;i<5;i++)
     {
         j = 0;
         node *tmp = node_hash[i];
         while(tmp)
         {
             printf("/r/n(%d,%d)=%d",i,j,tmp->num);
             j++;
             tmp=tmp->next;
         }
     }
     printf("/r/n-------------------------------");
}
/*在hash桶中,指定一个比较次数,依次删除符合项,但每掉一次函数,只比较这个指定个数的节点*/
void delete4()
{
    static node *node_p = NULL;
    static int location = 0;
    node node_tmp;
    int i = 0;
    int j = 0;
    int count = 0;
    
    i = location;
    
    
    for(j=0;j<5;j++)
    {
        node *tmp = NULL;
        
        if(!node_p)
        {
            node_tmp.next = node_hash[i];
            tmp = &node_tmp;
        }
        else
        {//printf("/r/nh:%d", node_p->num);
            tmp = node_p; 
        }
        
        while(tmp->next)
        {
            count++;/*次数统计*/
            printf("/r/n(%d,?)=%d", i, tmp->next->num);
            
            if(tmp->next->num%2==0)
            {
                node *tmp2 = tmp->next;
                if(tmp == &node_tmp)
                {
                    node_hash[i] = tmp2->next;
                }
                tmp->next = tmp2->next;
                free(tmp2);
            }
            else
            {
                tmp = tmp->next;
            }
            
            if(count == 15)
            {
                /*如果还是在起始点,则指向起始点*/
                if(tmp==&node_tmp)
                {
                    location = i;
                    node_p = NULL;
                }
                else if(tmp->next == NULL)/*如果已经结束,则指向下一个*/
                {
                    //printf("/r/n%d",location);
                    location++;
                    if(location == 5)
                    {
                        location = 0;
                    }
                    node_p = NULL;
                }
                else
                {
                    node_p = tmp;
                }
                return;
            }
        }

        node_p = NULL;
        location++;
        if(location == 5)
        {
            location = 0;
        }
        i = location;
    }
}


int main(int argc, char *argv[])
{
    init();
    print();
    delete4();
    print();
    delete4();
    print();
    delete4();
    print();
    delete4();
    print();
    delete4();
    print();
    delete4();
    print();
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值