-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashing.cpp
More file actions
167 lines (163 loc) · 4.38 KB
/
hashing.cpp
File metadata and controls
167 lines (163 loc) · 4.38 KB
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
162
163
164
165
166
167
//Implementation of direct access file using Hashing: linear probing with replacment and without replacment
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class student
{
int rollno;
char name[20];
int marks;
friend class Hashing;
};
class Hashing
{
public:
Hashing()
{
int i;
student e;
fstream file("data.txt",ios::app|ios::binary);
file.seekp(0,ios::end);
for(i=0;i<10;i++)
{
e.rollno=-1;
strcpy(e.name,"----");
e.marks=-1;
file.write((char*)&e,sizeof(e));
}
file.close();
}
void display_hashtable();
void linearprowithoutrep();
void linearprobingwithrep();
};
void Hashing::linearprobingwithrep()
{
student e;
int loc,i,newloc;
char ch;
fstream file("data.txt",ios::in|ios::out|ios::binary);
do{
fflush(stdin);
file.seekp(0,ios::beg);
cout<<"Enter rollno,name and marks";
cin>>e.rollno>>e.name>>e.marks;
loc=e.rollno%10;
student temp;
file.seekg((long int)loc*(sizeof(e)),ios::beg);
file.read((char*)&temp,sizeof(temp));
if(temp.rollno==-1)
{
cout<<"Inside first if";
file.seekp((long int)loc*sizeof(e),ios::beg);
file.write((char*)&e,sizeof(e));
}
else
{
newloc=temp.rollno%10;
if(newloc==loc)
{
for(i=(loc+1)%10;i!=loc;i=(i+1)%10)
{
file.seekg((long int)i*sizeof(e),ios::beg);
file.read((char*)&temp,sizeof(temp));
if(temp.rollno==-1)
{
cout<<"inside if";
file.seekp((long int)i*sizeof(e),ios::beg);
file.write((char*)&e,sizeof(e));
break;
}
}
}
else
{
student temp1=temp;
file.seekp(loc*sizeof(e),ios::beg);
file.write((char*)&e,sizeof(e));
for(i=(loc+1)%10;i!=loc;i=(i+1)%10)
{
file.seekg(i*sizeof(e),ios::beg);
file.read((char*)&temp,sizeof(temp));
if(temp.rollno==-1)
{
file.seekp(i*(sizeof(e)),ios::beg);
file.write((char*)&temp1,sizeof(temp1));
break;
}
}
}
if(i==loc) cout<<"Hash Table full";
}
file.flush();
display_hashtable();
cout<<"Do you want to continue";
cin>>ch;
}while(ch=='y');
}
void Hashing::linearprowithoutrep()
{
student e;
int loc,i;
char ch;
fstream file("data.txt",ios::in|ios::out|ios::binary);
do{
fflush(stdin);
file.seekp(0,ios::beg);
cout<<"Enter rollno,name and marks";
cin>>e.rollno>>e.name>>e.marks;
loc=e.rollno%10;
student temp;
file.seekg((long int)loc*(sizeof(e)),ios::beg);
file.read((char*)&temp,sizeof(temp));
if(temp.rollno==-1)
{
cout<<"Inside first if";
file.seekp((long int)loc*sizeof(e),ios::beg);
file.write((char*)&e,sizeof(e));
}
else
{
cout<<"Inside else";
for(i=(loc+1)%10;i!=loc;i=(i+1)%10)
{
file.seekg((long int)i*sizeof(e),ios::beg);
file.read((char*)&temp,sizeof(temp));
if(temp.rollno==-1)
{
cout<<"inside if";
file.seekp((long int)i*sizeof(e),ios::beg);
file.write((char*)&e,sizeof(e));
break;
}
}
if(i==loc) cout<<"Hash Table full";
}
file.flush();
display_hashtable();
cout<<"Do you want to continue";
cin>>ch;
}while(ch=='y');
}
void Hashing::display_hashtable()
{
int i;
student a;
cout<<"RollNo"<<"|"<<"Name"<<"|"<<"Marks"<<endl;
ifstream file("data.txt",ios::in|ios::binary);
while(file.read((char *)&a,sizeof(a)))
{
cout<<a.rollno<<"|"<<a.name<<"|"<<a.marks<<endl;
}
file.close();
}
int main()
{
Hashing h;
h.display_hashtable();
h.linearprowithoutrep();
h.display_hashtable();
h.linearprobingwithrep();
return 0;
}