Skip to content

Commit

Permalink
Implemented singly linked list queue in java
Browse files Browse the repository at this point in the history
  • Loading branch information
619frank committed Jun 17, 2024
1 parent 4550646 commit f877b3d
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions data_structures/04. Queues/java/SinglyLinkedListQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Node{
int val;
Node next;
Node(int val){
this.val = val;
this.next = null;
}
}

public class SinglyLinkedListQueue {
int length;
Node head;
Node tail;

public SinglyLinkedListQueue(){
this.head = null;
this.tail = null;
this.length = 0;
}

private void enqueue(int val){
Node newNode = new Node(val);

if(this.length == 0){
this.head = newNode;
}else{
this.tail.next = newNode;

}
this.tail = newNode;
this.length++;
}

private Integer dequeue(){

if(this.length == 0){
return null;
}

Node node = this.head;
if(this.length == 1){
this.head = null;
this.tail = null;
}else{
this.head = this.head.next;
}

node.next = null;
length--;
return node.val;
}

public static void main(String[] args) {
SinglyLinkedListQueue sllq = new SinglyLinkedListQueue();
sllq.enqueue(0);
sllq.enqueue(2);
sllq.enqueue(3);
System.out.println(sllq.dequeue());
System.out.println(sllq.dequeue());
System.out.println(sllq.dequeue());
System.out.println(sllq.dequeue());
}
}

0 comments on commit f877b3d

Please sign in to comment.