'C++프로그래밍/자료구조'에 해당되는 글 29건

//정렬되지 않은 배열에서 탐색

//순차탐색
int seq_search(int key, int low, int high){
 int i;
 for(i=low; i<high; i++){
  if(list[i] == key)
   return i;
 }
 return -1;
}

//개선된 순차탐색
int seq_search2(int key, int low, int high){
 int i;
 list[high+1] = key;
 for(i=low; list[i] != key; i++)
  ;
 if(i == (high+1)) return -1;
 else
  return i;
}

//정렬된 배열에서 탐색

//개선된 순차탐색
int sorted_seq_search(int key, int low, int high){
 int i;
 if(key<list[low] || key>list[high])
  return -1;
 for(i=low; i<=high; i++){
  if(list[i] > key) return -1;
  if(list[i] == key) return i;
 }
}

//이진 탐색(recursion)
int search_binary(int key, int low. int high){
 int middle;
 if(low<high){
  middle=(low+high)/2;
  if(key == list[middle])
   return middle;
  else if(key <list[middle])
   return search_binary(key,low,middle-1);
  else
   return search_binary(key,middle+1,high);
 }
 return -1;
}

//이진 탐색(반복)
int search_binary2(int key, int low, int high){
 int middle;

 while(low<=high){
  middle=(low+high)/2;
  if(key == list[middle])
   return middle;
  else if(key > list[middle])
   low = middle+1;
  else
   high = middle-1;
 }
 return -1;
}
//정렬된 배열에서 색인 순차 탐색
typedef struct{
 int key;
 int index;
}itable;
itable index_list[index_size];
//n은 전체 데이터의 수
int search_index(int key){
 int i,low,high;

 if(key<list[0] || key>list[n-1])
  return -1;

 for(i=0; i<INDEX_SIZE; i++)
  if(index_list[i].key<=key && index_list[i+1].key>key)
   break;
 if( i ==INDEX_SIZE){
  low = index_list[i-1].index;
  high=n;
 }
 else{
  low = index_list[i].index;
  high= index_list[i+1].index;
 }

 return seq_search(key,low,high);
}


//보간 탐색
int search_interpolation(int key,int n){
 int low,high,j;
 low=0;
 high=n-1;
 while((list[high] >= key) && (key>list[low])){
  j=((float)(key-list[low])/(list[high]-list[low])*(high-low))+low;
  if(key>list[j]) low = j+1;
  else if (key < list[j]) high = j-1;
  else low = j;
 }
 if(list[low] == key)  return(low);
 else return -1;
}



 

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

#define KEY_SIZE 10
#define TABLE_SIZE 13
#define equal(e1,e2) (!strcmp(e1.key,e2.key))
typedef struct{
 char key[TABLE_SIZE];
}element;

typedef struct ListNode{
 element item;
 struct ListNode* link;
}ListNode;

ListNode* hash_table[TABLE_SIZE];

void hash_chain_add(element item, ListNode* ht){
 int hash_value = hash_function(item.key);
 ListNode* ptr;
 ListNode* node_before = NULL;
 ListNode* node = ht[hash_value];
 for(; node; node_before = node , node=node->link){
  if(equal(node->item,item)){
   fprintf(stderr,"이미 탐색 키가저장되어 있음\n");
   return;
  }
 }
 ptr=(ListNode*)malloc(sizeof(ListNode));
 ptr->item = item;
 ptr->link = NULL;
 if(node_before)
  node_before->link = ptr;
 else
  ht[hash_value] = ptr;
}

void hash_chain_find(element item, ListNode* ht){
 ListNode* node;

 int hash_value = hash_function(item.key);
 for(node=ht[hash_value];node;node=node->link){
  if(equal(node->item,item)){
   printf("키를 찾았음\n");
   return;
  }
 }
 printf("키를 찾지 못함\n");
}


 

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KEY_SIZE 10
#define TABLE_SIZE 7

#define empty(e) (strlen(e.key)==0)
#define equal(e1,e2) (!strcmp(e1.key,e2.key))

typedef struct{
 char key[KEY_SIZE];
}element;

element hash_table[TABLE_SIZE];

//해시 테이블 초기화
void init_table(element* ht){
 int i;
 for(i=0; i<TABLE_SIZE; i++){
  ht[i].key[0]=NULL;
 }
}

//문자로 된 탐색키를 숫자로 변환
int transform(char *key){
 int number = 0;
 while(*key)
  number += *key++;
 return number;
}
//제산 함수를 사용한 해싱 함수
int hash_function(char* key){
 return transform(key) % TABLE_SIZE;
}
int hash_function2(char* key){
 return (5-(transform(key) % 5) % TABLE_SIZE);
}

//선형 조사법을 이용하여 테이블에 키를 삽입하고 테이블이 가득찬 경우에는 종료
void hash_lp_add(element item, element* ht){
 int i,hash_value;
 hash_value = i = hash_function(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색키가 중복 되었습니다\n");
   return;
  }
  i= (i+1) % TABLE_SIZE;
  if(i == hash_value){
   fprintf(stderr,"테이블이 가득 찼습니다\n");
   return;
  }
 }
 ht[i]=item;
}
//이차 조사
void hash_qp_add(element item, element* ht){
 int i,hash_value,inc=0;
 hash_value = i = hash_function(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색키가 중복 되었습니다\n");
   return;
  }
  i= (i+inc+1) % TABLE_SIZE;
  inc = inc+2;
  if(i == hash_value){
   fprintf(stderr,"테이블이 가득 찼습니다\n");
   return;
  }
 }
 ht[i]=item;
}
//이중 조사
void hash_dh_add(element item, element* ht){
 int i,hash_value,inc;
 hash_value = i = hash_function(item.key);
 inc = hash_function2(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색키가 중복 되었습니다\n");
   return;
  }
  i= (i+inc) % TABLE_SIZE;
  if(i == hash_value){
   fprintf(stderr,"테이블이 가득 찼습니다\n");
   return;
  }
 }
 ht[i]=item;
}
//선형 탐색
void hash_lp_search(element item, element* ht){
 int i,hash_value;
 hash_value = i = hash_function(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색성공 : 위치 = %d\n",i);
   return;
  }
  i= (i+1) % TABLE_SIZE;
  if(i == hash_value){
   fprintf(stderr,"찾는 값이 테이블에 없음\n");
   return;
  }
 }
 fprintf(stderr,"찾는 값이 테이블에 없음\n");
}
//이차 탐색
void hash_qp_search(element item, element* ht){
 int i,hash_value,inc=0;
 hash_value = i = hash_function(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색성공 : 위치 = %d\n",i);
   return;
  }
  i= (i+inc+1) % TABLE_SIZE;
  inc = inc+2;
  if(i == hash_value){
   fprintf(stderr,"찾는 값이 테이블에 없음\n");
   return;
  }
 }
 fprintf(stderr,"찾는 값이 테이블에 없음\n");
}
//이중 탐색
void hash_dh_search(element item, element* ht){
 int i,hash_value,inc;
 hash_value = i = hash_function(item.key);
 inc = hash_function2(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색성공 : 위치 = %d\n",i);
   return;
  }
  i= (i+inc) % TABLE_SIZE;
  if(i == hash_value){
   fprintf(stderr,"찾는 값이 테이블에 없음\n");
   return;
  }
 }
 fprintf(stderr,"찾는 값이 테이블에 없음\n");
}


//해싱 테이블의 내용을 출력
void hash_lp_print(element* ht){
 int i;
 for(i=0; i<TABLE_SIZE; i++)
  printf("[%d]= %s\n",i,ht[i].key);
}

main(){
 element tmp;
 int op;
 while(1){
  printf("원하는 연산을 입력(0=입력,1=탐색,2=종료)=");
  scanf("%d",&op);
  if( op == 2) break;
  printf("키를 입력하시오 : ");
  scanf("%s",tmp.key);
  if( op == 0 )
   hash_dh_add(tmp,hash_table);
  else if( op == 1)
   hash_dh_search(tmp,hash_table);
  hash_lp_print(hash_table);
 }
}


블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KEY_SIZE 10
#define TABLE_SIZE 13

#define empty(e) (strlen(e.key)==0)
#define equal(e1,e2) (!strcmp(e1.key,e2.key))

typedef struct{
 char key[KEY_SIZE];
}element;

element hash_table[TABLE_SIZE];

//해시 테이블 초기화
void init_table(element* ht){
 int i;
 for(i=0; i<TABLE_SIZE; i++){
  ht[i].key[0]=NULL;
 }
}

//문자로 된 탐색키를 숫자로 변환
int transform(char* key){
 int number = 0;
 while(*key)
  number += *key++;
 return number;
}
//제산 함수를 사용한 해싱 함수
int hash_function(char* key){
 return transform(key) % TABLE_SIZE;
}
//선형 조사법을 이용하여 테이블에 키를 삽입하고 테이블이 가득찬 경우는 종료
void hash_lp_add(element item,element* ht){
 int i,hash_value;
 hash_value = i =hash_function(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색키가 중복되었습니다\n");
   return;
  }
  i = (i+1) % TABLE_SIZE;
  if(i == hash_value){
   fprintf(stderr,"테이블이 가득 찼습니다\n");
   return;
  }
 }
 ht[i]=item;
}

//선형 조사법을 이용하여 테이블에 저장된 키를 탐색
void hash_lp_search(element item, element* ht){
 int i,hash_value;
 hash_value = i = hash_function(item.key);
 while(!empty(ht[i])){
  if(equal(item,ht[i])){
   fprintf(stderr,"탐색 성공 : 위치=%d\n",i);
   return;
  }
  i= (i+1) % TABLE_SIZE;
  if(i == hash_value){
   fprintf(stderr,"찾는 값이 테이블에 없음\n");
   return;
  }
 }
 fprintf(stderr,"찾는 값이 테이블에 없음\n");
}

//해싱 테이블의 내용을 출력
void hash_lp_print(element* ht){
 int i;
 for(i=0; i<TABLE_SIZE; i++)
  printf("[%d]   %s\n",i,ht[i].key);
}

main(){
 element tmp;
 int op;
 while(1){
  printf("원하는 연산을 입력(0=입력,1=탐색,2=종료)=");
  scanf("%d",&op);
  if( op == 2) break;
  printf("키를 입력하시오");
  scanf("%s",tmp.key);
  if( op == 0 )
   hash_lp_add(tmp,hash_table);
  else if(op == 1)
   hash_lp_search(tmp,hash_table);
  hash_lp_print(hash_table);
 }
}


블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

void shortestpath(int v, int cost[][MAX_VERTICES],int* distance,int n,short int* found)
{
 int i,u,w;
 for(i=0; i<n; i++){
  found[i]=FALSE;
  distance[i]=cost[v][i];
  }
 found[v]=TRUE;
 distance[v]=0;
 for(i=0; i<n-2; i++){
  u=choose(distance,n,found);
  found[u]=TRUE;
  for(w=0; w<n; w++)
   if(!found[w])
    if(distance[u]+cost[u][w]<distance[w]){
     distance[w]=distance[u]+cost[u][w];
     }
    }
}

int choose(int* distance,int n,short int* found){
 int i,min,minpos;
 min=INT_MAX;
 minpos = -1;
 for(i=0; i<n; i++)
  if(distance[i]<min && !found[i]){
   min=distance[i];
   minpos = i;
  }
  return minpos;
}


 

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

#define MAX_VERTICES 50
#define TRUE 1
#define FALSE 0
short intvidited[MAX_VERTICES];

typedef struct node* node_pointer;
typedef struct node{
 int vertex;
 struct node* link;
};
typedef struct queue* queue_pointer;
typedef struct queue{
 int vertex;
 queue_pointer link;
};
void addq(queue_pointer*, queue_pointer*,int);
int deleteq(queue_pointer*);

void bfs(int v){
 node_pointer w;
 queue_pointer front,rear;
 front=rear=NULL;

 printf("%5d",v);
 visited[v]=TRUE;
 addq(&front,&rear,v);
 while(front){
  v=deleteq(&front);
  for(w=graph[v]; w; w=w->link)
   if(visited[w->vertex]){
    printf("%5d",w->vertex);
    addq(&front,&rear,w->vertex);
    visited[w->vertex]=TRUE;
   }
 }
}


 

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,