-
-
Save sricharankrishnan/e4a566c50b0d7167590050c25fa6f18f to your computer and use it in GitHub Desktop.
A queue (singly linked list based) 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
/** | |
* Queue Data Structure (Singly Linked List Implementation) | |
* | |
* Insert/Enqueue: O(1) | |
* Remove/Dequeue: O(1) | |
* Peek: O(1) | |
* Search: O(n); | |
*/ | |
import java.util.*; | |
public class LinkedListQueue<E> { | |
/* node class */ | |
private class Node<E> { | |
private E value; | |
private Node next; | |
public Node(E value) { | |
this.next = null; | |
this.value = value; | |
} | |
public Node(Node next, E value) { | |
this.next = next; | |
this.value = value; | |
} | |
public E getValue() { | |
return this.value; | |
} | |
public Node getNext() { | |
return this.next; | |
} | |
public void setValue(E value) { | |
this.value = value; | |
} | |
public void setNext(Node next) { | |
this.next = next; | |
} | |
public String toString() { | |
return "[Node:" + this.value + "]"; | |
} | |
} | |
/* linked list queue props */ | |
private int size; | |
private Node head; | |
private Node tail; | |
public LinkedListQueue() { | |
this.head = this.tail = null; | |
this.size = 0; | |
} | |
/* O(1) */ | |
public void enqueue(E value) { | |
if (!Objects.isNull(value)) { | |
Node node = new Node(value); | |
if (this.isEmpty()) { | |
this.head = this.tail = node; | |
} | |
else { | |
this.tail.setNext(node); | |
this.tail = node; | |
} | |
++this.size; | |
} | |
} | |
/* O(1) */ | |
public Node dequeue() { | |
if (this.isEmpty()) { | |
return null; | |
} | |
Node node = this.head; | |
--this.size; | |
if (this.isEmpty()) { | |
this.head = this.tail = null; | |
} | |
else { | |
this.head = this.head.getNext(); | |
} | |
node.setNext(null); | |
return node; | |
} | |
/* O(1) */ | |
public Node peek() { | |
return (this.isEmpty()) ? null : this.head; | |
} | |
/* O(n) */ | |
public int search(E value) { | |
int index = -1; | |
if (!this.isEmpty()) { | |
Node node = this.head; | |
int counter = 0; | |
while (node != null) { | |
if ((E) node.getValue() == value) { | |
index = counter; | |
break; | |
} | |
++counter; | |
node = node.getNext(); | |
} | |
} | |
return index; | |
} | |
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.getNext() == null) ? node : node + " >> "; | |
node = node.getNext(); | |
} | |
return string; | |
} | |
public static void main(String[] args) { | |
/* remove comment to see how this works | |
LinkedListQueue queue = new LinkedListQueue(); | |
queue.enqueue(1); | |
queue.enqueue(12); | |
queue.enqueue(3); | |
queue.enqueue(29); | |
queue.dequeue(); | |
System.out.println("Search: " + queue.search(29)); | |
System.out.println(queue); | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment