Review Notes 2 - HashTable Programming

Review Notes 2 - HashTable Programming - KEYTYPE k; RECTYPE...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Hash -- Programming #define TABLESIZE 997 typedef int KEYTYPE; typedef int RECTYPE; struct record { KEYTYPE k; RECTYPE r; } table[TABLESIZE]; int search( KEYTYPE key, RECTYPE rec) { int i, count=0; i = hash(key); /* hash the key */
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 i = rehash(i, ++count); /* rehash */ if ( table[i].k == nullkey ) { /* insert the record into the empty position */ table[i].k = key; table[i].r = rec; } /* end if */ return (i); } /* end search */ int rehash ( int i, int count ) { return ( i + count*count ); /* Quadratic Probing */ }
Background image of page 2
3 struct nodetype {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: KEYTYPE k; RECTYPE r; struct nodetype *next; } typedef struct nodetype *NODEPTR; NODEPTR bucket[HashSize]; struct nodetype *search( KEYTYPE key, RECTYPE rec) { struct nodetype *p, *q, *s; int i; i = hash(key); q = NULL; p = bucket[i]; 4 while ( p != NULL && p->k != key ) { q = p; p = p->next; } /* end while */ if ( p->k == key ) return (p); /* insert a new record */ s = getnode(); s->k = key; s->r = rec; s->next = NULL; 5 if ( q == NULL ) bucket[i] = s; else q->next = s; return (s); } /* end search */...
View Full Document

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 5

Review Notes 2 - HashTable Programming - KEYTYPE k; RECTYPE...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online