0% found this document useful (0 votes)
9 views6 pages

Assignment No-1

The document outlines an assignment focused on implementing a telephone book database using a hash table for efficient client telephone number lookups. It covers the concepts of hashing, operations like insertion and searching in a hash table, and includes examples of how to code these operations in C++. The assignment aims to enhance understanding of object-oriented programming and the practical application of hash tables.

Uploaded by

nerkardigambar8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Assignment No-1

The document outlines an assignment focused on implementing a telephone book database using a hash table for efficient client telephone number lookups. It covers the concepts of hashing, operations like insertion and searching in a hash table, and includes examples of how to code these operations in C++. The assignment aims to enhance understanding of object-oriented programming and the practical application of hash tables.

Uploaded by

nerkardigambar8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment No-1 (Group-A)

Title:

Consider telephone book database of N clients. Make use of a hash table implementation to quickly
look up client‘s telephone number

Objectives:

1. To understand concept of Hashing


2. To understand to find record quickly using hash function.
3. To understand concept & features of object oriented programming.

Learning Objectives
 To understand concept of hashing.
 To understand operations like insert and search record in the database.

Learning Outcome
 Learn object oriented Programming features
 Understand & implement concept of hash table .

Theory:
Hash tables are an efficient implementation of a keyed array data structure, a structure
sometimes known as an associative array or map. If you're working in C++, you can take
advantage of the STL map container for keyed arrays implemented using binary trees, but this
article will give you some of the theory behind how a hash table works.

Keyed Arrays vs. Indexed Arrays

One of the biggest drawbacks to a language like C is that there are no keyed arrays. In a
normal C array (also called an indexed array), the only way to access an element would be
through its index number. To find element 50 of an array named "employees" you have to
access it like this:
1employees[50];

In a keyed array, however, you would be able to associate each element with a "key," which can
be anything from a name to a product model number. So, if you have a keyed array of employee
records, you could access the record of employee "John Brown" like this:

1employees["Brown, John"];

One basic form of a keyed array is called the hash table. In a hash table, a key is used to find
an element instead of an index number. Since the hash table has to be coded using an indexed
array, there has to be some way of transforming a key to an index number. That way is called
the hashing function.

Hashing Functions

A hashing function can be just about anything. How the hashing function is actually coded
depends on the situation, but generally the hashing function should return a value based on a
key and the size of the array the hashing table is built on. Also, one important thing that is
sometimes overlooked is that a hashing function has to return the same value every time it is
given the same key.

Let's say you wanted to organize a list of about 200 addresses by people's last names. A hash
table would be ideal for this sort of thing, so that you can access the records with the people's
last names as the keys.

First, we have to determine the size of the array we're using. Let's use a 260 element array so
that there can be an average of about 10 element spaces per letter of the alphabet.>

Now, we have to make a hashing function. First, let's create a relationship between letters and
numbers:

A --> 0
B --> 1
C --> 2
D --> 3
...
and so on until Z --> 25.

The easiest way to organize the hash table would be based on the first letter of the last name.

Since we have 260 elements, we can multiply the first letter of the last name by 10. So, when a
key like "Smith" is given, the key would be transformed to the index 180 (S is the 19 letter of the
alphabet, so S --> 18, and 18 * 10 = 180).
Since we use a simple function to generate an index number quickly, and we use the fact that the
index number can be used to access an element directly, a hash table's access time is quite small.
A linked list of keys and elements wouldn't be nearly as fast, since you would have to search
through every single key-element pair.

Basic Operations

Following are the basic primary operations of a hash table.

• Search − Searches an element in a hash table.


• Insert − inserts an element in a hash table.
• delete − Deletes an element from a hash table.

DataItem

Define a data item having some data and key, based on which the search is to be conducted in a
hash table.
struct DataItem
{
int data;
int key;
};

Hash Method

Define a hashing method to compute the hash code of the key of the data item.

int hashCode(int key){


return key % SIZE;
}

Search Operation

Whenever an element is to be searched, compute the hash code of the key passed and locate the
element using that hash code as index in the array. Use linear probing to get the element ahead if
the element is not found at the computed hash code.

Example
struct DataItem *search(int key)
{
//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

Insert Operation

Whenever an element is to be inserted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing for empty location, if an
element is found at the computed hash code.

Example
void insert(int key,int data)
{
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL &&
hashArray[hashIndex]->key != -1) { //go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

hashArray[hashIndex] = item;
}

Delete Operation

Whenever an element is to be deleted, compute the hash code of the key passed and locate the index
using that hash code as an index in the array. Use linear probing to get the element ahead if an
element is not found at the computed hash code. When found, store a dummy item there to keep the
performance of the hash table intact.

Example
struct DataItem* delete(struct DataItem* item) {
int key = item->key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] !=NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

Expected Output

Menu
1.Create Telephone book
2.Display
3.Look up
Enter Choice1
how many entries2
enter Name a
enter number1234567890
enter Name d
enter number3216549876

do u want to continue?(1 for continue)1

Menu
1. Create Telephone book
2. Display
3.Look up
Enter Choice2

d 3216549876
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
a 1234567890
0
0
do u want to continue?(1 for continue)1

Menu
1.Create Telephone book
2.Display
3.Look up
Enter Choice3
enter Name to searchd
found at 0
no of comparision1
do u want to continue?(1 for continue)0*/

Conclusion: In this way we have implemented Hash table for quick lookup using C++.
Questions:-.

1. Explain the different hashing technique?

2. Define Hashing? Explain collision resolution technique with example

You might also like