게임강의
각종 노하우 분류

각종 노하우 [C를 활용한 문제해결 기법의 근간] 학습용!! hash의 근간!! hash_table

컨텐츠 정보

본문

[출처: Gamezone]
작성자: 작성자 없음

#include <stdio.h>

#include <string.h>


#define KEY_SIZE 10

#define TABLE_SIZE 13 // 소수

#define MAX_STRING 30

typedef struct

{

    char name[MAX_STRING];

    char phone[MAX_STRING];

    int empty;


}element;

element hash_table[TABLE_SIZE];


void InitTable(element *ht) // 해시테이블 초기화

{

    int i;

    for (i = 0; i<TABLE_SIZE; i++)

    {

        memset(&ht[i].name, 0, sizeof(ht[i].name));

        memset(&ht[i].phone, 0, sizeof(ht[i].phone));

        ht[i].empty = -1;

    }

}


int Transform(char *key) // 문자를 숫자로 변환

{

    int number = 0;

    while (*key)

        number += (*key++);

    return number;

}


int HashFunction(char *key)

{

    return Transform(key) % TABLE_SIZE;

}


int HashAdd(element item, element *ht) // 항목 추가

{

    int i, value;

    value = i = HashFunction(item.name);


    while (ht[i].empty == 1) // 빈 버켓을 찾는다.

    {

        if (strcmp(item.name, ht[i].name) == 0)

        {

            printf("이름이 중복되었습니다\n");

            return -1;

        }


        i = (i + 1) % TABLE_SIZE; // 0~TABLE_SIZE 내에서 값이 1씩 증가한다.


        if (i == value) // 테이블 전체가 검사되었을 때

        {

            printf("테이블이 가득찼습니다\n");

            return -1;

        }

    }


    item.empty = 1;

    ht[i] = item; // 버켓이 비어있다면 바로 삽입


    return 1;

}


int HashDelete(element item, element *ht) // 데이터 삭제

{

    int i, value;

    value = i = HashFunction(item.name);

    while (!(ht[i].empty == -1)) // 버켓이 비어있지 않다면

    {

        if (strcmp(item.name, ht[i].name) == 0)

        {

            printf("삭제 데이터 : 위치 = %d, 이름 : %s, 전화번호 : %s\n", i, ht[i].name, ht[i].phone);

            memset(&ht[i].name, 0, sizeof(ht[i].name));

            memset(&ht[i].phone, 0, sizeof(ht[i].phone));

            ht[i].empty = 0; // 삭제 표시,한번 사용된 버켓

            return 1;

        }


        i = (i + 1) % TABLE_SIZE; // 0~TABLE_SIZE 내에서 값이 1씩 증가한다.


        if (i == value) // 테이블 전체가 검사되었을 때

        {

            printf("찾는 값이 테이블에 존재하지 않습니다.\n");

            return -1;

        }

    }


    printf("테이블이 비어있습니다..\n");

    return -1;


}

int HashSearch(element item, element *ht) // 데이터 탐색

{

    int i, value;

    value = i = HashFunction(item.name);

    while (!(ht[i].empty == -1)) // 버켓이 비어있지 않다면

    {

        if (strcmp(item.name, ht[i].name) == 0)

        {

            printf("탐색 성공 : 위치 = %d, 이름 : %s, 전화번호 : %s\n", i, ht[i].name, ht[i].phone);

            return 1;

        }


        i = (i + 1) % TABLE_SIZE; // 0~TABLE_SIZE 내에서 값이 1씩 증가한다.


        if (i == value) // 테이블 전체가 검사되었을 때

        {

            printf("찾는 값이 테이블에 존재하지 않습니다.\n");

            return -1;

        }

    }


    printf("찾는 값이 테이블에 존재하지 않습니다.\n");

    return -1;

}


int main()

{

    element temp;

    int input;

    int i;


    InitTable(hash_table);


    while (1)

    {

        printf("1:입력\n2:삭제\n3:탐색\n4:종료\n");

        scanf_s("%d", &input);

        fflush(stdin);

        printf("이름 입력 : ");

        gets_s(temp.name);


        switch (input)

        {

        case 1:

            printf("전화번호 입력 : ");

            gets_s(temp.phone);

            HashAdd(temp, hash_table);

            break;

        case 2:

            HashDelete(temp, hash_table);

            break;

        case 3:

            HashSearch(temp, hash_table);

            break;

        default:

            return 0;

        }


        for (i = 0; i<TABLE_SIZE; i++)

            printf("hashtable[%d] :%s\n", i, hash_table[i].name);

    }


    return 0;

}

관련자료

댓글 0
등록된 댓글이 없습니다.
전체 422 / 1 페이지
번호
제목
이름

강의실

🏆 포인트 랭킹 TOP 10
순위 닉네임 포인트
1 no_profile 타키야겐지쪽지보내기 자기소개 아이디로 검색 전체게시물 82,042
2 no_profile 라프텔쪽지보내기 자기소개 아이디로 검색 전체게시물 51,280
3 no_profile 동가리쪽지보내기 자기소개 아이디로 검색 전체게시물 33,479
4 no_profile Revolution쪽지보내기 자기소개 아이디로 검색 전체게시물 28,199
5 no_profile 닥터스쪽지보내기 자기소개 아이디로 검색 전체게시물 22,310
6 서번트쪽지보내기 자기소개 아이디로 검색 전체게시물 21,452
7 no_profile 불멸의행복쪽지보내기 자기소개 아이디로 검색 전체게시물 16,347
8 no_profile 호롤롤로쪽지보내기 자기소개 아이디로 검색 전체게시물 13,500
9 no_profile 검은고양이쪽지보내기 자기소개 아이디로 검색 전체게시물 13,246
10 no_profile 하늘2쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 13,239
알림 0