찾기 (2) - 해시 찾기

5897 단어 데이터 구조
#include 
#define MAXSIZE 11
#define key 11

void insert(int hashArray[],int x){
    int i = 0;
    int t = x % key;
    while(i < MAXSIZE){
        if(hashArray[t] <= -1){
            hashArray[t] = x;
            break;
        }else{
            t = (t+1) % key;
        }
        i++;
    }
    if(i == MAXSIZE){
        printf("     !
"
); } } void search(int hashArray[],int x){ int i = 0; int t = x % key; while(hashArray[t] != -1 && iif(hashArray[t] == x){ printf(" :%d",t); break; }else{ t = (t+1) % key; } i++; } if(i == MAXSIZE || hashArray[t] == -1){ printf(" !
"
); } printf("
"
); } void del(int hashArray[],int x){ int i = 0; int t = x % key; while(hashArray[t] != -1 && i < MAXSIZE){ if(hashArray[t] == x){ hashArray[t] = -2; printf(" !
"
); break; }else{ t = (t+1)%key; } i++; } if(i == MAXSIZE || hashArray[t] == -1){ printf(" !
"
); } } void print(int hashArray[]){ for(int i=0;iprintf("%d ",hashArray[i]); } } int main(){ int hashArray[MAXSIZE]; int x,k=0; int target; int delTarget; for(int i=0;i1; } printf(" !(-1 )
"
); scanf("%d",&x); while(x != -1 && k < MAXSIZE){ insert(hashArray,x); k++; scanf("%d",&x); } printf(" :
"
); print(hashArray); printf("
"
); printf(" 。
"
); scanf("%d",&target); search(hashArray,target); printf(" 。
"
); scanf("%d",&delTarget); del(hashArray,delTarget); printf(" :
"
); print(hashArray); return 0; }

좋은 웹페이지 즐겨찾기