Posts

Showing posts with the label Data Structures

Linked List Introduction and it's Implementation - The Coding Shala

Image
Home >> Data Structures >> Linked List Introduction and its Implementation  In this post, we will learn what is linked list data structure and will implement a singly linked list in Java. Linked List Data Structure A linked list is a linear data structure. The linked list is a chain of Nodes that are connected using pointers. One Node contains the data/element and a pointer called next that points to the next node. Unlike arrays, linked list elements are not stored in a contiguous location. There are different types of the linked lists like a singly linked list, doubly linked list, and circular linked list. Here we will cover a singly linked list. The following are main terms used in a linked list: head: front node of a linked list next: pointer to the next node node: contains data/value and the next pointer We can create a linked list of different data types like integer, string, etc. Advantages and Disadvantages of linked lists The following are some advan...

How to Find the Length of a Linked List using Recursion - The Coding Shala

Home >> Data Structures >> Find the Length of a Linked List using Recursion  In this post, we will learn how to find the length of a linked list using recursion in Java. How to Find the Length of a Linked List using Recursion You have given a singly linked list, write a Java program to count the number of elements or length of the linked list using recursion. Example 1: LinkedList: 1->2->3->4->5 Output: 5 Example 2: LinkedList: 2->4->6->7->5->1->0 Output: 7 Approach 1 If the current node is null then it will be our break condition of recursion. Java Program:  /* class Node{ int data; Node next; Node(int a){ data = a; next = null; } }*/ class Solution { public static int getCount ( Node head ) { return findLength ( head ); } public static int findLength ( Node curr ) { if ( curr == null ) { return 0 ; } ...

How to Find the Length of a Linked List - The Coding Shala

Home >> Data Structures >> Find the Length of a Linked List  In this post, we will learn how to find the length of a given linked list in Java. How to Find the Length of a Linked List You have given a singly linked list, write a Java program to count the number of elements or length of the linked list. Example 1: LinkedList: 1->2->3->4->5 Output: 5 Example 2: LinkedList: 2->4->6->7->5->1->0 Output: 7 Approach 1 Traverse the linked list until you reached the null node. Java Program:  /* class Node{ int data; Node next; Node(int a){ data = a; next = null; } }*/ class Solution { public static int getCount ( Node head ) { int count = 0 ; Node curr = head ; while ( curr != null ) { count ++; curr = curr . next ; } return count ; } } Other Posts You May Like Design Linked List...

Implement Queue using Linked List - The Coding Shala

Home >> Data Structure >> Implement queue using linked list  In this post, we will learn how to implement Queue using Linked List and will write a Java Program for the same. Implement Queue using Linked List We will implement Queue using Linked List. The basic operation of the queue like push/offer and remove/poll method will implement here. Java Program:  class QueueNode { int data ; QueueNode next ; QueueNode ( int a ) { data = a ; next = null ; } } class MyQueue { QueueNode front , rear ; // This function should add an item at // rear void push ( int a ) { QueueNode newNode = new QueueNode ( a ); if ( front == null || rear == null ) { front = newNode ; rear = front ; } else { rear . next = newNode ; rear = rear . next ; } } // This function should remove front // item from queue and should re...

Implement Queue Using Array - The Coding Shala

Home >> Data Structures >> Implement Queue Using Array  In this post, we will learn how to Implement Queue Using Array and will write a Java program for the same. Implement Queue Using Array We will write a simple Java Program to implement queue using the array, Only Push and Pop method we will implement here. Java Program:  class MyQueue { int front , rear ; int arr [] = new int [ 100005 ]; MyQueue () { front = 0 ; rear = 0 ; } /* The method push to push element into the queue */ void push ( int x ) { arr [ rear ] = x ; rear ++; } /* The method pop which return the element poped out of the queue*/ int pop () { if ( front == rear ) return - 1 ; int res = arr [ front ]; front ++; return res ; } } Other Posts You May Like Queue Data Structure Circular Queue Data Structure Stack Data Structure Implement Stack Using Array Implem...

Circular Queue Data Structure - The Coding Shala

Home >> Data Structures >> Circular Queue Circular Queue Data Structure In this post, you will learn what is Circular Queue in Data Structure and how to implement a circular queue in Java using an array. The circular queue is a linear data structure in which the operations are performed based on FIFO(first in First Out) principle and the last position is connected back to the first position to make a circle. It is also called the Ring Buffer. Why we need a Circular Queue? We know that in the simple queue if we remove an element from the head and we are not going to use that space again so it will be a complete waste. To prevent this we used a circular queue. For a circular queue, we may use a fixed-sized array and two pointers to indicate the starting position and the ending position. Circular Queue Java Program The following Java program explains the Circular queue. Here, i have implemented circular queue using an array. Java Program:  ...

Queue Data Structure - The Coding Shala

Home >> Data Structures >> Queue Queue Data Structure In this post, we will see the basics of the queue. The queue  is a Data Structure, also known as the First-in-first-out data structure(FIFO). In a queue, there are two main operations: Enqueue and Dequeue. The insert Operation is called enqueue and the new element is always added at the end of the queue. The delete operation is called dequeue and we only allowed to remove the first element. Queue Java Program The following is a simple implementation of the queue:  import java.util.ArrayList ; import java.util.List ; //Queue implementation class MyQueue { //to store data private List < Integer > data ; private int q_start ; //start point of queue //initial state MyQueue (){ data = new ArrayList < Integer >(); q_start = 0 ; } //Enqueue Operation //add element at the end of queue are return true if successful public boolean EnQueue ( ...

Stack Data Structure - The Coding Shala

Home >> Data Structures >> Stack Stack Data Structure In this post, we will see the basics of the stack data structure. The stack is Data Structure also known as Last-in-first-out(LIFO) data structure. In Stack, the newest element added to the stack will be processed first. Like Queue in stack two operations are important. One is inserted operation is called a stack. A new element is always added at the end of the stack. The delete operation is called pop, will always remove the last element. The general operations we perform on the stack are below: Push: Add an item to the stack. If the stack is full then return overflow. Pop: Remove an element from the stack. If the stack is empty, then underflow. Peek / Top: Return the top element of the stack. isEmpty: Returns true if the stack is empty else false. All operations push(), pop(), isEmpty(), peek() take O(1) time. How to implement a stack? There are two ways to implement a stack: Using Array. Using Linked List....

Graph Representation using Adjacency Matrix - The Coding Shala

Image
Home >> Data Structures >> Graph Representation using Adjacency Matrix Graph Representation using Adjacency Matrix In this post, we will see how to represent a graph using the Adjacency Matrix. Generally, the Adjacency matrix is used to check if there is any edge available between two vertices or not. In the Adjacency matrix, we take a 2D array of size v*V, where V is the number of vertices in a graph and array[i][j] = 1 indicates that there is an edge between vertex i and j. The following example explains the Adjacency matrix of a graph: Graph Representation using Adjacency Matrix Java Program We have given the number of vertices 'v' and edges 'E' of a bidirectional graph. Our task is to build a graph through the adjacency matrix and print it. Example 1: Input: 5 7 0 1 0 4 1 2 1 3 1 4 2 3 3 4 Output: 0 1 0 0 1  1 0 1 1 1  0 1 0 1 0  0 1 1 0 1  1 1 0 1 0  Java Code: import java.util....