Question

IN C++ Create a student hash table that contains information, studentID (int), name (string), marks_oop345 (float)....

IN C++

Create a student hash table that contains information, studentID (int), name (string), marks_oop345 (float). The size of hash table is equal to the number of students in the class. Use linear probing in case of collisions. Perform insertion, deletion, display and search operations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sample Output:

Code to be Copied:

//include required header files
#include<iostream>
#include<string>
using namespace std;
//Declare student record
struct student_record
{
   // structure for storing records
   int studentID;
   string name;
   float marks_oop345;
   //Display the record
   void displayrecord()
   {
       cout << "\nDisplaying search result: " << endl;
       cout << "Student Id = " << this->studentID << endl;
       cout << "Name = " << this->name << endl;
       cout << "Marks =" << this->marks_oop345 << endl;
   }
  
};

//Declare class to hash_node
class hash_node
{
//declare private mebers
public:
   //declare the id as key
   int student_id;
   student_record* value;
   //constructor
   hash_node(int key, student_record * r)
   {
       this->student_id = key;
       value = r;
   }
};

//Declare hash_class
class hashclass
{
   //declare data members
   //for table size and current_size
   int table_size;
   int curr_size;
   //declare pointer nodes
   hash_node ** h;
   hash_node *temp_node;

//member functions
public:
   hashclass(int n)
   {
       //set size to n
       table_size = n;
       curr_size = 0;
       //create hash_node
       h = new hash_node*[table_size];
       //set values to null
       for (int i = 0; i < table_size; i++)
       {
           h[i] = NULL;
       }
       //create a dummy node
       temp_node = new hash_node(-1, NULL);
   }

   //get the hash value
   int gethashcode(int k)
   {
       //apply hash function
       return k % table_size;
   }

   //Implement function to insert the record at
   //hash function code
   void insertion_record(int k, student_record* r)
   {
       //Create hash node
       hash_node* value = new hash_node(k, r);
       //get the index
       int hash_index = gethashcode(k);
       //use hile loop to check whether element is existed
       //at that index or not
       while (h[hash_index] != NULL &&
           h[hash_index]->student_id != k &&
           h[hash_index]->student_id != -1)
       {
           //increment hash size
           ++hash_index;
           // if empty deleted or non key then skip
           hash_index %= table_size;
       }
       //if the index is filled
       if (h[hash_index] == NULL ||
           h[hash_index]->student_id == -1)
       {
           //then increment the index
           //by using linear probing technique
           ++curr_size;
           //set the value
           h[hash_index] = value;
       }
   }
   //Implement the function
   //to delete the student record
   student_record * deleterecord(int delete_id)
   {
       //get the index of the student_id
       int pos = gethashcode(delete_id);
       //finding the hash_node with given key
       while (h[pos] != NULL)
       {
           //if hash_node is found
           if (h[pos]->student_id == delete_id)
           {
               hash_node* t = h[pos];
               //set the temp node
               h[pos] = temp_node;
               //increment current_size
               curr_size--;
               //return deleted value
               return t->value;
           }
           //increment index
           pos++;
           //again get hash code
           pos %= table_size;
       }
       //If not found return null
       return NULL;
   }

   //Implement method search_record
   student_record* search_record(int key)
   {
       // Apply hash function to find index for given key
       int hindex = gethashcode(key);
       int counter = 0;
       //finding the hash_node with given key
       while (h[hindex] != NULL)
       {
           int size = 0;
           //to avoid infinite loop
           if (size++ > table_size)
           {
               return NULL;
           }
           //if hash_node found return its value
           if (h[hindex]->student_id == key)
           {
               return h[hindex]->value;
           }
           //increment index
           hindex++;
           hindex %= table_size;
       }
       return NULL;
   }

   //Implement method to display values
   void display()
   {
       //print message
       cout << "\nDisplaying Hash Table\n";
       //use for-loop to iterate the table
       for (int i = 0; i < table_size; i++)
       {
           if (h[i] != NULL && h[i]->student_id != -1 && h[i]->student_id!=0)
           {
               cout << "Index= " << i
                   << " Student id= " << h[i]->value->studentID;
               cout << " Name= " << h[i]->value->name
                   << " Marks= "
                   << h[i]->value->marks_oop345 << endl;
           }
       }
   }
};

//Implement the method to read the record
student_record *getrecord(int id, string nam, float mar)
{
   //create object for record
   student_record * r = new student_record;
   //if record is null
   //then it is empty
   if (r == NULL)
   {
       cout << "Invalid data";
   }
   r->studentID = id;
   r->name = nam;
   r->marks_oop345 = mar;
   //return record
   return r;
}

//main method
int main()
{
   //declare local variables
   int option,studentID=0;
   float marks_oop345=0.0;
   string name="";
   //create table of size 50
   hashclass obj(50);
   do
   {
       //print menu
       cout << "\nHash Table Operations\n";
       cout << "1. Insertion\n2. Deletion\n3. Search\n4. Exit";
       //prompt and read the option
       cout << "\nEnter the option: ";
       cin >> option;
       //crete student record
       student_record *r = getrecord(studentID, name, marks_oop345);
       if (option == 1)
       {
           obj.insertion_record(r->studentID, r);
           cin.ignore();
           cout << "\nEnter the student name: ";
           getline(cin, name);
           cout << "\nEnter the student Id: ";
           cin >> studentID;
           cout << "\nEnter marks: ";
           cin >> marks_oop345;
           r = getrecord(studentID, name, marks_oop345);
       }
       //Implement else if condition for
       //delete the record
       else if (option == 2)
       {
           //prompt and read the student id
           cout << "Enter the student id to delete: ";
           cin >> studentID;
           //delete the record
           r = obj.deleterecord(studentID);
           //if return parameter is null
           if (r != NULL)
           {
               //display message
               cout << "Record is deleted\n" << endl;
           }
           else
           {
               //record not found
               cout << "Record not found" << endl;
           }
           cout << "After deleting the record: "<<endl;
           obj.display();
       }

       //Implement option for searching the record
       else if (option == 3)
       {
           obj.insertion_record(r->studentID, r);
           //Display records first
           obj.display();
           //prompt and read the id of the student
           cout << "Enter the student id to search: ";
           cin >> studentID;
          
           //search the record
           r = obj.search_record(studentID);
           if (r != NULL)
           {
               cout << "\nRecord found at index: " << obj.gethashcode(r->studentID);
               r->displayrecord();
           }
           else
           {
               cout << "Record not found" << endl;
           }          
       }
   }while(option != 4);
   //exit from the program
   if (option == 4)
   {
       cout << "End of the program....!" << endl;
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
IN C++ Create a student hash table that contains information, studentID (int), name (string), marks_oop345 (float)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *** C++ *** Create a class called Student that contains a first name ( string) and...

    *** C++ *** Create a class called Student that contains a first name ( string) and an student id number (type long).     1. Include a member function called get_data( ) to get data from the user for insertion into the object,     2. Also include a function called display_data( ) that displays the student data. Then write a complete program that uses the class to do some computation. Your main function, main( ), should create a Dynamic Array of...

  • create a hash table to implement spell checker in java 1. Create a file of 100...

    create a hash table to implement spell checker in java 1. Create a file of 100 words of varying length (max 8 characters). 2. All the words are in lower case. 3. design a hash table. 4. enter each word in the hash table. Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • Create a hash table class/struct.in C++ Define an array that holds 27 elements. Define a function...

    Create a hash table class/struct.in C++ Define an array that holds 27 elements. Define a function called Hash(int) -This function returns the modulo of that int by the size of the table (array). Define an add function that takes an integer. -This function takes the integer, determines the hash of that number by calling the above hash function, then adds it to the table using linear probing for collision resolution. Define a function that looks up a value, it takes...

  • Use C++ to create a class called Student, which represents the students of a university. A...

    Use C++ to create a class called Student, which represents the students of a university. A student is defined with the following attributes: id number (int), first name (string), last name (string), date of birth (string), address (string). and telephone (area code (int) and 7-digit telephone number(string). The member functions of the class Student must perform the following operations: Return the id numb Return the first name of the student Modify the first name of the student. . Return the...

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

  • struct Item { string name; int quantity; float cost; }; const int MAX_SIZE = 50; class...

    struct Item { string name; int quantity; float cost; }; const int MAX_SIZE = 50; class ManageInventory { public: ManageInventory() : count{0}, p_pInventoryItems {new Item*[size]} { } ManageInventory(int size) : size{size}, count{0}, p_pInventoryItems {new Item*[size]} { } ~ManageInventory(); void addItem(string name, int quantity, float cost); private: int size {MAX_SIZE}; int count; Item ** p_pInventoryItems; }; Write the definition for addItem. Use the new operator to dynamically create instances of type Item. Store pointers to inventory items in the inventoryItems array....

  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • You will create a class to keep student's information: name, student ID, and grade. The program...

    You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality: - The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change. - The program should provide the option to create a new entry with a student's name, ID, and grade. - There should be an option to lookup a student from his student ID (this will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT