Advertisement
Guest User

Hash

a guest
Oct 22nd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. // HashingFunctions.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. typedef struct
  11. {
  12.     char name[20];
  13.     int facultyNumber;
  14.     Student next;
  15. } Student;
  16.  
  17. Student *students[1024];
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     *students = (Student *)malloc(1024 * sizeof(Student *));
  22.     Student newStudent;
  23.     strcpy(newStudent.name,"Pesho");
  24.     newStudent.facultyNumber = 2012123;
  25.     if(search(newStudent.facultyNumber) != NULL)
  26.     {
  27.         printf("Found!");
  28.     }
  29.     else
  30.     {
  31.         printf("Not found!");
  32.     }
  33.  
  34.     return 0;
  35. }
  36.  
  37. Student * search(int facultyNumber)
  38. {
  39.     int currentStudentPositionInArray = returnHashCode(facultyNumber);
  40.     for(Student *current = students[currentStudentPositionInArray]; current!=NULL; current = current->next)
  41.     {
  42.         if(current ->facultyNumber == facultyNumber)
  43.         {
  44.             return current;
  45.         }
  46.     }
  47.     return NULL;
  48. }
  49.  
  50. int returnHashCode(int key)
  51. {
  52.     int hash = (int)(key % 1024);
  53.     return hash;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement