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...
please explain how does the following C code work. a) int function1(int num1, int num2) {...
please explain how does the following C code work. a) int function1(int num1, int num2) { int num = num1 ^ num2; int result = 0; while(num > 0) { result += (num & 1); num >>= 1; } return result; } b) int function2(unsigned int num) { return num & ~(num - 1); } c) int f1(unsigned int x) { int count = 0; while(x) { count++; x = x&(x-1); } return count; } d) double ldexp(double x, int...
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;
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
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)...
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:" <<...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT