File tree Expand file tree Collapse file tree 8 files changed +360
-0
lines changed
hashing/separate-chaining Expand file tree Collapse file tree 8 files changed +360
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include < iostream>
7
+ #include < string>
8
+ #include " hashtable.h"
9
+
10
+ using namespace std ;
11
+
12
+ int main ()
13
+ {
14
+ int id, choice, size;
15
+ string name;
16
+
17
+ cout << " Enter initial size of table : " ;
18
+ cin >> size;
19
+
20
+ HashTable table (size);
21
+
22
+ while (true )
23
+ {
24
+ cout << " 1.Insert a record \n " ;
25
+ cout << " 2.Search a record \n " ;
26
+ cout << " 3.Delete a record \n " ;
27
+ cout << " 4.Display table \n " ;
28
+ cout << " 5.Exit \n " ;
29
+ cout << " Enter your choice : " ;
30
+ cin >> choice;
31
+
32
+ if ( choice == 5 )
33
+ break ;
34
+
35
+ switch ( choice )
36
+ {
37
+ case 1 :
38
+ cout << " Enter student id : " ;
39
+ cin >> id;
40
+ cout << " Enter student name : " ;
41
+ cin >> name;
42
+ table.insertRecord (StudentRecord (id,name));
43
+ break ;
44
+ case 2 :
45
+ cout << " Enter a key to be searched : " ;
46
+ cin >> id;
47
+
48
+ if ( table.searchRecord (id) )
49
+ cout << " Key found \n " ;
50
+ else
51
+ cout << " Key not found \n " ;
52
+ break ;
53
+ case 3 :
54
+ cout << " Enter a key to be deleted : " ;
55
+ cin >> id;
56
+ table.deleteRecord (id);
57
+ break ;
58
+ case 4 :
59
+ table.displayTable ();
60
+ break ;
61
+ case 5 :
62
+ exit (1 );
63
+ break ;
64
+ default :
65
+ cout << " Wrong choice : \n " ;
66
+ }
67
+ }
68
+ }
69
+
70
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include < iostream>
7
+ #include " hashtable.h"
8
+
9
+ using namespace std ;
10
+
11
+ HashTable :: HashTable(int tableSize)
12
+ {
13
+ m = tableSize;
14
+ n = 0 ;
15
+ arr = new SingleLinkedList[m];
16
+ }
17
+
18
+ HashTable :: ~HashTable ()
19
+ {
20
+ delete[] arr;
21
+ }
22
+
23
+ int HashTable :: hash(int key)
24
+ {
25
+ return key % m;
26
+ }
27
+
28
+ bool HashTable :: searchRecord(int key)
29
+ {
30
+ int h = hash (key);
31
+
32
+ Node *p = arr[h].search (key);
33
+
34
+ if ( p != NULL )
35
+ {
36
+ cout << p->info ;
37
+ return true ;
38
+ }
39
+ return false ;
40
+ }
41
+
42
+ void HashTable :: insertRecord(StudentRecord newRecord)
43
+ {
44
+ int key = newRecord.getstudentId ();
45
+ int h = hash (key);
46
+
47
+ if ( searchRecord (key) )
48
+ {
49
+ cout << " Duplicate key\n " ;
50
+ return ;
51
+ }
52
+ arr[h].insertAtBeginning (newRecord);
53
+ n++;
54
+ }
55
+
56
+ void HashTable :: deleteRecord(int key)
57
+ {
58
+ int h = hash (key);
59
+
60
+ arr[h].deleteNode (key);
61
+ n--;
62
+ }
63
+
64
+ void HashTable :: displayTable()
65
+ {
66
+ for ( int i=0 ; i<m; i++ )
67
+ {
68
+ cout << " [" << i << " ] --> " ;
69
+
70
+ if ( ! arr[i].isEmpty () )
71
+ arr[i].displayList () ;
72
+ else
73
+ cout << " ___" << " \n " ;
74
+ }
75
+ }
76
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include < iostream>
7
+ #include " single.h"
8
+
9
+ using namespace std ;
10
+
11
+ SingleLinkedList::SingleLinkedList ()
12
+ {
13
+ start = NULL ;
14
+ }
15
+
16
+ SingleLinkedList::~SingleLinkedList ()
17
+ {
18
+ Node *p;
19
+ while ( start != NULL )
20
+ {
21
+ p = start->link ;
22
+ delete start;
23
+ start = p;
24
+ }
25
+ }
26
+
27
+ inline bool SingleLinkedList::isEmpty () const
28
+ {
29
+ return start == NULL ;
30
+ }
31
+
32
+ void SingleLinkedList::displayList () const
33
+ {
34
+ if ( isEmpty () )
35
+ return ;
36
+
37
+ Node *p = start;
38
+ while ( p != NULL )
39
+ {
40
+ cout << p->info << " " ;
41
+ p = p->link ;
42
+ }
43
+ cout << " \n " ;
44
+ }
45
+
46
+ Node* SingleLinkedList::search (int x) const
47
+ {
48
+ Node *p = start;
49
+
50
+ while ( p != NULL )
51
+ {
52
+ if ( p->info .getstudentId () == x )
53
+ break ;
54
+ p = p->link ;
55
+ }
56
+ return p;
57
+ }
58
+
59
+ void SingleLinkedList::insertAtBeginning (StudentRecord data)
60
+ {
61
+ Node *temp = new Node (data);
62
+ temp->link = start;
63
+ start = temp;
64
+ }
65
+
66
+ void SingleLinkedList :: deleteNode(int x)
67
+ {
68
+ if (start == NULL )
69
+ {
70
+ cout << " Value " << x << " not present\n " ;
71
+ return ;
72
+ }
73
+
74
+ /* Deletion of first node*/
75
+ if ( start->info .getstudentId () == x )
76
+ {
77
+ start = start->link ;
78
+ return ;
79
+ }
80
+
81
+ /* Deletion in between or at the end*/
82
+ Node *p = start;
83
+ while ( p->link != NULL )
84
+ {
85
+ if ( p->link ->info .getstudentId () == x )
86
+ break ;
87
+ p = p->link ;
88
+ }
89
+
90
+ if ( p->link == NULL )
91
+ cout << " Value " << x << " not present\n " ;
92
+ else
93
+ p->link = p->link ->link ;
94
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include < iostream>
7
+ #include < string>
8
+ #include " student.h"
9
+
10
+ using namespace std ;
11
+
12
+ StudentRecord :: StudentRecord()
13
+ { }
14
+
15
+ StudentRecord :: StudentRecord(int i, string name)
16
+ {
17
+ studentId = i;
18
+ studentName = name;
19
+ }
20
+
21
+ int StudentRecord :: getstudentId()
22
+ {
23
+ return studentId;
24
+ }
25
+
26
+ void StudentRecord :: setstudentId(int i)
27
+ {
28
+ studentId = i;
29
+ }
30
+
31
+ ostream& operator <<(ostream& out, const StudentRecord& st)
32
+ {
33
+ out << " " << st.studentId << " " << st.studentName <<" " ;
34
+ return out;
35
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include < iostream>
7
+ #include " single.h"
8
+ using namespace std ;
9
+
10
+ class HashTable
11
+ {
12
+ SingleLinkedList *arr;
13
+ int m; // size of the array
14
+ int n; // number of records
15
+ int hash (int key);
16
+
17
+ public:
18
+ ~HashTable ();
19
+ HashTable (int tableSize=11 );
20
+ void insertRecord (StudentRecord newRecord);
21
+ bool searchRecord (int key);
22
+ void deleteRecord (int key);
23
+ void displayTable ();
24
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include " student.h"
7
+
8
+ class Node
9
+ {
10
+ public:
11
+ StudentRecord info;
12
+ Node *link;
13
+
14
+ Node (StudentRecord i)
15
+ {
16
+ info = i;
17
+ link = NULL ;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+
7
+ #include " node.h"
8
+
9
+ class SingleLinkedList
10
+ {
11
+ Node *start;
12
+
13
+ public:
14
+ SingleLinkedList ();
15
+ ~SingleLinkedList ();
16
+ bool isEmpty () const ;
17
+ void displayList () const ;
18
+ Node* search (int x) const ;
19
+ void insertAtBeginning (StudentRecord data);
20
+ void deleteNode (int data);
21
+ };
22
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ #include < iostream>
7
+ using namespace std ;
8
+
9
+ class StudentRecord
10
+ {
11
+ int studentId;
12
+ string studentName;
13
+
14
+ public:
15
+ StudentRecord ();
16
+ StudentRecord (int i, string name);
17
+ int getstudentId ();
18
+ void setstudentId (int i);
19
+ friend ostream& operator <<(ostream& out, const StudentRecord& st);
20
+ };
You can’t perform that action at this time.
0 commit comments