DSA Lab 11

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Course: Data Structures and Algorithms

Lab Number: 11
Lab Title: Using ADT and Implement Hashing
Date: Dec 30, 2023
Lab Level Attempted: Advanced

Objectives:
1. Using C++ STL classes (List ADT) to store objects of user created data type (a
structure).
2. Implement Hashing in C++.
3. Hashing Strings.
4. Understanding Hashing with separate Chaining.
Description:
Lab 1:
1. We created a struct called “student”.
2. Using C++ list ADT(from STL) we created a list of struct “student”.
3. Challenge Task:
The list was sorted based on Roll Number using list function called “sort” which took
a user defined function as input.
Lab 2:
1. The given program was used as a reference.
2. Hashing was implemented on string data type.
3. Hashing Function used was changed a little bit from “Method 2” as on the given
inputs it didn’t gave uniform hashing.
Hashing Function used:
(key [0]+9∗key [1]+81∗key [2])%NoOfBuckets
Conclusion:
We were able to implement both the tasks and got required results. Outputs are shown in
the output section.
UML Class Diagrams

Hash
- noOfBuckets: int
- hashtable: list<string>*

+ Hash(buckets: int)
+ ~Hash()
+ insertString(key: string): void
+ deleteString(key: string): void
+ hashFunction(key: string): int
+ printTable(): void

<<struct>>
student
+ name: string
+ rollNumber: int
+ semester: int
+ gpa: double

Task 1
Structure Header File
#ifndef STRUCT_H_INCLUDED
#define STRUCT_H_INCLUDED
//Including Libraries
#include<iostream>
using namespace std;

//Structure for Student Information


struct student
{
//Student Name
string name;
//Student RollNumber
int rollNumber;
//Student Semester
int semester;
//Student GPA
double gpa;

};
//A utility function to print the structure;
void printStruct(student s1)
{

cout<<"|"<<left<<setw(10)<<s1.name<<"|"<<setw(12)<<s1.rollNumber<<"|"<<setw(9)<<s1.semester<<"
|"<<setw(5)<<s1.gpa<<"|"<<endl;
}
//Function to pass to list function "sort" to sort structure based on Roll Number
bool sortOnRollNumber(const student& s1,const student& s2)
{
return s1.rollNumber<s2.rollNumber;
}

#endif // STRUCT_H_INCLUDED

Main Function
#include <iostream>
#include <iostream>
//Including iomanip to use setfill function
#include <iomanip>
//Including C++ STL to use list adt
#include<list>
//Including struct Header File
#include"struct.h"
using namespace std;
//Function to print the list of student type
void printList(list<student> listOfStudents);
//Main Function
int main()
{
//Initializing a list of student struct type
list<student> listOfStudents;
//Storing the structures in a array to avoid manual input
student studentInfo[5];
//Struct 1
studentInfo[0]={"Ali",21,7,3.92};
//Struct 2
studentInfo[1]={"Fahad",45,3,3.92};
//Struct 3
studentInfo[2]={"Zakria",2,1,367};
//Struct 4
studentInfo[3]={"Ahsan",9,5,4};
//Struct 5
studentInfo[4]={"Saad",34,3,3.05};

//Inserting student structures into the list declared;


for(int i=0;i<5;i++)
{
//Declaring and defining the iterator of the list
auto iterator=listOfStudents.begin();
//Advancing the iterator
advance(iterator,i);
//Inserting the struct into the list
listOfStudents.insert(iterator,studentInfo[i]);
}
//Printing the original list of students as a table
cout<<"ORIGINAL LIST"<<endl;
printList(listOfStudents);

//Function to sort list based on Roll Number


listOfStudents.sort(sortOnRollNumber);

//Printing the sorted list of students as a table


cout<<"SORTED LIST"<<endl;
printList(listOfStudents);
}
//Function definition to print the list
void printList(list<student> listOfStudents)
{
//Initializing iterator to print student number
int i=0;
//line to print _ for Table Formating
cout<<setw(41)<<setfill('_')<<"_"<<endl;
//Printing Column names
cout<<"|"<<left<<setw(10)<<setfill(' ')<<"Name"<<"|"<<setw(12)<<"Roll
Number"<<"|"<<setw(9)<<"Semester"<<"|"<<setw(5)<<"GPA"<<"|"<<endl;
//line to print _ for Table Formating
cout<<setw(41)<<setfill('-')<<"-"<<setfill(' ')<<endl;
//Loop to print each student info
for(student studenttoPrint:listOfStudents)
{
//calling print function to print structure
printStruct(studenttoPrint);
}
//line to print _ for Table Formating
cout<<setw(41)<<setfill('-')<<"-"<<setfill(' ')<<endl;
}

Output
Task 2
Header File
#ifndef HASH_H
#define HASH_H
//including STL to use lists;
#include<list>
//including iostream to use basic function
#include<iostream>
using namespace std;
//Hash Class Declaration
class Hash
{
public:
/** Default constructor */
Hash(int buckets);
/** Default destructor */
~Hash();
/** Function to insert string */
void insertString(string key);
/** Function to delete string */
void deleteString(string key);
/** Hash Function to calculate index */
int hashFunction(const string & key);
/**Function to display the hash table*/
void printTable();
list<int> hello;
list<int> hey;
protected:

private:
int noOfBuckets; // Member variable "noOfBuckets"
list<string> *hashTable; // Member variable "*hashTable"
};

#endif // HASH_H

Implementation File
#include "Hash.h"
#include<iomanip>
#include<iostream>
#include<list>
using namespace std;
/** Default constructor */

Hash::Hash(int buckets)
{

//Initializing class member noOfBuckets to buckets


noOfBuckets=buckets;
//Dynamically allocating memory to array of lists to save strings
hashTable=new list<string>[buckets];
}
/** Default destructor */

Hash::~Hash()
{
//Deleting all lists
for(int i=0;i<noOfBuckets;i++)
{
hashTable[i].clear();
}
//De-allocating memory reserved for hashtable
delete hashTable;
}
/** Function to insert string */
void Hash::insertString(string key)
{
//Calculating index using hash function
int index=hashFunction(key);
//Declaring and defining iterator to place the key;
auto iterator=hashTable[index].begin();
//Advancing thie iterator to position of required index
advance(iterator,index);
//Inseting key into hash table
hashTable[index].insert(iterator,key);
}
/** Function to delete string */
void Hash::deleteString(string key)
{
//Calculating index using hash function
int index=hashFunction(key);
//Declaring and defining iterator to place the key;
auto iterator=hashTable[index].begin();
//Advancing thie iterator to position of required index
advance(iterator,index);
//Deleting key from hash table
hashTable[index].remove(key);
}
/** Hash Function to calculate index */
int Hash::hashFunction(const string & key)
{
//Calculating indev to insert key
return (key[0]+9*key[1]+81*key[2])%noOfBuckets;
/*Changed the hash function from 27 as that was not providing uniform hashing*/
}
/**Function to display the hash table*/
void Hash::printTable()
{
cout<<" ___"<<endl;;
//Loop to print hash table
for(int i=0;i<noOfBuckets;i++)
{
cout<<"|"<<left<<setw(3)<<i+1<<"|";
//Printing lists
for(string key : hashTable[i])
cout<<"--> "<<key;
cout<<endl;
}
cout<<" ---"<<endl;;
}

Main Function
#include <iostream>
//including Hash Header File
#include"Hash.h"

using namespace std;


//Main Function
int main()
{
//Declaring Hash Table for Data structures
Hash DataStructures(5);
//Hash Function used
cout<<"HASH FUNCTION USED"<<endl<<endl<<"(key[0]+9*key[1]+81*key[2])
%NoOfBuckets"<<endl<<endl;
//Inserting strings into hash table
DataStructures.insertString("Arrays");
DataStructures.insertString("Linked Lists");
DataStructures.insertString("Stacks");
DataStructures.insertString("Queues");
DataStructures.insertString("Tries");
DataStructures.insertString("Priority Queues");
DataStructures.insertString("Graphs");
DataStructures.insertString("Heaps");
DataStructures.insertString("Vector");
DataStructures.insertString("B+Trees");
DataStructures.insertString("Binary Trees");
DataStructures.insertString("AVL Trees");
cout<<"HASH TABLE BEFORE DELETION"<<endl<<endl;
//Printing the Hash Table
DataStructures.printTable();
//Removing some strings;
DataStructures.deleteString("Vector");
DataStructures.deleteString("Arrays");
DataStructures.deleteString("Binary Trees");

cout<<endl<<"HASH TABLE AFTER DELETION"<<endl<<endl;


//Printing the Hash Table after deleting elements
DataStructures.printTable();

Output

You might also like