Friday, June 21, 2019

Implementation of Hash table in CPP

Implementation of Hash table in CPP
#include <iostream>
#include <string>
using namespace std;
int TABLE_SIZE = 10000;
template <typename K, typename V>
class HashNode
{
public:
HashNode(K _key, V _value)
{
key = _key;
value = _value;
next = NULL;
}
void setKey(K _key)
{
key = _key;
}
K getKey()
{
return key;
}
void setValue(V _value)
{
value = _value;
}
V getValue()
{
return value;
}
HashNode *getNext()
{
return next;
}
void setNext(HashNode *_next)
{
next = _next;
}
void printHashNode()
{
cout << key << ":" << value << endl;
}
private:
K key;
V value;
HashNode *next;
};
template <typename K, typename V>
class HashTable
{
private:
HashNode<K, V> *hash_table;
public:
HashTable()
{
hash_table = (HashNode<K, V> *)malloc(TABLE_SIZE * sizeof(HashNode<K, V>));
}
~HashTable()
{
free(hash_table);
}
unsigned long int _keyhash(string str)
{
unsigned long int strsum = 0;
for (char i : str)
{
strsum = strsum + (int)i;
}
return strsum;
}
void addEntry(HashNode<K, V> hash_node)
{
int index = _keyhash(hash_node.getKey()) % TABLE_SIZE;
if (hash_table[index].getNext() == NULL)
{
HashNode<string, string> lastNode("", "");
hash_node.setNext(&lastNode);
hash_table[index] = hash_node;
}
else
{
HashNode<K, V> *recurse = (HashNode<K, V> *)hash_table + index;
HashNode<K, V> *lastRealNode;
while (recurse->getNext() != NULL)
{
lastRealNode = recurse;
if (recurse->getKey() == hash_node.getKey()){
cout << "Key already present : " << recurse->getKey() << endl;
recurse->setValue(hash_node.getValue());
cout << "new Value : " << recurse->getValue() << endl;
return;
}
recurse = recurse->getNext();
}
lastRealNode->setNext(&hash_node);
HashNode<string, string> lastNode("", "");
hash_node.setNext(&lastNode);
}
return;
}
V getValue(const K key) {
int index = _keyhash(key) % TABLE_SIZE;
if (hash_table[index].getNext() == NULL ) {
cout << "key hash not found \n" << endl;
}
else {
for (HashNode<K, V> *recurse = hash_table+index; recurse->getNext() != NULL ; recurse = recurse->getNext() ){
if (recurse->getKey() == key) return recurse->getValue();
}
cout << "key hash was found , key not found \n";
return NULL;
}
}
void printHashTable()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (hash_table[i].getNext() != NULL)
{
HashNode<K, V> *entry = (HashNode<K, V> *)(hash_table + i);
cout << entry->getKey() << endl;
for (HashNode<K, V> *recurse = entry; recurse->getNext() != NULL; recurse = recurse->getNext())
{
cout << "key:" << recurse->getKey() << " value:" << recurse->getValue() << endl;
}
}
}
}
};
int main(int argc, char *argv[])
{
HashNode<string, string> hn1("semiconductor1", "intel");
HashNode<string, string> hn2("pertroleum2", "chevron");
HashNode<string, string> hn3("semiconductor2", "nvidia");
HashNode<string, string> hn4("pertroleum2", "exxon");
HashTable<string, string> ht;
ht.addEntry(hn1);
ht.addEntry(hn2);
ht.addEntry(hn3);
ht.addEntry(hn4);
cout << ht.getValue("pertroleum2") << endl;
ht.printHashTable();
}
view raw hashmap.cpp hosted with ❤ by GitHub

Monday, June 10, 2019

Stack implementation in CPP

Here is a stack implementation in CPP
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Stack {
public:
vector <T> vec;
size_t size;
Stack(int temp){
size = (size_t) temp;
}
void push(T val){
cout << "stack size :"<< vec.size() << endl;
if (vec.size() > size){
cout << " Stack is full " << endl;
return;
}
vec.push_back(val);
}
T pop(){
if(vec.empty()){
cout<< "Stack is empty"<< endl;
return 0;
}
T temp = vec.back();
vec.pop_back();
return temp;
}
T peek() {
if (vec.empty()){
cout<< "Stack is empty" << endl;
return 0;
}
return vec.back();
}
void printStack(){
for ( T val : vec)
cout << val << " " ;
cout<<endl;
}
void operator << (T val) {
cout << "stack size :"<< vec.size() << endl;
if (vec.size() > size){
cout << " Stack is full " << endl;
return;
}
vec.push_back(val);
}
};
int main( int argc, char *argv[]){
Stack<int> stack(2);
stack.push(2);
stack.printStack();
stack.push(3);
stack.printStack();
stack.push(4);
stack.printStack();
stack.push(5);
stack.printStack();
stack<< 6;
stack.printStack();
cout << stack.pop() << endl;
stack.printStack();
cout << stack.peek() << endl;
stack.printStack();
return 0;
}
view raw stack.cpp hosted with ❤ by GitHub

Sunday, March 3, 2019

Variable arguments in C

Variable arguments can be passed to a function in C. The best example for this is the inbuilt printf function. For printf the first argument is a character array and variable number of arguments after that. This is implemented using macros within the stdarg header. When parameters are passed to a function , its copied to a stack created for that function. va_list and va_arg and va_end are macros which help traverse thru this stack. This is partial implementation of your own printf function.
#include<stdarg.h>
#include<iostream>
using namespace std;
void my_printf(char* format, ...)
{
va_list argp;
va_start(argp, format);
while (*format != '\0') {
if (*format == '%') {
format++;
if (*format == '%') {
putchar('%');
} else if (*format == 'c') {
char char_to_print = va_arg(argp, int);
putchar(char_to_print);
} else {
fputs("Not implemented", stdout);
}
} else {
putchar(*format);
}
format++;
}
va_end(argp);
}
int main()
{
my_printf((char *) "char %c %c", '1', '2');
my_printf((char *) "string %c %c %d", '3', '4', 5);
}