#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;
}