HASH表中解决冲突的方法(链址法)…

// DEBUG2.cpp : Defines the entry point for the console application.
//
// data.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define HASHSIZE 6999999
#define DATASIZE 2000
#define I_USED 1
#define I_UNUSED 0
typedef unsigned int __u32;
typedef unsigned short __u16;
typedef unsigned char __u8;
int DATACOUNT=DATASIZE;
int HASHCOUNT=0;
struct _data
{
__u32 saddr;
__u32 ext_isn;
__u32 int_isn;
__u16 sport;
__u8 int_ok;
__u8 three_shake;
__u8 fin_count;
char flag;
__u32 head;
struct _data *next;
struct _data *prev;
}data_list[DATASIZE],*data_hash[HASHSIZE];
struct _data *data_head;
struct _data *data_tail;
int  alloc_data_inode(__u16 sport, __u32 saddr ,__u32  ext_isn,__u32 index)
{
 struct _data *pcb_p;
 if(DATACOUNT>1){
 
 if(data_tail!=NULL){
  pcb_p=data_tail;
  (pcb_p->prev)->next=NULL;
  data_tail=pcb_p->prev;
  pcb_p->prev=NULL;
  DATACOUNT--;
 }
 else if(data_tail==data_head){
  pcb_p=data_tail=data_head;
 }
 else{
  printf("inodes have been used!!");
 }
  
 
   if( data_hash[index] != NULL )  
    {
     data_hash[index]->prev=pcb_p;
     pcb_p->next = data_hash[index];
     pcb_p->prev=NULL;
     data_hash[index]=pcb_p;
     HASHCOUNT++;
    }
    else if(data_hash[index]==NULL)
     {
     data_hash[index]=pcb_p;
     pcb_p->next=NULL;
     pcb_p->prev=NULL;
     HASHCOUNT++;
    }
   
    pcb_p->saddr=saddr;
    pcb_p->sport=sport;
    pcb_p->ext_isn=ext_isn;
    pcb_p->three_shake=1;
    pcb_p->head=index;
    pcb_p->flag=I_USED;
     return 0;
 
 }else{
  printf("inodes have been used!!");
  
 }
 
}
 
int detach_pcb(struct _data *pcb_p,__u32 index,__u16 sport,__u32 saddr)
{
// char j;
 while( data_hash[index] != NULL )
 {
   
  if( pcb_p->saddr==saddr && pcb_p->sport==sport )
  {
   //delete it from data_link and add to the data_tail
   //MIDDLE
   if(pcb_p->next !=NULL&&pcb_p->prev !=NULL )
    {
     
     (pcb_p->prev)->next=pcb_p->next;
     (pcb_p->next)->prev=pcb_p->prev;
     data_head->prev=pcb_p;
     pcb_p->prev=NULL;
     pcb_p->next=data_head;
     data_head=pcb_p;
        
   }//TAIL
   else if(pcb_p->next==NULL&&pcb_p->prev!=NULL)
    { 
     (pcb_p->prev)->next=NULL;
     pcb_p->prev=NULL;
     data_head->prev=pcb_p;
     pcb_p->prev=NULL;
     pcb_p->next=data_head;
     data_head=pcb_p;
   }//HEAD
      else if(pcb_p->next!=NULL&&pcb_p->prev==NULL)
    { 
     data_hash[index]=data_hash[index]->next;
     (pcb_p->next)->prev=NULL;
     pcb_p->next=NULL;
     data_head->prev=pcb_p;
     pcb_p->prev=NULL;
     pcb_p->next=data_head;
     data_head=pcb_p;
   }
  
    pcb_p->saddr=0;
    pcb_p->sport=0;
    pcb_p->three_shake=0;
    pcb_p->ext_isn=0;
    pcb_p->fin_count=0;
    pcb_p->int_isn=0;
    pcb_p->int_ok=0;
    pcb_p->flag=I_UNUSED;
    pcb_p->head=0; 
    return 0; //detach sucessful!
 
  }else{
   if(pcb_p->next!=NULL){
    pcb_p=pcb_p->next;
   }else{
    printf("NO INODE!!!");
    break;
   }
  }
  
 }
 return -1;
 
}
struct _data * lookup_pcb(__u32 saddr,__u16  sport,__u32 index)
{
 struct _data *pcb_p;
 if( data_hash[index] != NULL )
 {
  pcb_p = data_hash[index];
  while( pcb_p != NULL )
  {
   if( pcb_p->saddr==saddr && pcb_p->sport==sport )
   {
    
    return pcb_p;
   }
   else
   {
    pcb_p = pcb_p->next;
   }
  }
 }
 return NULL;
}

void init_data(int i)
{
 data_head=&data_list[0];
 data_tail=&data_list[DATASIZE-1];
 for(i=0;i  {
  if(i == 0)
  {
   data_list[i].prev = NULL;
   data_list[i].next = &data_list[1];
  }
  else if(i == (DATASIZE-1))
  {
   data_list[i].next= NULL;
   data_list[i].prev=&data_list[i-1];
  }
  else
  {
   data_list[i].next= &data_list[i+1];
   data_list[i].prev=&data_list[i-1];
  }
  
  data_list[i].int_ok=0;
  data_list[i].three_shake=0;
  data_list[i].sport=0;
  data_list[i].saddr=0;
  data_list[i].ext_isn=0;
  data_list[i].int_isn=0;
  data_list[i].fin_count=0;
  data_list[i].head=0;
  data_list[i].flag=I_UNUSED;
   }
 for(i=0;i   data_hash[i]=NULL;
}
int main(int argc, char* argv[])
{
 int i;
 int j=0;
 int x=0;
 __u32 saddr;
 __u16 sport;
 struct _data *pPtr;
 init_data(1);
 printf("data_init sucessful\n");
 for(i=0;i  {
  
  alloc_data_inode(123+i,987654+i,98765432+i,123456);
  printf("i=%d\n",i);
 }
 printf("-------------------------\nDATACOUNT=%d\n",DATACOUNT);
 printf("-------------------------\nHASHCOUNT=%d\n",HASHCOUNT);
 pPtr=data_hash[123456];
 while(pPtr!=NULL)
 {
  printf("(%d)%p:prev=%p,next=%p[saddr=%d,sport=%d]\n",x,pPtr,pPtr->prev,pPtr->next,pPtr->saddr,pPtr->sport);
  pPtr=pPtr->next;
  x++;
 }
 printf("-----------------------------------------\n");
 x=0;
 pPtr=data_hash[123456];
 for(i=0;i  {
  saddr=987654+i;
  sport=123+i;
  detach_pcb(pPtr,123456,sport,saddr);
  printf("(%d)delect:%d,pPtr=%p,pPtr->sport=%p\n",x,i,pPtr,pPtr->sport);
  x++;
 }
printf("-----------------------------------------\n");
  pPtr=data_hash[123456];
  x=0;
 while(pPtr!=NULL)
 {
  printf("(%d)%p:prev=%p,next=%p[saddr=%d,sport=%d]\n",x,pPtr,pPtr->prev,pPtr->next,pPtr->saddr,pPtr->sport);
  pPtr=pPtr->next;
  x++;
 }
 printf("-----------------------------------------\n");
 for(i=0;i  {
 pPtr=lookup_pcb(987654+i,123+i,123456);
 if(pPtr!=NULL)
 {
  j++;
  printf("i=%d found it\n",i);
 }
 if(pPtr==NULL)
  printf("i=%d not found\n",i);
 }
 pPtr=data_hash[123456];
 while(pPtr!=NULL)
 {
  printf("(%d)%p:prev=%p,next=%p[saddr=%d,sport=%d]\n",x,pPtr,pPtr->prev,pPtr->next,pPtr->saddr,pPtr->sport);
  pPtr=pPtr->next;
  x++;
 }
 //exit(0);
 return 0;
}
 
 

你可能感兴趣的:(C/C)