In: Computer Science
convert this string type program into interger data type
#include "stdafx.h"
#include
#include
unsigned int putIntoHashTable(char *ptrInputData, unsigned int
bufferLength); // function to add to hash table
unsigned int getFromHashTable(char *ptrOutputData, unsigned int
bufferLength); // function to retrieve data from hash table
#define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value)
#define HASH_SIZE 100 // size of hash table to be used (for testing I suggest making this number much lower)
// data
structure used to keep track of hashed data
struct myHashStruct {
char *ptrBuffer = NULL; // pointer to data stored in
hash (you will need to malloc space for string to be stored)
struct myHashStruct *ptrNextHashData = NULL; //
pointer to next item in this hash bucket (or NULL if no more)
};
struct myHashStruct *myHash[HASH_SIZE]; // create an empty hash table structure (note this is basically an arrary of linked list heads)
int main()
{
char inputBuffer[INPUT_BUFFER_SIZE];
// initialize the hash table to empty one
for (int i = 0; i < HASH_SIZE; i++)
{
if ((myHash[i] = (struct
myHashStruct *)calloc(1, sizeof(struct myHashStruct))) ==
NULL)
{
printf("calloc
failed!\n");
return(-1);
}
}
// add to hash table loop
while (1)
{
printf("enter data to be added to
hash table or exit when done\n");
// get strings from the console
and place in hash until nothing entered
scanf_s("%s", inputBuffer,
INPUT_BUFFER_SIZE);
// stop adding data into hash
table when "exit" is entered
if (strcmp(inputBuffer, "exit") ==
0)
break;
if
(putIntoHashTable(inputBuffer, strlen(inputBuffer)) ==
HASH_SIZE)
printf("Error
putting into hash table\n");
}
// check if data is in hash table
while (1)
{
unsigned int hashIndexNumber =
0;
printf("Enter data to find, done when complete\n");
// get strings from the console
and check if in hash table
scanf_s("%s", inputBuffer,
INPUT_BUFFER_SIZE);
// stop adding data into hash
table when "done" is entered
if (strcmp(inputBuffer, "done") ==
0)
break;
if ((hashIndexNumber =
getFromHashTable(inputBuffer, strlen(inputBuffer))) ==
HASH_SIZE)
printf("%s not
found in hash table\n", inputBuffer);
else
printf("%s found
in hash table at %u\n", inputBuffer, hashIndexNumber);
}
return 0;
}
// FUNCTION : myHashFunction
// DESCRIPTION :
// Calculate a hash value to use in storing the data into the hash
table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash
table
//
// RETURNS :
// Hash value calculated ( HASH_SIZE on failure)
unsigned int myHashFunction(char *ptrInputBuffer)
{
unsigned int calculatedHashValue = 0;
// add code to create a hashed value here!!!
for (int i = 0; i < HASH_SIZE; i++) // here string
can be add based on hash value
{
calculatedHashValue += ptrInputBuffer[i]; // hash value will be
added in buffer of size i
}
printf("Hash Calculated to be :%d \n", calculatedHashValue); //
hash value that calculated will be printed
// make sure if hash value is bigger than the table
size, the value wraps
return calculatedHashValue % HASH_SIZE;
}
// FUNCTION : putIntoHashTable
// DESCRIPTION :
// Put the supplied data into a hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash
table
// bufferLength - how many characters are in the buffer being put
into the hash table
//
// RETURNS :
// Hash value used ( HASH_SIZE on failure)
unsigned int putIntoHashTable(char *ptrInputData, unsigned int
bufferLength)
{
unsigned int hashValue =
myHashFunction(ptrInputData);
// check if the value is in the hash
// add code to put data into the hash table!!!
return hashValue;
}
// FUNCTION : getFromHashTable
// DESCRIPTION :
// Read as much data as there is room for from the hash table
// PARAMETERS :
// ptrOutputBuffer - a pointer to the buffer to place the data read
from hash table
// bufferLength - maxiumum number of characters that can be
read
//
// RETURNS :
// Hash value used ( return HASH_SIZE value on failure)
unsigned int getFromHashTable(char *ptrOutputData, unsigned int
bufferLength)
{
unsigned int hashValue =
myHashFunction(ptrOutputData);
// check if the data is in the hash table here!!!
return hashValue;
}
// end code * * *
CODE
#include<stdio.h>
#include <stdlib.h>
unsigned int putIntoHashTable(int inputData); // function to add to hash table
unsigned int getFromHashTable(int inputData); // function to retrieve data from hash table
#define HASH_SIZE 100 // size of hash table to be used (for testing I suggest making this number much lower)
// data structure used to keep track of hashed data
struct myHashStruct {
int data;
struct myHashStruct *ptrNextHashData; // pointer to next item in this hash bucket (or NULL if no more)
};
struct myHashStruct *myHash[HASH_SIZE]; // create an empty hash table structure (note this is basically an arrary of linked list heads)
int main()
{
int input;
// initialize the hash table to empty one
for (int i = 0; i < HASH_SIZE; i++)
{
if ((myHash[i] = (struct myHashStruct *)calloc(1, sizeof(struct myHashStruct))) == NULL)
{
printf("calloc failed!\n");
return(-1);
}
}
// add to hash table loop
while (1)
{
printf("enter data to be added to hash table or -1 when done\n");
// get strings from the console and place in hash until nothing entered
scanf("%d", &input);
// stop adding data into hash table when "exit" is entered
if (input == -1)
break;
if (putIntoHashTable(input) == HASH_SIZE)
printf("Error putting into hash table\n");
}
// check if data is in hash table
while (1)
{
unsigned int hashIndexNumber = 0;
printf("Enter data to find, 0 when complete\n");
// get strings from the console and check if in hash table
scanf("%d", &input);
// stop adding data into hash table when "done" is entered
if (input == 0)
break;
if ((hashIndexNumber = getFromHashTable(input)) == HASH_SIZE)
printf("%d not found in hash table\n", input);
else
printf("%d found in hash table at %u\n", input, hashIndexNumber);
}
return 0;
}
// FUNCTION : myHashFunction
// DESCRIPTION :
// Calculate a hash value to use in storing the data into the hash table
// PARAMETERS :
// input - an integer
// RETURNS :
// Hash value calculated ( HASH_SIZE on failure)
unsigned int myHashFunction(int input)
{
return input * input % HASH_SIZE;
}
// FUNCTION : putIntoHashTable
// DESCRIPTION :
// Put the supplied data into a hash table
// PARAMETERS :
// input - an integer
// RETURNS :
// Hash value used ( HASH_SIZE on failure)
unsigned int putIntoHashTable(int input)
{
unsigned int hashValue = myHashFunction(input);
// check if the value is in the hash
// add code to put data into the hash table!!!
return hashValue;
}
// FUNCTION : getFromHashTable
// DESCRIPTION :
// Read as much data as there is room for from the hash table
// PARAMETERS :
// input - an integer
// RETURNS :
// Hash value used ( return HASH_SIZE value on failure)
unsigned int getFromHashTable(int input)
{
unsigned int hashValue = myHashFunction(input);
// check if the data is in the hash table here!!!
return hashValue;
}
// end code * * *