Вы находитесь на странице: 1из 14

Telephone Directory

TELEPHONE DIRECTORY
1.Introduction
In computer science, a data structure is a particular way of organizing data in a computer so
that it can be used efficiently. Different kinds of data structures are suited to different kinds of
applications, and some are highly specialized to specific tasks.
Data structures provide a means to manage large amounts of data efficiently, such as large
databases and internet indexing services. Usually, efficient data structures are a key in designing
efficient algorithms. Some formal design methods and programming languages emphasize data
structures, rather than algorithms, as the key organizing factor in software design. Storing and
retrieving can be carried out on data stored in both main memory and in secondary memory.
Data structures are generally based on the ability of a computer to fetch and store data at any
place in its memory, specified by a pointer-a bit string, representing a memory address that can be
itself stored in memory and manipulated by the program. Thus, the array and record data structures
are based on computing the addresses of data items with arithmetic operations; while the linked data
structures are based on storing addresses of data items within the structure itself. Many data
structures use both principles, sometimes combined in non-trivial ways (as in XOR linking).
The implementation of a data structure usually requires writing a set of procedures that
create and manipulate instances of that structure. The efficiency of a data structure cannot be
analysed separately from those operations. This observation motivates the theoretical concept of an
abstract data type, a data structure that is defined indirectly by the operations that may be performed
on it, and the mathematical properties of those operations (including their space and time cost).

2.Project Description
The appropriate data structure used for this project is hashing.
In computing, a hash table (also hash map ) is a data structure used to implement an associative
array, a structure that can map keys to values. A hash table uses a hash function to compute
an index into an array of buckets or slots, from which the correct value can be found[3].

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

A telephone directory, also known as a telephone book, telephone address book, Phone book or the
white/yellow pages, is a listing of telephone subscribers in a geographical area or subscribers to
services provided by the organisation that publishes the directory. Its purpose is to allow the
telephone number of a subscriber identified by name and address to be found.
2.1 Creating Hash Table To Store Data Basing Name As Key
To Create Hash table to store the data of names, telephone numbers based on name as a key.
In this module we will insert the data. We will insert the name of the person and the phone number
related to that person. We create a hash table to store the data. We use insert function and in that
function we will call the hash function. We declare the hash table also.
2.2 Creating Hash Table To Store Data Basing Number As Key
To Create Hash table to store the data of names, telephone numbers based on telephone number as a
key.
In this module we will write the logic to find the phone number when name was asked. It means
when we give the name to be searched it has to search the name and display both the name and the
phone number related to that person.
2.3 Searching, Displaying And Deleting Basing On Name
To search the hash table based on query for name. In this module we will the logic to find the name
when the phone number related to that person was given. When we give a phone number to search it
has to search for that phone number and display both the phone number and name.
Ex: the following are the entries of telephone directory.
Sai kiran
912399482
Satish
949278583
Sarath
0920542939
Surendar
9349294845
If the user queries for sai* then it should display sai kiran records.
2.4 Searching, Displaying And Deleting Basing On Phone Number
To search ,delete and to display the hash table based on query for phone number.
Ex:for the above given data if the user queries for 949278583* then it should display name satish.
3.Algorithm
1. Create a Hashtable and its node consisting of Telephone number and name.
KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

2. Hash Function is: int hf(char *s)


{
int i,sum=0;
for(i=0;s[i]!='\0';i++)
sum=sum+s[i]*(i+1);
return (sum*1/2)%tablesize; or return(sum*1/4)%tablesize
}
3.Insert values into hashtable by this hash function.
4.Search the names by using numbers and numbers using names.

4.Source Code
#include<stdio.h>

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define tablesize 502
#define tablesize1 503
struct hash *hashtable=NULL;
struct hash *hashtable1=NULL;
struct node
{
char name[50];
char telnum[15];
struct node *next;
};
struct hash
{
struct node *head;
int pcount;
};
struct node *createnode(char *name, char *telnum)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));

strcpy(newnode->telnum,telnum);
strcpy(newnode->name,name);
newnode->next=NULL;
return newnode;
}
int hf(char *s)
{
int i,sum=0;
for(i=0;s[i]!='\0';i++)

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

sum=sum+s[i]*(i+1);
return (sum*1/2)%tablesize;
}
int hf1(char *t)
{
int i,sum=0;
for(i=0;t[i]!='\0';i++)
sum=sum+t[i]*(i+2);
return (sum*1/4)%tablesize1;
}
void insert(char *name, char *telnum)
{
int hashindex=hf(name);
struct node *newnode=createnode(name,telnum);
if(!hashtable[hashindex].head)
{
hashtable[hashindex].head=newnode;
return;
}
newnode->next=(hashtable[hashindex].head);
hashtable[hashindex].head=newnode;
return;
}
void insert1(char *name, char *telnum)
{
int hashindex1=hf1(telnum);
struct node *newnode=createnode(name,telnum);
if(!hashtable1[hashindex1].head)
{
hashtable1[hashindex1].head=newnode;
return;
}

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

newnode->next=(hashtable1[hashindex1].head);
hashtable1[hashindex1].head=newnode;
return;
}

void searchinhash(char *name)


{
int hashindex=hf(name);
struct node *mynode=NULL;
mynode=hashtable[hashindex].head;
if(mynode==NULL)
{
printf("%s",mynode->name);
printf("not found");
getch();
return;
}
while(strcmp(mynode->name,name)!=0)
{
mynode=mynode->next;
if(mynode==NULL)
break;
}
if(mynode!=NULL)
{
printf("name:%s\n",mynode->name);
printf("num:%s",mynode->telnum);
}
else
{
printf("not found");

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

}
getch();
}
void searchinhash1(char *telnum)
{
int hashindex1=hf1(telnum);
struct node *mynode=NULL;
mynode=hashtable1[hashindex1].head;
if(mynode==NULL)
{
printf("%s",mynode->telnum);
printf("not found");
getch();
return;
}
while(strcmp(mynode->telnum,telnum)!=0)

{
mynode=mynode->next;
if(mynode==NULL)
break;
}
if(mynode!=NULL)
{
printf("name:%s\n",mynode->name);
printf("num:%s",mynode->telnum);
}
else
{
printf("not found");
}
getch();

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

}
void display(hashtable H)
{
position p;
list l;
int i; int j=0;

for(i=0;i<H->tablesize;i++)
{
l=H->lists[i];
p=l->nextnode;
while(p!=NULL)
{
printf("\n The name and telnum at[%d][%d] is:\t%s \t%s",i,j,p->name,p->telnum);
p=p->nextnode;
j++;
}
}
}
void main()
{
int ch,ch1,ch2;
char name[30],tel[30],search[30],search1[15];
clrscr();
while(1)
{
printf(" TELEPHONE DIRECTORY");
printf("\n____________________");
printf(" \n1. name search \n 2.number search \n 3.back up the file \n 4.display \n 5. exit \n ");
printf(" enter ur choice");
scanf("%d",&ch);
if(ch==1)

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

{
printf("\n 1.add \n 2.search by name \n 3.exit");
printf(" \n enter ur choice");
scanf("%d",&ch1);
switch(ch1)
{
case 1:

printf("enter name");

scanf("%s",name);
printf("enter telephone number");
scanf("%s",tel);
insert(name,tel);
break;
case 2:
printf("enter name to search");
scanf("%s",search);
searchinhash(search);
break;
case 3:exit(0);
}
}
if(ch==2)
{
printf("\n 1.add \n 2.search by ph num \n 3.exit");
printf("enter ur choice ");
scanf("%d",&ch2);
switch(ch2)
{
case 1:

printf("enter name");

scanf("%s",name);
printf("enter telephone number");
scanf("%s",tel);
insert1(name,tel);

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

10

break;
case 2:
printf("enter telephone num to search");
scanf("%s",search1);
searchinhash1(search1);
break;
case 3:Take_The_Backup_of_Hashtable(H);
printf("\n The Backup was sucussesfull");
break;

case 4:display(H); break;


case 5:exit(0);
}
}
if(ch==5)
exit(0);
}

5.Snapshots Of Outputs

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

11

Figure.5.1 Output 1

Figure.5.2 Output 2

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

12

Figure.5.3 Output 3

Figure.5.4 Output 4

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

13

6.Conclusion
A telephone directory is a listing of telephone subscribers in a geographical area or subscribers to
services provided by the organization that publishes the directory. Its purpose is to allow the
telephone number of a subscriber identified by name and address to be found. This project helps in
using Telephone Directory in an efficient way. We can easily search the contacts by using hash
table. The given contacts are displayed easily by this program. We can search, modify, display and
delete the contacts either by searching name or by searching number.

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING

Telephone Directory

14

7..References
http://stackoverflow.com/questions/17805842/hash-table-with-linked-list-with-bug
http://www.dreamincode.net/forums/topic/259953-hash-table-with-linked-lists/
http://en.wikipedia.org/wiki/Computer_science
http://en.wikipedia.org/wiki/Telephone_directory
http://en.wikibooks.org/wiki/Data_Structures/Hash_Tables#Choosing_a_good_hash_function

KL UNIVERSITY

DEPARTMENT OF COMPUTER SCIECE & ENGINEERING