diff --git a/HashMap.cpp b/HashMap.cpp new file mode 100644 index 00000000..26e1a7a4 --- /dev/null +++ b/HashMap.cpp @@ -0,0 +1,141 @@ +#include +using namespace std; + +class MyHashMap { +private: + struct Node { + int key; + int value; + Node* next; + Node (int k, int val) { + key = k; + value = val; + next = nullptr; + } + }; + + static const int buckets = 10000; + vector storage; +public: + MyHashMap() { + // Initialize a null array + storage.resize(buckets); + } + + Node* getPreviousNode(int key, Node* head) { + Node* prev = head; + Node* cur = head ? head->next : nullptr; + // Search the innerList. + // If the element is found, return the prevNode + // Else, we will end up returning the dummy head. + while (cur != nullptr && cur->key != key) { + // Update prev and move ahead. + prev = cur; + cur = cur->next; + } + + return prev; + } + + // HashFunction + int getBucketIndex(int key) { + return key % buckets; + } + + void put(int key, int value) { + // First things first, let us get the bucket + // we are interested in. + int idx = getBucketIndex(key); + + // There can be 3 cases + // 1) Exisiting key, need to update the value. + // 2) Unique key being added for the first time, + // add it to the list. + // 3) Bucket being used for the first time. + + // Handle case3: + if (storage[idx] == nullptr) { + // dummy node. + storage[idx] = new Node(-1,-1); + storage[idx]->next = new Node(key,value); + return; + } + + Node* prevNode = getPreviousNode(key, storage[idx]); + + // If case 2, then next should be null. Append it to the list + if (prevNode->next == nullptr) { + prevNode->next = new Node(key, value); + } else { + // Already existing, need to update the value. + prevNode->next->value = value; + } + + return; + } + + int get(int key) { + // First things first, let us get the bucket + // where the key is. + int idx = getBucketIndex(key); + + // There can be 3 cases + // 1) Exisiting key, return the value + // 2) Key not found, return -1. + // 3) Bucket not initialized, return -1 + + // Handle case3: + if (storage[idx] == nullptr) { + return -1; + } + + Node* prevNode = getPreviousNode(key, storage[idx]); + + // If case 2, then next should be null. + if (prevNode->next == nullptr) { + return -1; + } else { + // Already existing, need to return the value. + return prevNode->next->value; + } + } + + void remove(int key) { + // First things first, let us get the bucket + // we want to remove. + int idx = getBucketIndex(key); + + // There can be 3 cases + // 1) Exisiting key, need to remove it. + // 2) Key not found, return. + // 3) Bucket being used for the first time. + + // Handle case3: + if (storage[idx] == nullptr) { + return; + } + + Node* prevNode = getPreviousNode(key, storage[idx]); + + // If case 2, then next should be null. + if (prevNode->next == nullptr) { + return; + } else { + // Already existing, remove it by pointing the + // prevNode's next pointer to it's next pointer. + Node* toDelete = prevNode->next; + prevNode->next = toDelete->next; + delete toDelete; + } + + return; + } +}; + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap* obj = new MyHashMap(); + * obj->put(key,value); + * int param_2 = obj->get(key); + * obj->remove(key); + */ \ No newline at end of file diff --git a/QueueStack.cpp b/QueueStack.cpp new file mode 100644 index 00000000..6d8ea336 --- /dev/null +++ b/QueueStack.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +class MyQueue { + private: + stack st1; + stack st2; + + // Rebalance the stacks to support FIFO + void rebalance() { + while (!st1.empty()) { + st2.push(st1.top()); + st1.pop(); + } + } +public: + MyQueue() { + + } + + void push(int x) { + st1.push(x); + } + + int pop() { + // st2 empty? rebalance due to FIFO policy. + if (st2.empty()) { + rebalance(); + } + int val = st2.top(); + st2.pop(); + return val; + } + + int peek() { + // st2 empty? rebalance due to FIFO policy. + if (st2.empty()) { + rebalance(); + } + + return st2.top(); + } + + bool empty() { + return st1.empty() && st2.empty(); + } +}; + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue* obj = new MyQueue(); + * obj->push(x); + * int param_2 = obj->pop(); + * int param_3 = obj->peek(); + * bool param_4 = obj->empty(); + */ \ No newline at end of file