Advertisement
sanpai

Hash Table Entry

Nov 4th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<string.h>
  4. #define SIZE 10
  5. struct emp
  6. {
  7. char name[20];
  8. float salary;
  9. int age;
  10. struct emp *ptr;
  11. };
  12.  
  13. struct emp *DICTIONARY[SIZE];
  14.  
  15.  
  16. void null()
  17. {
  18. int i;
  19. for(i=0;i<SIZE;i++)
  20. {
  21. DICTIONARY[i]=NULL;
  22. }
  23. }
  24.  
  25. int hash(int age)
  26.  
  27. {
  28. return(age%SIZE);
  29. }
  30.  
  31. void insert()
  32. {
  33. int n,i,age,key;
  34. float sal;
  35. char name[20];
  36. printf("Enter the number of Employees");
  37. scanf("%d",&n);
  38. struct emp *newnode,*head;
  39.  
  40. for(i=0;i<n;i++)
  41. {
  42. printf(" \n Enter the age of the employee : ");
  43. scanf("%d",&age);
  44. key=hash(age);
  45. printf("\n Enter the name of the employee : ");
  46. scanf("%s",name);
  47. printf("\n enter the salary of the employee : ");
  48. scanf("%f",&sal);
  49.  
  50. head=DICTIONARY[key];
  51. newnode=(struct emp *)malloc(sizeof(struct emp));
  52. strcpy(newnode->name,name);
  53. newnode->age=age;
  54. newnode->salary=sal;
  55. newnode->ptr=head;
  56. DICTIONARY[key]=newnode;
  57. }
  58. }
  59.  
  60. void display()
  61. {
  62. int age,key;
  63. float sum=0,average;
  64. char name[20];
  65. struct emp *temp;
  66. printf("\n Enter the age of the employee to be searched : ");
  67. scanf("%d",&age);
  68. key=hash(age);
  69. printf("\n Enter the employee name to be searched : ");
  70. scanf("%s",name);
  71. temp=DICTIONARY[key];
  72.  
  73. while(temp!=NULL)
  74. {
  75. if(strcmp(temp->name,name)==0)
  76. break;
  77. else
  78. temp=temp->ptr;
  79. }
  80.  
  81. if(temp==NULL)
  82. {
  83. printf("\n Employee not found ");
  84. }
  85.  
  86. else
  87.  
  88. {
  89.  
  90. printf("\n the employee found");
  91. printf("\n Name : %s",temp->name);
  92. printf("\n age : %d",temp->age);
  93. printf("\n salary: %f",temp->salary);
  94. }
  95.  
  96.  
  97.  
  98. }
  99.  
  100.  
  101. int main()
  102. {
  103.  
  104. null();
  105. insert();
  106. display();
  107. 1,1 Top
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement