Posts

Mastering Queues in Java: A Step-by-Step Implementation (Array Based)

Image
Mastering Queues in Java Mastering Queues in Java: A Step-by-Step Implementation (Array Based) In this tutorial, we’ll implement an Array-Based Queue in Java. The code includes detailed comments to explain what each part does. Queue Class // Queue class manages all operations using an array public class Queue { private int[] queueArray; // Array to hold queue elements private int front; // Index of the front element private int rear; // Index of the rear element private int capacity; // Maximum size of the queue private int currentSize; // Current number of elements // Constructor initializes the queue with given capacity public Queue(int capacity) { this.capacity = capacity; this.queueArray = new int[capacity]; this.front = 0; this.rear = -1; this.currentSize = 0; } // Check if the queue is empty public boolean isEmpty() { return this.curren...

Mastering Stacks in Java: A Step-by-Step Implementation (Array Based)

Image
Mastering Stacks in Java Mastering Stacks in Java: A Step-by-Step Implementation (Array Based) In this tutorial, we’ll implement an Array-Based Stack in Java. The code includes detailed comments to explain what each part does. Stack Class // Stack class manages all operations using an array public class Stack { private int[] stackArray; // Array to hold stack elements private int top; // Index of the top element private int capacity; // Maximum size of the stack // Constructor initializes the stack with given capacity public Stack(int capacity) { this.capacity = capacity; this.stackArray = new int[capacity]; this.top = -1; // Indicates empty stack } // Check if the stack is empty public boolean isEmpty() { return this.top == -1; } // Check if the stack is full public boolean isFull() { return this.top == this.capacity - 1; } // Push an element onto...

Circular Singly Linked List in Java | Full Example & Explanation

Image
Circular Singly Linked List in Java | Full Example & Explanation Circular Singly Linked List in Java – Full Example with Step-by-Step Explanation Introduction A Circular Singly Linked List (CSLL) is similar to a Singly Linked List, but the next pointer of the last node points back to the head node. This creates a circular structure, allowing continuous traversal without null termination. Java Code with Step-by-Step Explanation // Node class for CSLL class Node { int data; // Stores node value Node next; // Reference to next node Node(int data){ this.data = data; this.next = null; } } Step: Define a node with data and next . In CSLL, next of the last node will point back to the head. // Circular Singly Linked List class class CircularSinglyLinkedList { Node head; // start of the list // Insert at end public voi...

Doubly Linked List in Java | Full Example Code & Explanation

Image
Doubly Linked List in Java | Full Example Code & Explanation Doubly Linked List in Java – Full Example with Step-by-Step Explanation Introduction A Doubly Linked List (DLL) allows navigation forward and backward. Each node has data , a prev pointer to the previous node, and a next pointer to the next node. This makes insertion and deletion faster than Singly Linked List in the middle of the list. Java Code with Step-by-Step Explanation // Node class for DLL class Node { int data; // Stores node value Node prev; // Reference to previous node Node next; // Reference to next node Node(int data) { // Constructor this.data = data; this.prev = null; this.next = null; } } Step: We define the node structure. Each node knows its data , prev and next . // DLL class class DoublyLinkedList { Node head; // start o...

How to Implement Linked List in Java | Insertion & Deletion Explained

Image
Mastering Linked Lists in Java Mastering Linked Lists in Java: A Step-by-Step Implementation (Singly Linked List) In this tutorial, we’ll implement a Singly Linked List in Java. The code includes detailed comments to explain what each part does. Node Class // Node represents each element in the Linked List public class Node { int data; // Stores the value Node next; // Points to the next node // Constructor initializes data and sets next to null public Node(int data) { this.data = data; this.next = null; } } LinkedList Class // LinkedList class manages all operations public class LinkedList { Node head; // The first node in the list // Constructor initializes the head as null (empty list) public LinkedList() { this.head = null; } // Check if the list is empty public boolean isEmpty() { return this.head == null; } // Insert a node at the beginning publ...

Fundamentals of Data Structures & Algorithms (DSA) — Beginner Friendly Guide

Image
DSA Fundamentals — Basics DSA Fundamentals This post covers the basic concepts of Data Structures and Algorithms (DSA) in simple language with clear examples. What is DSA? Data Structures are ways of organizing data, and Algorithms are methods to operate on that data efficiently. Together they form the backbone of problem-solving in programming. Big-O Complexity (Performance) O(1): Constant time (e.g., accessing array element) O(log n): Logarithmic time (e.g., binary search) O(n): Linear time (e.g., traversing an array) O(n log n): Efficient sorting (e.g., merge sort) O(n²): Nested loops (e.g., bubble sort) Common Data Structures Array: Fast random access, fixed size Linked List: Flexible size, slower access Stack: Last In First Out (Undo operations) Queue: First In First Out (Task scheduling) Hash Table: Fast lookups using key...

DSA Mini Q&A Module – Arrays, ArrayList, Linked List, and Stack

Image
DSA Mini Q&A Module DSA Mini Q&A Module 1. Arrays Q1: What is an array? An array is a collection of elements of the same type stored in contiguous memory locations. Each element is accessed by an index starting from 0. Q2: Advantages of arrays Fast access via index (O(1)) Easy to iterate Q3: Disadvantages of arrays Fixed size (cannot grow/shrink dynamically) Inserting/deleting in middle is expensive (O(n)) Q4: Array vs ArrayList Feature Array ArrayList Size Fixed Dynamic Data type Same type (primitive or object) Objects only Memory Contiguous Flexible, resizable Performance Faster for access Slightly slower due to dynamic resizing 2. ArrayList Q1: What is an ArrayList? A resizable array in Java, part of java.util, which can grow ...