Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/MyQueue.class
/MyHashMap.class
/MyHashMap$Node.class
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Design2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
99 changes: 99 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@


// Here we are using an array where each index stores a reference to a linked list.
// A hash function is used to determine the index in the array.
// If no linked list is found at that index, a dummy node (-1, -1) is created.
// Subsequent key-value pairs are stored as new nodes in the linked list.
// For each key, only one node exists in the list (no duplicates).
// This way, collisions are handled using separate chaining with linked lists.
class MyHashMap {
int bucket;
Node[] storage;

class Node{
int key;
int value;
Node next;

public Node(int key, int value)
{
this.key= key;
this.value= value;
}
}

public MyHashMap() {
this.bucket= 10000;
this.storage= new Node[bucket];

}

public int hash1(int key){
return key%bucket;
}

public Node helper(Node head, int key){
Node prev= null;
Node curr= head;
while(curr!=null && curr.key!= key){
prev=curr;
curr=curr.next;
}
return prev;
}

public void put(int key, int value) {
int primaryIndex= hash1(key);
if(storage[primaryIndex]== null){
storage[primaryIndex]= new Node(-1,-1);
storage[primaryIndex].next= new Node(key,value);

}

Node prev= helper(storage[primaryIndex],key);
if(prev.next== null){
prev.next= new Node(key,value);
}
else
{
prev.next.value=value;
}

}

public int get(int key) {
int index= hash1(key);
if(storage[index]==null){
return -1;
}
Node prev= helper(storage[index],key);
if(prev.next==null){
return -1;
}
else{
return prev.next.value;
}


}

public void remove(int key) {
int index= hash1(key);
if(storage[index]== null){
return;
}
Node prev= helper(storage[index], key);
if(prev.next==null){
return;
}
else
{
Node curr= prev.next;
curr.next= null;
prev.next.next= prev.next;
}



}
}
53 changes: 53 additions & 0 deletions MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

// Here we are using two stacks: one for 'in' and another for 'out'.
// New elements are always pushed onto the 'in' stack.
// When we need to pop, we first check if 'out' is empty.
// If 'out' is empty, we move all elements from 'in' to 'out', reversing their order.
// Then we pop the element from the 'out' stack.
// This ensures the queue order (FIFO) is maintained.
import java.util.*;
class MyQueue {
Stack<Integer> in;
Stack<Integer> out;

public MyQueue() {
this.in= new Stack<>();
this.out= new Stack<>();

}
//O(1)
public void push(int x) {
in.push(x);

}

public int pop() {
if(out.isEmpty()){
while(!in.isEmpty()){
int x= in.pop();
out.push(x);
}
}
return out.pop();


}

public int peek() {
if(out.isEmpty()){
while(!in.isEmpty()){
int x= in.pop();
out.push(x);
}
}
return out.peek();


}

public boolean empty() {
return (in.isEmpty() && out.isEmpty());

}
}