hash simple linear

자료구조 2015. 5. 22. 15:26

#include <stdio.h>

#include <stdlib.h>

#define HASH_SIZE 10

int hash[HASH_SIZE];


int hf(int key)

{

return key%10;

}


void hashin(int k)

{

int f = hf(k);

int i;


if (hash[f] == -1)

{

hash[f] = k;

}

else

{

for (i= (f+1) % HASH_SIZE ; i != f ; i = (i+1) % HASH_SIZE )

{

if (hash[i] == -1)

break;

}


if (i ==f)

{// hash full

printf ("꽉참\n");

return;

}

else

hash[i] = k;


}

}


void search(int k)

{

int f = hf(k);

int i;


if (hash[f] == k)

{

printf ("있음\n");

}

else

{

for (i= (f+1) % HASH_SIZE ; i != f && hash[i] != -1 ; i = (i+1) % HASH_SIZE )

{

if (hash[i] == k)

break;

}


if (i ==f || hash[i] == -1)

{// hash full

printf ("없음\n");

return;

}

else

printf ("있음\n");

}

}



int main(void)

{

int a;

int i;


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

{

hash[i] = -1;

}


hashin(55);

hashin(16);

hashin(45);

hashin(33);

hashin(78);

hashin(88);

hashin(98);


search(33);

search(45);

search(1);


return 0;

}

'자료구조' 카테고리의 다른 글

simple adjacency list  (0) 2015.05.22
binary search tree ( insert, kth smallest, inorder, delete by key)  (0) 2015.05.15
link list(single)  (0) 2015.05.01
boolean combination  (0) 2015.05.01
Posted by rjh
,

#include <stdio.h>

#include <stdlib.h>


struct node

{

int key;

int k;

struct node* left;

struct node* right;

};

typedef struct node* TREE;




TREE makenode(int key);

TREE insert(TREE root, int key);

void insert2(TREE root, int key);


TREE deletenode(TREE root);

TREE deletekey(TREE root, int key);

TREE insertrightmost(TREE a, TREE b);


void inorder(TREE root);

void printk(TREE root,int k);//kth smallest


int main(void)

{

TREE root=NULL;

int i;

root = insert(root, 4);

root = insert(root, 2);

root = insert(root, 1);

root = insert(root, 3);


insert2(root,6);

insert2(root,5);

insert2(root,7);


inorder(root);

printf("\n");

//printk(root,1);

//printk(root,2);


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

printk(root,i+1);

//root = deletekey(root,4);

root = deletekey(root,7);

inorder(root);

}



TREE makenode(int key)

{

TREE temp = (TREE)malloc(sizeof(struct node));

temp->key=key;

temp->left=NULL;

temp->right=NULL;

temp->k= 1;

return temp;

}

TREE insert(TREE root, int key)

{

if (root == NULL){

return makenode(key);

}


if (root->key > key)

{

root->k++;

root -> left = insert(root->left,key);

}

else if (root->key < key)

{

root->right = insert(root->right,key);

}

return root;

}



void insert2(TREE root, int key)

{

if (root == NULL){

root = makenode(key);

return;

}


if (root->key > key){

if (root->left == NULL){

root->k++;

root->left = makenode(key);

return;

}

root->k++;

insert(root->left,key);

}

else if (root->key < key){

if (root->right == NULL){

root->right= makenode(key);

return;

}

insert(root->right,key);

}

}


TREE insertrightmost(TREE a, TREE b)

{

if (a == NULL)

return b;

a->right = insertrightmost(a->right,b);

return a;

}


TREE deletenode(TREE root)

{

TREE temp, lefttemp;

temp=root->right;

//temp->left = root->left;


if (root->right && root->right->left)

{

root->left = insertrightmost(root->left,root->right->left);

}


if (root->left)

temp->left = root->left;


if (temp && temp->right)

temp->right->left=NULL;

root = temp;

return root;

}


TREE deletekey(TREE root, int key)

{

if (root==NULL)

return NULL;

if (root->key == key)

{

root= deletenode(root);

}

else if (root->key > key)

{

root ->left = deletekey(root->left,key);

}

else //if (root -> key < key)

{

root ->right = deletekey(root->right,key);

}


return root;

}




void printk(TREE root,int k)

{

if (root->k == k)

{

printf ("%d\n",root->key);

}

else if (k > root->k)

{

printk(root->right, k - (root->k));

}

else

{

printk(root->left,k);

}


}


void inorder(TREE root)

{

if (root == NULL)

return;

inorder(root->left);

printf ("%d ",root->key);


inorder(root->right);

}


'자료구조' 카테고리의 다른 글

simple adjacency list  (0) 2015.05.22
hash simple linear  (0) 2015.05.22
link list(single)  (0) 2015.05.01
boolean combination  (0) 2015.05.01
Posted by rjh
,

link list(single)

자료구조 2015. 5. 1. 18:11

#include <stdio.h>

#include <stdlib.h>


struct _node{

int item;

struct _node* next;

};

typedef struct _node* NODE;


NODE createnode(int item);


void insert(NODE* list,NODE* tail, int item, NODE option(NODE* list, NODE* tail, NODE node) );


NODE insert_first (NODE* list, NODE* tail, NODE node);

NODE insert_last (NODE* list, NODE* tail, NODE node);

NODE insert_asc (NODE* list, NODE* tail, NODE node);

NODE insert_desc (NODE* list, NODE* tail, NODE node);

NODE insert_ith (NODE* list, NODE* tail, NODE node);


void delete_first(NODE* list,NODE* tail);

void delete_last(NODE* list, NODE* tail);

void delete_ith(NODE* list, NODE* tail);


void print_list(NODE list);



int main(void)

{

struct _node* list=NULL;

struct _node* tail=NULL;

int n,i,item;

FILE *fin=fopen("input.txt","r");




fscanf(fin,"%d",&n);

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

{

fscanf (fin,"%d",&item);

insert(&list, &tail, item, insert_desc);

}

print_list(list);


delete_first(&list,&tail);

delete_first(&list,&tail);

delete_first(&list,&tail);

delete_first(&list,&tail);

//delete_first(&list,&tail);


print_list(list);

fclose(fin);

return 0;

}

// 리스트 출력

void print_list(NODE list)

{

NODE temp=list;


while(temp!=NULL){

printf ("%d",temp->item);

temp=temp->next;}

printf ("\n");

}

// 노드만들기

NODE createnode(int item)

{

NODE temp=(NODE)malloc(sizeof(NODE*));


temp->item=item;

temp->next=NULL;


return temp;

}



// 노드추가

void insert(NODE* list,NODE* tail, int item, NODE option(NODE* list, NODE* tail, NODE node) )

{

NODE new_node=NULL;

new_node=createnode(item);


option(list,tail,new_node);

}

// 앞에

NODE insert_first(NODE* list, NODE* tail, NODE node)

{

node->next = *list;

*list=node;

return *list;

}

// 뒤에

NODE insert_last(NODE* list, NODE* tail, NODE node)

{

if ( (*list) == NULL)

{

*list=node;

*tail=node;

return *list;

}


(*tail)->next=node;

(*tail) = node;


return *list;

}

// i번째에

NODE insert_ith(NODE* list, NODE* tail, NODE node)

{

int i;

NODE prev= NULL;

NODE temp = (*list);


scanf ("%d",&i);


if ( (*list) == NULL)

{

*list=node;

*tail=node;

return *list;

}


while(temp != NULL)

{

i--;

if (i<0)

{


node->next = temp;

if (prev)

prev->next=node;


if (temp == *list)

*list=node;

//node->next = temp->next;

//temp->next = node;

break;

}

prev=temp;

temp=temp->next;

}


if (i>=0)

{

(*tail)->next=node;

(*tail) = node;

}


return *list;

}

// 오름차순

NODE insert_asc(NODE* list, NODE* tail, NODE node)

{

NODE temp=*list;

NODE prev=NULL;


if (temp == NULL)

{

*list=node;

*tail=node;


return *list;

}


while(temp !=NULL)

{

if (temp->item > node->item)

break;

prev = temp;

temp = temp->next;

}


if (temp != NULL)

node->next=temp;


if (prev != NULL)

prev->next=node;





//head, tail의 변경

if (temp == (*list))

*list = node;

if (prev == (*tail))

*tail = node;


return *list;

}

// 내림차순

NODE insert_desc(NODE* list, NODE* tail, NODE node)

{

NODE temp=*list;

NODE prev=NULL;


if (temp == NULL)

{

*list=node;

*tail=node;


return *list;

}


while(temp !=NULL)

{

if (temp->item < node->item)

break;

prev = temp;

temp = temp->next;

}


if (temp != NULL)

node->next=temp;


if (prev != NULL)

prev->next=node;





//head, tail의 변경

if (temp == (*list))

*list = node;

if (prev == (*tail))

*tail = node;


return *list;

}


void delete_first(NODE* list,NODE* tail)

{

NODE temp=*list;


*list=(*list)->next;

if (temp == *tail)

tail=NULL;


}

void delete_last(NODE* list, NODE* tail)

{

}

void delete_ith(NODE* list, NODE* tail)

{

NODE cur=*list;

NODE prev=NULL;

int i;

scanf ("%d",&i);


while(cur !=NULL)

{

if (i<=0)

{

if (prev == NULL)

{

(*list) = (*list)->next;

}

else

{

prev->next=cur->next;

}

if (cur == (*tail))

*tail=NULL;

break;

}

i--;

prev=cur;

cur=cur->next;

}


}

'자료구조' 카테고리의 다른 글

simple adjacency list  (0) 2015.05.22
hash simple linear  (0) 2015.05.22
binary search tree ( insert, kth smallest, inorder, delete by key)  (0) 2015.05.15
boolean combination  (0) 2015.05.01
Posted by rjh
,