Skip to content

Commit 7947efa

Browse files
Merge remote-tracking branch 'origin/master'
2 parents a964d66 + f7af0d4 commit 7947efa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1971
-32
lines changed

src/main/java/misc/DepthFirstSearchUsingStack.java renamed to src/main/java/dsa/graphs/DepthFirstSearchUsingStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package misc;
1+
package dsa.graphs;
22

33
import java.util.Stack;
44

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package dsa.hashmap;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
6+
public class CustomHashMap {
7+
static class HashMap<K, V> { //generics
8+
private class Node {
9+
K key;
10+
V value;
11+
12+
public Node(K key, V value) {
13+
this.key = key;
14+
this.value = value;
15+
}
16+
}
17+
18+
private int nodeLength; //nodeLength - nodes
19+
private int bucketSize = 4; //bucketSize - buckets
20+
private LinkedList<Node>[] buckets; //bucketSize = buckets.length
21+
22+
@SuppressWarnings("unchecked")
23+
public HashMap() {
24+
this.buckets = new LinkedList[this.bucketSize];
25+
for (int i = 0; i < this.bucketSize; i++) {
26+
this.buckets[i] = new LinkedList<>();
27+
}
28+
}
29+
30+
private int hashFunction(K key) {
31+
int bucketIndex = key.hashCode();
32+
return Math.abs(bucketIndex) % bucketSize;
33+
}
34+
35+
private int searchInLinkedList(K key, int bucketIndex) {
36+
LinkedList<Node> ll = buckets[bucketIndex];
37+
38+
for (int i = 0; i < ll.size(); i++) {
39+
if (ll.get(i).key == key) {
40+
return i; //dataIndex
41+
}
42+
}
43+
44+
return -1;
45+
}
46+
47+
@SuppressWarnings("unchecked")
48+
private void rehash() {
49+
LinkedList<Node>[] oldBucket = buckets;
50+
bucketSize = bucketSize * 2;
51+
buckets = new LinkedList[bucketSize];
52+
for (int i = 0; i < bucketSize; i++) {
53+
buckets[i] = new LinkedList<>();
54+
}
55+
for (LinkedList<Node> ll : oldBucket) {
56+
for (Node node : ll) {
57+
put(node.key, node.value);
58+
}
59+
}
60+
}
61+
62+
public void put(K key, V value) {
63+
int bucketIndex = hashFunction(key);
64+
int dataIndex = searchInLinkedList(key, bucketIndex); //dataIndex = -1
65+
if (dataIndex == -1) { //key doesn't exist
66+
buckets[bucketIndex].add(new Node(key, value));
67+
nodeLength++;
68+
} else { //key exists
69+
Node node = buckets[bucketIndex].get(dataIndex);
70+
node.value = value;
71+
}
72+
double lambda = (double) nodeLength / bucketSize;
73+
74+
if (lambda > 2.0) {
75+
rehash();
76+
}
77+
}
78+
79+
public boolean containsKey(K key) {
80+
int bucketIndex = hashFunction(key);
81+
int dataIndex = searchInLinkedList(key, bucketIndex); //dataIndex = -1
82+
if (dataIndex == -1) { //key doesn't exist
83+
return false;
84+
} else { //key exists
85+
return true;
86+
}
87+
}
88+
89+
public V remove(K key) {
90+
int bucketIndex = hashFunction(key);
91+
int dataIndex = searchInLinkedList(key, bucketIndex); //dataIndex = -1
92+
if (dataIndex == -1) { //key doesn't exist
93+
return null;
94+
} else { //key exists
95+
Node node = buckets[bucketIndex].remove(dataIndex);
96+
nodeLength--;
97+
return node.value;
98+
}
99+
}
100+
101+
public V get(K key) {
102+
int bucketIndex = hashFunction(key);
103+
int dataIndex = searchInLinkedList(key, bucketIndex); //dataIndex = -1
104+
if (dataIndex == -1) { //key doesn't exist
105+
return null;
106+
} else { //key exists
107+
Node node = buckets[bucketIndex].get(dataIndex);
108+
return node.value;
109+
}
110+
}
111+
112+
public ArrayList<K> keySet() {
113+
ArrayList<K> keys = new ArrayList<>();
114+
for (LinkedList<Node> ll : buckets) {
115+
for (Node node : ll) { //dataIndex
116+
keys.add(node.key);
117+
}
118+
}
119+
return keys;
120+
}
121+
122+
public boolean isEmpty() {
123+
return nodeLength == 0;
124+
}
125+
}
126+
127+
public static void main(String[] args) {
128+
HashMap<String, Integer> map = new HashMap<>();
129+
map.put("India", 190);
130+
map.put("China", 200);
131+
map.put("US", 50);
132+
ArrayList<String> keys = map.keySet();
133+
for (String key : keys) {
134+
System.out.println(key + " " + map.get(key));
135+
}
136+
137+
map.remove("India");
138+
System.out.println(map.get("India"));
139+
}
140+
}

src/main/java/dsa/stacksqueues/CircularQueue.java renamed to src/main/java/dsa/queue/CircularQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dsa.stacksqueues;
1+
package dsa.queue;
22

33
public class CircularQueue {
44
protected int[] data;

src/main/java/dsa/stacksqueues/CustomQueue.java renamed to src/main/java/dsa/queue/CustomQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dsa.stacksqueues;
1+
package dsa.queue;
22

33
import java.util.NoSuchElementException;
44

src/main/java/dsa/stacksqueues/DynamicQueue.java renamed to src/main/java/dsa/queue/DynamicQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dsa.stacksqueues;
1+
package dsa.queue;
22

33
public class DynamicQueue extends CircularQueue {
44
public DynamicQueue() {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dsa.queue;
2+
3+
public class QueueUsingArrayList {
4+
public static void main(String args[]) {
5+
Queue q = new Queue(5);
6+
q.add(1);
7+
q.add(2);
8+
q.add(3);
9+
System.out.println(q.remove());
10+
System.out.println(q.peek());
11+
}
12+
13+
static class Queue {
14+
static int[] arr;
15+
static int size;
16+
static int rear;
17+
18+
19+
Queue(int size) {
20+
Queue.size = size;
21+
arr = new int[size];
22+
rear = -1;
23+
}
24+
25+
26+
public static boolean isEmpty() {
27+
return rear == -1;
28+
}
29+
30+
31+
public static boolean isFull() {
32+
return rear == size - 1;
33+
}
34+
35+
36+
public static void add(int data) {
37+
if (isFull()) {
38+
System.out.println("Queue Overflow");
39+
return;
40+
}
41+
arr[++rear] = data;
42+
}
43+
44+
45+
//O(n)
46+
public static int remove() {
47+
if (isEmpty()) {
48+
System.out.println("Queue is empty");
49+
return -1;
50+
}
51+
int front = arr[0];
52+
for (int i = 0; i < rear; i++) {
53+
arr[i] = arr[i + 1];
54+
}
55+
rear--;
56+
return front;
57+
}
58+
59+
60+
public static int peek() {
61+
if (isEmpty()) {
62+
System.out.println("empty queue");
63+
return -1;
64+
}
65+
66+
return arr[0];
67+
}
68+
}
69+
70+
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dsa.queue;
2+
3+
public class QueueUsingLinkedList {
4+
public static void main(String[] args) {
5+
Queue q = new Queue();
6+
q.add(1);
7+
q.add(2);
8+
q.add(3);
9+
q.add(4);
10+
q.add(5);
11+
12+
13+
while (!q.isEmpty()) {
14+
System.out.println(q.peek());
15+
q.remove();
16+
}
17+
}
18+
19+
static class Node {
20+
int data;
21+
Node next;
22+
23+
Node(int data) {
24+
this.data = data;
25+
next = null;
26+
}
27+
}
28+
29+
static class Queue {
30+
static Node head = null;
31+
static Node tail = null;
32+
33+
public static boolean isEmpty() {
34+
return head == null && tail == null;
35+
}
36+
37+
public static void add(int data) {
38+
Node newNode = new Node(data);
39+
if (isEmpty()) {
40+
tail = head = newNode;
41+
} else {
42+
tail.next = newNode;
43+
tail = newNode;
44+
}
45+
}
46+
47+
public static int remove() {
48+
if (isEmpty()) {
49+
System.out.println("empty queue");
50+
return -1;
51+
}
52+
int front = head.data;
53+
//single node
54+
if (head == tail) {
55+
tail = null;
56+
}
57+
head = head.next;
58+
return front;
59+
}
60+
61+
public static int peek() {
62+
if (isEmpty()) {
63+
System.out.println("empty queue");
64+
return -1;
65+
}
66+
67+
return head.data;
68+
}
69+
}
70+
}

src/main/java/dsa/stacksqueues/CustomStack.java renamed to src/main/java/dsa/stack/CustomStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dsa.stacksqueues;
1+
package dsa.stack;
22

33
import java.util.EmptyStackException;
44

src/main/java/dsa/stacksqueues/DynamicStack.java renamed to src/main/java/dsa/stack/DynamicStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dsa.stacksqueues;
1+
package dsa.stack;
22

33
public class DynamicStack extends CustomStack {
44

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dsa.stack;
2+
3+
import java.util.ArrayList;
4+
5+
public class StackUsingArrayList {
6+
public static void main(String[] args) {
7+
Stack stack = new Stack();
8+
stack.push(1);
9+
stack.push(2);
10+
stack.push(3);
11+
stack.push(4);
12+
13+
while (!stack.isEmpty()) {
14+
System.out.println(stack.peek());
15+
stack.pop();
16+
}
17+
}
18+
19+
static class Stack {
20+
ArrayList<Integer> list = new ArrayList<>();
21+
22+
public void push(int data) {
23+
list.add(data);
24+
}
25+
26+
public boolean isEmpty() {
27+
return list.size() == 0;
28+
}
29+
30+
public int pop() {
31+
if (isEmpty()) {
32+
return -1;
33+
}
34+
int top = list.remove(list.size() - 1);
35+
return top;
36+
}
37+
38+
public int peek() {
39+
if (isEmpty()) {
40+
return -1;
41+
}
42+
return list.get(list.size() - 1);
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)