Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. /*hashing by first letter of name order by alphabet*/
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #define SIZE 26
  6.  
  7. struct Data{
  8. char name[50];
  9. char phone[15];
  10. struct Data *next; //for chaining if there is collision
  11. };
  12.  
  13. typedef struct Data Dt; //struct Data alias Dt
  14. Dt *head[SIZE]; //26 head (hashing table with size 26)
  15.  
  16. int getMenu(){
  17. int menu;
  18. printf("Phone Book\n");
  19. printf("----------\n");
  20. printf("1. Add\n");
  21. printf("2. Search\n");
  22. printf("3. Display\n");
  23. printf("4. Delete\n");
  24. printf("5. Exit\n");
  25. printf("Choose menu: ");
  26. scanf("%d", &menu); getchar();
  27. return menu;
  28. }
  29.  
  30. int getHashKey(char name[]){
  31. char firstLetter = name[0];
  32. if( 'A' <= firstLetter && firstLetter <= 'Z') return firstLetter - 'A';
  33. if( 'a' <= firstLetter && firstLetter <= 'z') return firstLetter - 'a';
  34. return -1; //if first letter of name is not an alphabet, it's not valid
  35. }
  36.  
  37. void add(Dt in, int key){
  38. Dt *node;
  39. node = (Dt*) malloc(sizeof(Dt));
  40. *node = in; //copy values of one struct
  41. node->next = NULL;
  42. if(head[key] == NULL){
  43. head[key] = node;
  44. }
  45. else{ //collision
  46. Dt *curr; //find tail using curr
  47. curr = head[key];
  48. while( curr->next != NULL ){
  49. curr = curr->next;
  50. }
  51. curr->next = node; //put node after tail
  52. }
  53. }
  54.  
  55. void display(){
  56. Dt *curr;
  57. for(int i=0; i<SIZE; i++){
  58. printf("[%2d] ", i);
  59. curr = head[i];
  60. while(curr != NULL){
  61. printf("%s %s -> ", curr->name, curr->phone);
  62. curr = curr->next;
  63. }
  64. printf("\n");
  65. }
  66. printf("\n");
  67. }
  68.  
  69. void search(int key, char src[]){
  70. Dt *curr=head[key];
  71. if(head[key]==NULL){
  72. printf("Name Not Found!\n\n");
  73. }
  74. else if(head[key]!=NULL && strcmp(head[key]->name, src)==0){
  75. printf("%s %s\n",curr->name,curr->phone);
  76. }
  77. else{
  78. while(curr!=NULL){
  79. if(strcmp(curr->name,src)==0){
  80. printf("%s %s\n",curr->name,curr->phone);
  81. return;
  82. break;
  83. }
  84. curr=curr->next;
  85. }
  86. printf("Name Not Found!\n\n");
  87. }
  88.  
  89.  
  90. }
  91.  
  92. void del(int key, char src[]){
  93. int flag=0;
  94. int cnt=-1;
  95. Dt *curr=head[key];
  96. if(head[key]==NULL){
  97. printf("No Name to be deleted!\n\n");
  98. }
  99. else if(head[key]!=NULL && strcmp(head[key]->name,src)==0){
  100. head[key]=head[key]->next;
  101. free(curr);
  102. printf("%s's contact sucessfully deleted!\n",src);
  103. }
  104. else{
  105. Dt *temp;
  106. while(curr->next!=NULL){
  107. if(strcmp(curr->next->name,src)==0){
  108. temp=curr->next;
  109. free(temp);
  110. flag=1;
  111. printf("%s's contact sucessfully deleted!\n",src);
  112. }
  113. cnt++;
  114. curr=curr->next;
  115. }
  116. if(head[key]!=NULL && strcmp(curr->name,src)==0){
  117. temp=head[cnt];
  118. temp->next=NULL;
  119. free(curr);
  120. printf("%s's contact sucessfully deleted!\n",src);
  121. flag=1;
  122. }
  123. if(flag==0){
  124. printf("No name to be deleted!\n\n");
  125. }
  126. }
  127.  
  128. }
  129.  
  130. void removeAll(){
  131. Dt *curr;
  132. Dt *temp;
  133. while(curr!=NULL){
  134. temp=curr;
  135. curr=curr->next;
  136. free(temp);
  137. }
  138. }
  139.  
  140.  
  141. int main(){
  142. int menu;
  143.  
  144. do{
  145. menu = getMenu();
  146. if(menu == 1){ //add
  147. Dt in;
  148. printf("Name : ");
  149. scanf("%[^\n]", in.name); getchar();
  150. printf("Phone: ");
  151. scanf("%[^\n]", in.phone); getchar();
  152.  
  153. int key = getHashKey(in.name);
  154. if(key == -1){
  155. printf("Name is invalid\n\n");
  156. }
  157. else{
  158. add(in, key);
  159. printf("Success adding new contact\n\n");
  160. }
  161. }
  162. else if(menu == 2){ //search
  163. char src[50];
  164. printf("Name to Search: ");
  165. int key=getHashKey(src);
  166. scanf("%[^\n]",&src);getchar();
  167. search(key,src);
  168.  
  169. }
  170. else if(menu == 3){ //display
  171. display();
  172. }
  173. else if(menu == 4){ //delete
  174. char src[50];
  175. int key=getHashKey(src);
  176. printf("Name to delete: ");
  177. scanf("%[^\n]",&src);
  178. del(key,src);
  179. }
  180. else if(menu == 5){ //exit
  181. removeAll();
  182. }
  183. }while(menu != 5); //repeat menu while menu is not exit
  184.  
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement