Question

In: Computer Science

How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This...

How to code the following function in C? (NOT C++)

int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle)

This function inserts a vehicle into the specified file.

• Determine the RBN using the driver's hash function.

• Use readRec to read the record at that RBN.

• If that location doesn't exist or the record at that location has a szVehicleId[0] == '\0': o Write this new vehicle record at that location using writeRec.

• If that record exists and that vehicle's szVehicleId matches, return RC_REC_EXISTS. (Do not update it.)

• Otherwise, return RC_SYNONYM. Note that in program #2, we will actually insert synonyms.

Solutions

Expert Solution

// CPP program to implement hashing with chaining 
#include<bits/stdc++.h> 
using namespace std; 

class Hash 
{ 
        int BUCKET; // No. of buckets 

        // Pointer to an array containing buckets 
        list<int> *table; 
public: 
        Hash(int V); // Constructor 

        // inserts a key into hash table 
        void insertItem(int x); 

        // deletes a key from hash table 
        void deleteItem(int key); 

        // hash function to map values to key 
        int hashFunction(int x) { 
                return (x % BUCKET); 
        } 

        void displayHash(); 
}; 

Hash::Hash(int b) 
{ 
        this->BUCKET = b; 
        table = new list<int>[BUCKET]; 
} 

void Hash::insertItem(int key) 
{ 
        int index = hashFunction(key); 
        table[index].push_back(key); 
} 

void Hash::deleteItem(int key) 
{ 
// get the hash index of key 
int index = hashFunction(key); 

// find the key in (inex)th list 
list <int> :: iterator i; 
for (i = table[index].begin(); 
                i != table[index].end(); i++) { 
        if (*i == key) 
        break; 
} 

// if key is found in hash table, remove it 
if (i != table[index].end()) 
        table[index].erase(i); 
} 

// function to display hash table 
void Hash::displayHash() { 
for (int i = 0; i < BUCKET; i++) { 
        cout << i; 
        for (auto x : table[i]) 
        cout << " --> " << x; 
        cout << endl; 
} 
} 

// Driver program 
int main() 
{ 
// array that contains keys to be mapped 
int a[] = {15, 11, 27, 8, 12}; 
int n = sizeof(a)/sizeof(a[0]); 

// insert the keys into the hash table 
Hash h(7); // 7 is count of buckets in 
                        // hash table 
for (int i = 0; i < n; i++) 
        h.insertItem(a[i]); 

// delete 12 from hash table 
h.deleteItem(12); 

// display the Hash table 
h.displayHash(); 

return 0; 
} 

Related Solutions

Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int x) { return (x * 3); } 2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers? 3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array. 4)...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT