From 3161246b0ac5fcbbfc36ba15991076d917091837 Mon Sep 17 00:00:00 2001 From: mukul Date: Fri, 26 Dec 2025 06:43:07 +0530 Subject: [PATCH] Done with Design-2 --- HashMap.java | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ MyQueue.java | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 HashMap.java create mode 100644 MyQueue.java diff --git a/HashMap.java b/HashMap.java new file mode 100644 index 00000000..17d1ae47 --- /dev/null +++ b/HashMap.java @@ -0,0 +1,88 @@ +//Here we are using linear chaining to implement hashmap +//we are initiating a linked list to an array to performput,get and remove functions +//we have created a find function to traverse through linked list and using that function in put, get and remove + +class MyHashMap { + class Node{ + int key; + int value; + Node next; + + public Node(int key,int value){ + this.key=key; + this.value=value; + } + } + private Node []storage; + private int buckets; + + private int hash(int key){ + return key%buckets; + } + + public MyHashMap() { + this.buckets=1000; + this.storage=new Node[buckets]; + } + + private Node find(Node head,int key){ + Node prev=head;//we have a dummy node + Node curr=head.next; + while(curr!=null && curr.key!=key){ + prev=curr; + curr=curr.next; + } + return prev; + } + + public void put(int key, int value) { + int idx= hash(key); + if(storage[idx]==null){ + storage[idx]= new Node(-1,-1); + } + Node prev= find(storage[idx],key); + //if current key is not there in list + if(prev.next==null){ + Node new_node= new Node(key,value); + prev.next=new_node; + } + else{ + //if current key is not there in list + prev.next.value=value; + } + + } + + public int get(int key) { + int idx= hash(key); + if(storage[idx]==null){ + return -1; + } + Node prev= find(storage[idx],key); + if(prev.next==null) return -1; + return prev.next.value; + + } + + public void remove(int key) { + int idx= hash(key); + if(storage[idx]==null){ + return ; + } + Node prev= find(storage[idx],key); + if(prev.next==null) return; + Node temp= prev.next; + prev.next=prev.next.next; + temp.next=null; + + + } +} + +/** + * 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/MyQueue.java b/MyQueue.java new file mode 100644 index 00000000..1d74f54b --- /dev/null +++ b/MyQueue.java @@ -0,0 +1,54 @@ +//Here we will approach this problem with 2 stacks, lets say inStack and outStack +//In inStack stack we will push the elements +//If outStack is empty then we will store popped elements from inStack to our outStack +//We will then pop from outStack to reach FIFO state + + +class MyQueue { + Stack inStack; + Stack outStack; + + public MyQueue() { + this.inStack=new Stack<>(); + this.outStack=new Stack<>(); + } + + + + public void push(int x) { + inStack.push(x); + } + + public int pop() { + if(outStack.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.pop(); + + } + + public int peek() { + + if(outStack.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.peek(); + } + + public boolean empty() { + return inStack.isEmpty() && outStack.isEmpty(); + } +} + +/** + * 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(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file