Skip to content

Commit 412f2d9

Browse files
committed
Cleaning up repo
1 parent 68efb58 commit 412f2d9

25 files changed

+599
-22
lines changed

src/com/deepak/LinkedList/BasicOperations.java

-5
This file was deleted.

src/com/deepak/LinkedList/DoublyLinkedList.java

-5
This file was deleted.

src/com/deepak/LinkedList/LinkedListConcepts

-2
This file was deleted.

src/com/deepak/Arrays/BasicOperations.java renamed to src/com/deepak/data/structures/Arrays/BasicOperations.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Data-Structures-And-Algorithms-in-Java
33
*/
4-
package com.deepak.Arrays;
4+
package com.deepak.data.structures.Arrays;
55

66
/**
77
* Class to hold implementation of basic operations in java

src/com/deepak/Hashing/ConcurrentHashMapImplementation.java renamed to src/com/deepak/data/structures/Hashing/ConcurrentHashMapImplementation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Data-Structures-And-Algorithms-in-Java
33
* ConcurrentHashMapImplementation.java
44
*/
5-
package com.deepak.Hashing;
5+
package com.deepak.data.structures.Hashing;
66

77
import java.util.Map;
88
import java.util.concurrent.ConcurrentHashMap;

src/com/deepak/Hashing/ConcurrentModificationException.java renamed to src/com/deepak/data/structures/Hashing/ConcurrentModificationException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Data-Structures-And-Algorithms-in-Java
33
* ConcurrentModificationException.java
44
*/
5-
package com.deepak.Hashing;
5+
package com.deepak.data.structures.Hashing;
66

77
import java.util.HashMap;
88
import java.util.Iterator;

src/com/deepak/Hashing/CustomHashMap.java renamed to src/com/deepak/data/structures/Hashing/CustomHashMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Data-Structures-And-Algorithms-in-Java
33
* CustomHashMap.java
44
*/
5-
package com.deepak.Hashing;
5+
package com.deepak.data.structures.Hashing;
66

77
import java.util.Map;
88
import java.util.Objects;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.deepak.data.structures.Hashing;
2+
3+
import java.util.Map;
4+
import java.util.Objects;
5+
6+
public class CustomHashTable<K, V> {
7+
8+
/**
9+
* This is a node representation of Linked List
10+
* @author Deepak
11+
*
12+
* @param <K>
13+
* @param <V>
14+
*/
15+
static class Entry<K, V> {
16+
final int hash;
17+
final K key;
18+
V value;
19+
Entry<K, V> next;
20+
21+
/**
22+
* Constructor for creating a entry
23+
* @param hash
24+
* @param key
25+
* @param value
26+
* @param next
27+
*/
28+
public Entry(int hash, K key, V value, Entry<K, V> next) {
29+
this.hash = hash;
30+
this.key = key;
31+
this.value = value;
32+
this.next = next;
33+
}
34+
35+
/**
36+
* HashCode implementation for entry
37+
*/
38+
public final int hashCode() {
39+
return Objects.hashCode(key) ^ Objects.hashCode(value);
40+
}
41+
42+
/**
43+
* Method to check if two objects are equal
44+
*/
45+
public final boolean equals(Object o) {
46+
if (o == this) {
47+
return true;
48+
}
49+
if (o instanceof Map.Entry) {
50+
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
51+
if (Objects.equals(key, e.getKey()) &&
52+
Objects.equals(value, e.getValue())) {
53+
return true;
54+
}
55+
}
56+
return false;
57+
}
58+
59+
}
60+
61+
}

src/com/deepak/Hashing/HashCodeAndEquals.java renamed to src/com/deepak/data/structures/Hashing/HashCodeAndEquals.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Data-Structures-And-Algorithms-in-Java
33
* HashCodeAndEquals.java
44
*/
5-
package com.deepak.Hashing;
5+
package com.deepak.data.structures.Hashing;
66

77
import java.util.HashMap;
88
import java.util.HashSet;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
``` HashTable v/s HashMap
2+
Before this, I would suggest to go through the topic Iterator V/S Enumeration here.
3+
4+
There are some significant factors that makes both of these data structures different
5+
6+
1. Synchronization or Thread Safety
7+
- This is the most important difference between the two.
8+
- Hash tables are synchronized and thread safe whereas Hash maps are not.
9+
10+
2. Null keys and Null values
11+
- HashMap allows one null key and multiple null values, whereas hash table doesn't allow neither null values not null keys.
12+
13+
3. Iterating the Values
14+
- Values in hash map are iterated using an iterator, whereas in hash table values are iterated using enumerator.
15+
- Only Vector other then hash table uses enumerator to iterate through elements.
16+
17+
4. Fail Fast Iterator
18+
- Iterator in hash map is fail fast iterator, whereas enumerator for hash table is not.
19+
- If hash table is structurally modified at any time after the iterator is created other then iterators own remove method, then it will throw Concurrent Modification exception.
20+
- Structural modification means adding or removing elements from the collection.
21+
22+
5. Super class and Legacy
23+
- HashTable is a subclass of Dictionary, which is now obsolete from JDK 1.7
24+
25+
6. Performance
26+
- HashMap is much faster and uses less memory because it is not synchronized.
27+
28+
Similarities :
29+
1. Order of elements
30+
- Cannot be guaranteed, because both of these work based on the hashing logic. use Linked Hash map for that
31+
2. Both of them comes from Map interface.
32+
3. Both provides constant time for performance for put and get methods, assuming that objects are distributed uniformly.
33+
4. Both works on the principle of hashing
34+
35+
When to use?
36+
Avoid using HashTable, as they are obsolete now, ConcurrentHashMap has replaced it.
37+
Single threaded apps - use HashMap
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Though both of us allows to traverse through the collection of elements.
2+
3+
Both Iterator and Enumeration are interfaces, found in java.util package
4+
1. Iterator has 3 methods,
5+
- hasNext()
6+
- next()
7+
- remove()
8+
2. Enumeration has 2 methods,
9+
- hasMoreElements()
10+
- nextElement()
11+
12+
- Iterator differs from Enumeration because iterator allows the caller to remove the elements while traversing through the collection. Enumeration doesn't have remove method. Enumeration is a legacy class and not all data structures supports it. for ex. Vector supports enumeration but a array list doesn't. On the other hand iterator is a standard class for traversal of elements.
13+
- Enumeration is older and exists from JDK 1.0, whereas iterator was introduced later.
14+
- Functionality of enumeration interface is duplicated in Iterator with some extra features on top of it.
15+
- Enumeration acts as read only interface, because it has methods only to traverse the elements. Whereas, iterator can modify the collection too, because remove method exits in it.
16+
- Iterator is more secure and safe, because it does not allow any other thread to modify the collection if one thread is working on the collection already. Iterator will throw ConcurrentModification Exception in this use case.
17+
18+
When to use?
19+
- Use enumeration when only reading of elements is associated.

src/com/deepak/Hashing/WeakHashMapImplementation.java renamed to src/com/deepak/data/structures/Hashing/WeakHashMapImplementation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Data-Structures-And-Algorithms-in-Java
33
* WeakHashMapImplementation.java
44
*/
5-
package com.deepak.Hashing;
5+
package com.deepak.data.structures.Hashing;
66

77
import java.util.Map;
88
import java.util.WeakHashMap;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deepak.data.structures.LinkedList;
2+
3+
public class BasicOperations {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deepak.data.structures.LinkedList;
2+
3+
public class DoublyLinkedList {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Linked List
2+
</br>
3+
4+
We already have arrays, what's the problem with it that we have to move with a new data structure??
5+
6+
- Arrays are fixed size data structure. Once created in memory we cannot cross that limit.
7+
Resizing can be done but there is no surety that we will have that much memory available later on.
8+
For ex. We created a array of initial size 10. Application is running and it has taken lot of memory.
9+
We got a need to increase the size of out array to 25 now. We don't have enough contiguous memory to create it.
10+
Problem !!!!
11+
2. Array insertion and deletion may require adjustment in size. (Overhead)
12+
3. The best part with Arrays is, each element can be accessed in a very efficient manner since index is known.
13+
Applications which can be better managed without using a contiguous memory based data structure, Linked lists are introduced.
14+
15+
Linked list is a collection of objects (called Nodes) connected to each other in the memory.
16+
Each Node contains some data and pointer to some other Node. Last node points to null reference.
17+
Entry point to the linked list is called first node or head node. If list is empty, head will be null.
18+
Unlike arrays, nodes cannot be accessed using index. Also they don't take contiguous memory.
19+
If we want to traverse through the list, we have to start from head and then check each element.
20+
21+
### Types of Linked List
22+
- Singly Linked List - Provides access to list from the head node. Traversal is allowed only one way and there is no going back.
23+
- Doubly Linked List - Provides access to list from head and tail nodes. Traversal is allowed from both the sides of the list.
24+
- Circular Linked List - Provides access to list from the head node. But pointer of last node again points back to first node.
25+
26+
### Operations on Linked List
27+
- Traversing through linked list
28+
- Check if list is empty
29+
- Check the size of the list
30+
- Inserting a element in the list
31+
- Search a element by index
32+
- Search a element by value
33+
- Delete a element from the list
34+
- Converting a list to and from a Array
35+

src/com/deepak/LinkedList/SinglyLinkedList.java renamed to src/com/deepak/data/structures/LinkedList/SinglyLinkedList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.deepak.LinkedList;
1+
package com.deepak.data.structures.LinkedList;
22

33
public class SinglyLinkedList<E> {
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Data-Structures-And-Algorithms-in-Java
3+
* ArrayBasedQueue.java
4+
*/
5+
package com.deepak.data.structures.Queue;
6+
7+
import java.util.NoSuchElementException;
8+
9+
/**
10+
* Implementation of array based queue
11+
* @author Deepak
12+
*/
13+
public class ArrayBasedQueue {
14+
15+
/**
16+
* Method to test queue implementation
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
System.out.println("Creating a new Array based queue.");
21+
ArrayBasedQueue queue = new ArrayBasedQueue(10);
22+
System.out.println("Size of Queue => " + queue.size());
23+
System.out.println("Is Queue Empty => " + queue.isEmpty());
24+
System.out.println("Inserting two items in Queue.");
25+
queue.enqueue(10);
26+
queue.enqueue(20);
27+
System.out.println("After Insertion, Size of Queue => " + queue.size());
28+
System.out.println("After Insertion, Is Queue Empty => " + queue.isEmpty());
29+
System.out.println("Removing an item from Queue.");
30+
queue.dequeue();
31+
System.out.println("After Removal, Size of Queue => " + queue.size());
32+
System.out.println("Top element on Queue => " + queue.peek());
33+
}
34+
35+
/**
36+
* Array of objects
37+
*/
38+
private Object[] array;
39+
40+
/**
41+
* Size of queue
42+
*/
43+
int size = 0;
44+
45+
/**
46+
* Since it is a queue, we need track of both head and tail
47+
* Initial values to be 0
48+
*/
49+
int head = 0;
50+
int tail = 0;
51+
52+
/**
53+
* Constructor to create new array based queue
54+
* @param capacity
55+
*/
56+
public ArrayBasedQueue(int capacity) {
57+
array = new Object[capacity];
58+
}
59+
60+
/**
61+
* Method to push a new item in queue
62+
* We will deal only with tail while adding new item
63+
* @param item
64+
*/
65+
public void enqueue(Object item) {
66+
if (size == array.length) {
67+
throw new IllegalStateException("Cannot add to full queue");
68+
}
69+
array[tail] = item;
70+
tail = (tail + 1) % array.length;
71+
size++;
72+
}
73+
74+
/**
75+
* Method to pop a item from queue
76+
* We will deal only with head while deleting a element
77+
* @return
78+
*/
79+
public Object dequeue() {
80+
if (size == 0) {
81+
throw new NoSuchElementException("Cannot remove from empty queue");
82+
}
83+
Object item = array[head];
84+
array[head] = null;
85+
head = (head + 1) % array.length;
86+
size--;
87+
return item;
88+
}
89+
90+
/**
91+
* Method to check the top item in queue
92+
* @return
93+
*/
94+
public Object peek() {
95+
if (size == 0) {
96+
throw new NoSuchElementException("Cannot peek from empty queue");
97+
}
98+
return array[size - 1];
99+
}
100+
101+
/**
102+
* Method to check size of queue
103+
* @return
104+
*/
105+
public int size() {
106+
return size;
107+
}
108+
109+
/**
110+
* Method to check if queue is empty
111+
* @return
112+
*/
113+
public boolean isEmpty() {
114+
return size == 0;
115+
}
116+
117+
}

0 commit comments

Comments
 (0)