Skip to content

Commit 8fec263

Browse files
authored
Add files via upload
1 parent d0308ce commit 8fec263

8 files changed

+512
-0
lines changed

HashMaps/Chaining.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package DataStructures.HashMaps;
2+
3+
import java.util.LinkedList;
4+
5+
public class Chaining {
6+
7+
public class Entry {
8+
9+
int key;
10+
String value;
11+
12+
public Entry(int key, String value) {
13+
this.key = key;
14+
this.value = value;
15+
}
16+
17+
}
18+
19+
LinkedList<Entry>[] entries = new LinkedList[5];
20+
21+
public void put(int key, String value) {
22+
23+
int index = hash(key);
24+
25+
if (entries[index] == null) {
26+
entries[index] = new LinkedList<Entry>();
27+
}
28+
29+
for (Entry entry : entries[index]) {
30+
if (entry.key == key) {
31+
entry.value = value;
32+
}
33+
}
34+
35+
Entry entry = new Entry(key, value);
36+
entries[index].addLast(entry);
37+
}
38+
39+
public String get(int key) {
40+
41+
int index = hash(key);
42+
43+
for (Entry entry : entries[index]) {
44+
if (entry.key == key) {
45+
return entry.value;
46+
}
47+
}
48+
49+
return "Not Found";
50+
}
51+
52+
public void remove(int key) {
53+
int index = hash(key);
54+
55+
for (Entry entry : entries[index]) {
56+
if (entry.key == key) {
57+
entries[index].remove(entry);
58+
return;
59+
}
60+
}
61+
}
62+
63+
private int hash(int key) {
64+
return key % entries.length;
65+
}
66+
}

HashMaps/LinearProbing.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package DataStructures.HashMaps;
2+
3+
import java.util.LinkedList;
4+
5+
public class LinearProbing {
6+
public class Entry {
7+
8+
int key;
9+
String value;
10+
11+
public Entry() {
12+
}
13+
14+
public Entry(int key, String value) {
15+
this.key = key;
16+
this.value = value;
17+
}
18+
19+
}
20+
21+
Entry[] entries = new Entry[5];
22+
23+
public void put(int key, String value) {
24+
25+
int index = hash(key);
26+
int previous = index - 1;
27+
28+
while (entries[index] != null) {
29+
if (index > previous) {
30+
System.out.println("List full");
31+
break;
32+
} else if (index >= entries.length) {
33+
index = 0;
34+
}
35+
index++;
36+
}
37+
38+
Entry entry = new Entry(key, value);
39+
entries[index] = entry;
40+
}
41+
42+
public String get(int key) {
43+
44+
int index = hash(key);
45+
int previous = index - 1;
46+
47+
while (entries[index].key != key) {
48+
if (index > previous) {
49+
return "Not Found";
50+
} else if (index >= entries.length) {
51+
index = 0;
52+
}
53+
index++;
54+
}
55+
56+
return entries[index].value;
57+
58+
}
59+
60+
public void remove(int key) {
61+
int index = hash(key);
62+
int previous = index - 1;
63+
64+
while (entries[index].key != key) {
65+
if (index > previous) {
66+
return;
67+
} else if (index >= entries.length) {
68+
index = 0;
69+
}
70+
index++;
71+
}
72+
73+
entries[index] = null;
74+
}
75+
76+
private int hash(int key) {
77+
return key % entries.length;
78+
}
79+
}

HashMapsExample.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package DataStructures;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class HashMapsExample {
7+
8+
Map<Character, Integer> map = new HashMap<>();
9+
10+
public Character findFirstNonRepeating(String input) {
11+
12+
for (char ch : input.toCharArray()) {
13+
14+
if (ch == ' ') {
15+
continue;
16+
} else if (!map.containsKey(ch)) {
17+
map.put(ch, 1);
18+
} else {
19+
map.put(ch, map.get(ch) + 1);
20+
}
21+
22+
}
23+
24+
for (char ch : input.toCharArray()) {
25+
26+
if (ch == ' ' || map.get(ch) > 1) {
27+
continue;
28+
} else {
29+
return ch;
30+
}
31+
}
32+
33+
return '0';
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "HashMaps [map=" + map + "]";
39+
}
40+
41+
}

LinkedList.java

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package DataStructures;
2+
3+
public class LinkedList {
4+
5+
private class Node {
6+
7+
private int value;
8+
private Node next;
9+
10+
public Node(int value) {
11+
this.value = value;
12+
}
13+
14+
}
15+
16+
private Node first;
17+
private Node last;
18+
private int size = 0;
19+
20+
public void addFirst(int value) {
21+
Node currentNode = first;
22+
first = new Node(value);
23+
first.next = currentNode;
24+
25+
if (last == null) {
26+
last = first;
27+
}
28+
29+
size++;
30+
}
31+
32+
public void addLast(int value) {
33+
if (first == null) {
34+
last = new Node(value);
35+
first = last;
36+
} else {
37+
last.next = new Node(value);
38+
39+
last = last.next;
40+
}
41+
42+
size++;
43+
44+
}
45+
46+
public int indexOf(int value) {
47+
return indexOf(first, value);
48+
}
49+
50+
int index = 0;
51+
52+
public int indexOf(Node node, int value) {
53+
if (node.value == value) {
54+
return index;
55+
} else if (node.next == null) {
56+
return -1;
57+
}
58+
59+
index++;
60+
return indexOf(node.next, value);
61+
}
62+
63+
public boolean contains(int value) {
64+
if (indexOf(value) > -1) {
65+
return true;
66+
}
67+
return false;
68+
}
69+
70+
public void deleteFirst() {
71+
first = first.next;
72+
}
73+
74+
public void deleteLast() {
75+
deleteLast(first);
76+
size--;
77+
}
78+
79+
public Node deleteLast(Node node) {
80+
if (node.next.next == null) {
81+
return node;
82+
}
83+
84+
last = deleteLast(node.next);
85+
last.next = null;
86+
return last;
87+
}
88+
89+
public int size() {
90+
return size;
91+
}
92+
93+
public int[] toArray() {
94+
int[] array = new int[size];
95+
int index = 0;
96+
return toArray(first, array, index);
97+
}
98+
99+
public int[] toArray(Node node, int[] array, int index) {
100+
101+
if (node == null) {
102+
return array; // 0 -1, 1 - 2, 2 - 3, 3 - 4
103+
}
104+
105+
array[index++] = node.value;
106+
toArray(node.next, array, index);
107+
108+
return array;
109+
110+
}
111+
112+
public void reverse() {
113+
// Node previous = first;
114+
Node newFirst = reverse(first.next, first);
115+
last = first;
116+
last.next = null;
117+
118+
first = newFirst;
119+
}
120+
121+
public Node reverse(Node node, Node previous) {
122+
123+
if (node == null) {
124+
return previous;
125+
}
126+
127+
Node current = node.next;
128+
node.next = previous;
129+
previous = node;
130+
return reverse(current, previous);
131+
132+
}
133+
134+
public int getKthFromEnd(int k) {
135+
Node slow = first;
136+
Node fast = first;
137+
138+
for (int i = 1; i < k; i++) {
139+
fast = fast.next;
140+
}
141+
142+
while (fast != last) {
143+
slow = slow.next;
144+
fast = fast.next;
145+
}
146+
147+
return slow.value;
148+
}
149+
150+
}

Queues/ArrayQueue.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package DataStructures.Queues;
2+
3+
public class ArrayQueue {
4+
5+
public int[] queue = new int[5];
6+
7+
int front = 0;
8+
int rear = -1;
9+
10+
public void add(int value) {
11+
rear++;
12+
if (rear > queue.length - 1) {
13+
if (queue[0] == 0) {
14+
rear = 0;
15+
} else {
16+
System.out.println("Queue is full");
17+
return;
18+
}
19+
} else if (rear == front && front != 0) {
20+
System.out.println("Queue is full");
21+
return;
22+
}
23+
24+
queue[rear] = value;
25+
26+
}
27+
28+
public void remove() {
29+
if (front > queue.length - 1) {
30+
front = 0;
31+
}
32+
queue[front] = 0;
33+
front++;
34+
}
35+
36+
public int peek() {
37+
return queue[front];
38+
}
39+
}

0 commit comments

Comments
 (0)