-
-
Save sricharankrishnan/66ebe0b7b191b4090379884520060d39 to your computer and use it in GitHub Desktop.
A doubly linked list data structure implementation done in java along with respective time complexities for each method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Doubly Linked List Implementation | |
* | |
* public void insertAtHead(E value): O(1) | |
* public void insertAtTail(E value): O(1) | |
* public void insertAtIndex(E value, int index): O(n) | |
* public Node removeAtHead(): O(1) | |
* public Node removeAtTail(): O(1) | |
* public Node removeAtIndex(int index): O(n) | |
* public Node get(int index): O(n) | |
* public boolean search(E value): O(n) | |
*/ | |
import java.util.*; | |
public class DoublyLinkedList<E> { | |
private class Node<E> { | |
public E value; | |
public Node<E> prev; | |
public Node<E> next; | |
public Node(E value) { | |
this.value = value; | |
this.prev = this.next = null; | |
} | |
public String toString() { | |
return "{Node: " + this.value + "}"; | |
} | |
} | |
private Node<E> head; | |
private Node<E> tail; | |
private int size; | |
public DoublyLinkedList() { | |
this.size = 0; | |
this.head = this.tail = null; | |
} | |
/* O(1) */ | |
public void insertAtHead(E value) { | |
if (!Objects.isNull(value)) { | |
Node node = new Node(value); | |
if (this.isEmpty()) { | |
this.head = this.tail = node; | |
} | |
else { | |
node.next = this.head; | |
this.head.prev = node; | |
this.head = node; | |
} | |
++this.size; | |
} | |
} | |
/* O(1) */ | |
public void insertAtTail(E value) { | |
if (!Objects.isNull(value)) { | |
Node node = new Node(value); | |
if (this.isEmpty()) { | |
this.head = this.tail = node; | |
} | |
else { | |
this.tail.next = node; | |
node.prev = this.tail; | |
this.tail = node; | |
} | |
++this.size; | |
} | |
} | |
/* O(n) */ | |
public void insertAtIndex(E value, int index) { | |
if (!Objects.isNull(value)) { | |
if (!(index >= 0 && index < this.size)) { | |
throw new IndexOutOfBoundsException("Index: " + index + " is out of bounds"); | |
} | |
else if (index == 0) { | |
this.insertAtHead(value); | |
return; | |
} | |
else if (index == this.size) { | |
this.insertAtTail(value); | |
return; | |
} | |
else { | |
Node node = new Node(value); | |
Node prev = null; | |
Node next = null; | |
for (int i = 0; i < index; ++i) { | |
prev = (i == 0) ? this.head : prev.next; | |
} | |
next = prev.next; | |
prev.next = node; | |
node.next = next; | |
node.prev = prev; | |
next.prev = node; | |
++this.size; | |
} | |
} | |
} | |
/* O(1) */ | |
public Node removeAtHead() { | |
if (this.isEmpty()) { | |
return null; | |
} | |
Node node = this.head; | |
if (this.size() == 1) { | |
this.head = this.tail = null; | |
} | |
else { | |
this.head = this.head.next; | |
this.head.prev = null; | |
} | |
--this.size; | |
return node; | |
} | |
/* O(1) */ | |
public Node removeAtTail() { | |
if (this.isEmpty()) { | |
return null; | |
} | |
Node node = this.tail; | |
if (this.size() == 1) { | |
this.head = this.tail = null; | |
} | |
else { | |
this.tail = this.tail.prev; | |
this.tail.next = null; | |
} | |
--this.size; | |
return node; | |
} | |
/* O(n) */ | |
public Node removeAtIndex(int index) { | |
if (this.isEmpty()) { | |
return null; | |
} | |
else if (!(index >= 0 && index < this.size)) { | |
throw new IndexOutOfBoundsException("Index: " + index + " is out of bounds"); | |
} | |
else if (index == 0) { | |
return this.removeAtHead(); | |
} | |
else if (index == this.size - 1) { | |
return this.removeAtTail(); | |
} | |
else { | |
Node node = null; | |
Node prev = null; | |
Node next = null; | |
for (int i = 0; i < index; ++i) { | |
prev = (i == 0) ? this.head : prev.next; | |
} | |
next = prev.next.next; | |
node = prev.next; | |
prev.next = next; | |
next.prev = prev; | |
--this.size; | |
return node; | |
} | |
} | |
/* O(n) */ | |
public Node get(int index) { | |
if (this.isEmpty()) { | |
return null; | |
} | |
else if (!(index >= 0 && index < this.size)) { | |
throw new IndexOutOfBoundsException("Index: " + index + " is out of bounds"); | |
} | |
else { | |
Node node = null; | |
for (int i = 0; i <= index; ++i) { | |
node = (i == 0) ? this.head : node.next; | |
} | |
return node; | |
} | |
} | |
/* O(n) */ | |
public boolean search(E value) { | |
boolean result = false; | |
if (!this.isEmpty() && !Objects.isNull(value)) { | |
Node current = this.head; | |
while (current != null) { | |
if ((E) current.value == value) { | |
result = true; | |
break; | |
} | |
current = current.next; | |
} | |
} | |
return result; | |
} | |
public boolean isEmpty() { | |
return this.size == 0; | |
} | |
public int size() { | |
return this.size; | |
} | |
public String toString() { | |
String string = ""; | |
Node node = this.head; | |
while (node != null) { | |
string += node.next == null ? node : node + " <-> "; | |
node = node.next; | |
} | |
return string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment