-
Notifications
You must be signed in to change notification settings - Fork 0
/
GROUP-A-1.cpp
164 lines (149 loc) · 4.18 KB
/
GROUP-A-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
SAYALI RAHANE
CLASS- SE A
BATCH- C50
GROUP-A-1.Consider telephone book database of N clients. Make use of a hash table
implementation to quickly look up client‘s telephone number. Make use of two collision
handling techniques and compare them using number of comparisons required to find a set of
telephone numbers.*/
//Header Files
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
int v;
node* next;
}*HashTable[10]; //create hash table of size 10
class hashing
{
public:
hashing() // constructor of hashing class
{
for(int i=0 ; i<10 ; i++) //initializaton
{
HashTable[i]=NULL; // assign null value to hash table
}
}
int HashFunction(int v)
{
return (v%10); //division method used
}
node* create_node(int x) // create node
{
node* temp=new node;
temp->next=NULL;
temp->v=x;
return temp;
}
void display()
{
for(int i=0 ; i< 10; i++)
{
node * temp=new node;
temp=HashTable[i];
cout<<"a["<<i<<"] : ";
while(temp !=NULL)
{
cout<<" ->"<<temp->v;
temp=temp->next;
}
cout<<"\n";
}
}
//for searching
int searchElement(int v)
{
bool flag = false;
int hash_val = HashFunction(v);
node* entry = HashTable[hash_val];
cout<<"\nElement found at : ";
while (entry != NULL)
{
if (entry->v==v)
{
cout<<hash_val<<" : "<<entry->v<<endl;
flag = true;
}
entry = entry->next;
}
if (!flag)
return -1;
}
//for deletion
void deleteElement(int v)
{
int hash_val = HashFunction(v); //find the index of v using hashfunction
node* entry = HashTable[hash_val];
if (entry == NULL ) // no element is present
{
cout<<"No Element found ";
return;
}
if(entry->v==v)
{
HashTable[hash_val]=entry->next; //link pointer to next
return;
}
while ((entry->next)->v != v)
{
entry = entry->next;
}
entry->next=(entry->next)->next;
}
//for insertion
void insertElement(int v)
{
int hash_val = HashFunction(v); //index of v using hashfunction
node* temp=new node; //create temporary node
node* head=new node; //create head node
head = create_node(v); //create node to be inseted as head node
temp=HashTable[hash_val];//position element in hashtable
if (temp == NULL)
{
HashTable[hash_val] =head; //if temp null then insert head node in that position
}
else
{
while (temp->next != NULL) //traverse the hashtable until we get null
{
temp = temp->next;
}
temp->next =head; // when found null then insert head node in that position
}
}
};
int main()
{
int ch; //take choice
int data,search,del;
hashing h; //object
do
{
cout<<"\nTelephone : \n1.Insert \n2.Display \n3.Search \n4.Delete \n5.Exit";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter phone no. to be inserted : ";
cin>>data;
h.insertElement(data);
break;
case 2:h.display();
break;
case 3:cout<<"\nEnter the no to be searched : ";
cin>>search;
if (h.searchElement(search) == -1)
{
cout<<"No element found at key ";
continue;
}
break;
case 4:cout<<"\nEnter the phno. to be deleted : ";
cin>>del;
h.deleteElement(del);
cout<<"Phno. Deleted"<<endl;
break;
}
}while(ch!=5);
return 0;
}