HomeTOOLSEXAMPLESEXPLORE EMBEDDEDE CSERVICESECU SAMPLESRegistration
Embedded C programming Tutorial , Keil C ide , microsoftware.gr
Keil CRC and CAN BUS codes.
1. Shift led left
2.It's time for DAVE! <7/6/13>
3.Capture/Compare unit 6
4.ASC0-GPT1-MACROS
5.ASC0-FIFO-PEC
6.Analog converter
7.Memory manipulation routines
8. Recursion
9.Understanding interrupt priorities using CAPCOM2 module
10. POINTERS TO FUNCTION <4/7/13>,<4/28/13>
11.Memory models, memory types
12. The heap , part 1
13. The heap , part 2
14. The heap , part 3
15. Structure example
16. Nested structures, Array of structures.
17. Passing array of structures to function using pointers.<1/5/13>
18. Self Referential Structures
19. BITFIELDS
20. Linked list example
21. Circular linked list
22. Union example
23. Enumeration example
24. Watchdog timer example
25. Void pointer example <7/4/13>
26. The sieve of Eratosthenes
27. The stack
28. Union and bitfields as flags example. <6/23/13>
29. Look up table example. <8/11/13>
30. Seven segment display multiplexing -four digits with dot- example
31. LCD character display example - JHD162A
32. Hash table introduction example <8/27/14>
33. Array of Linked Lists example
34. Array of Linked lists-more functions included.
35. Hash table construction,searching and printing.
36. Fininte state machines- a first approach.
37. Finite state machines- two events example.
38. SPI port and an AT25128 serial eeprom hardware.
39. CRC CHECK
40. Definite Integral Calculator for Scientists, Engineers...
41 .Hamming distance of a CRC polynomial
42. Linux play starting.
43. Galois GF(2^4) Finite Field
44. Construct your own time triggered real time operating system.
45. CANBUS C CODE EXAMPLE.
35. Hash table construction,searching and printing.

Hash table construction,searching and printing example.

You, may be, imagined that when you try to do Hashing at an array throught a Hash function the job will be much more easier when you have your Hash function first ! and after it to construct the array where you will place your data.
Now we will do Hashing to an array of Linked Lists and our keys will be strings (names of people at the example).
Each one node of the linked lists will include the name of the people and his age.


 struct  node  {
char far data [20];
int age;
struct node far *next;
} s;


We will begin with a "good" Hash function first !
One is the:

/* treat strings as base-256 integers */
/* with digits in the range 1 to 255 */
//#define BASE (256)

unsigned long hash (const char *s )
{
    unsigned long m=5;
    unsigned long h;
    unsigned const char *us;

    /* cast s to unsigned const char * */
    /* this ensures that elements of s will be treated as having values >= 0 */
    us = (unsigned const char *) s;

     h = 0;
    while(*us != '\0') {
        h = (h * BASE + *us) % m;
        us++;
    }

    return h;
}


This function accepts a string and returns an integer. For example accepting "John" returns 4.
It is  "good"  when unsigned long m  integer is a long prime number and you have and some luck also. Then this function returns none or a few amount of collisions. Here we don't worry about collisions because we put our data as nodes of linked lists of an array and "on the fly".
There is a version of this function including multyplication instead of division, included at the C code.

Next we put our data as nodes at an array of linked lists using AppendNode() function
struct node far * AppendNode(struct node far **headRef, char* num , int w )

example: AppendNode(a[hash("John")],"John",20)  ;

saves person John having age 20  at linked list  a[hash("John")] of the array of linked lists.

In reverse, searching the list with search_in_list() function.
void  search_in_list(char* num, struct node **prev)

example:  search_in_list("John", a[hash("John")  ]);

At the end we print the linked list with  printlist()  function.
 void printlist( struct node **head )

 example: printlist (a[hash("John") ]);

Functions that we used are the functions of article No 34. Functions calls that are not in use are left as comments.

The C code                           Result at Hyperterminal



ps: No way to find the end of ...Hashing !!!


 

Home|TOOLS|EXAMPLES|EXPLORE EMBEDDEDE C|SERVICES|ECU SAMPLES|Registration