Make Selenium Easy

1. Data Structure For Testers – Implement Linked List in Java

Introduction

As a part of the Data structure for Testers in this post, we will learn to implement a Linked List data structure in Java.

What is Node?

A node is a building block. We can relate it with a brick. When we use multiple bricks together we can create a structure. Similarly, we can use multiple nodes together to create a structure like LinkedList, Tree, etc.

Image Source – Google Image

A red brick isolated on white.

In the programming world, a node may have two, three, or many compartments. If we talk about a node with two compartments then the first compartment will hold data and the second compartment will be used to store the address of the next node to form a structure.

If we talk about a node with three compartments then the first compartment will store the address of a previous node while the third compartment will have an address of the next node. A middle node will store data.

A node may store multiple data compartments as well.

This feature of Node allows us to create a structure whose nodes or elements are stored in non-contiguous locations.

Java class to represent a Node

Let’s focus on a node with two compartments as of now. So a node can hold any type of data such as int, string, etc. When I say that a Node holds the address of another node i.e. a pointer to the next node.

What is a Linked List?

A LinkedList is a linear data structure whose elements may not be stored at contagious locations. A linear data structure where elements are arranged in a sequential manner and each element may have its previous or next element information. For example – Array, stack, queue, etc.

As shown above a LinkedList a group of nodes and has head node and tail node. Head node will be starting of List while Tail node is the last node of List. I have represented how a node will point to another node to make a sequnetial node. Please note no other node holds address of Head node which makes a starting point. Last node also does not hold address of any other node to make end of list. This is also called Singly Linked List.

Implementing a Linked List in Java

Let’s think of a logic to build a LinkedList.

  1. A LinkedList will have a head and tail.
  2. When list is empty then Head and Tail will point to null.
  3. If there is only one node then both Head and Tail will point to the same node.

Logic to add a new Node

If List is empty then simple Head will point to new node and since there is only one element so Tail will also point to same new node like Head.

If list is not empty, then the existing Tail Node will not point to NULL any more and will point to new Node. This logic can be understand from below diagram:-

Java Program

Below is a self explantory Java program:-

Output

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

1. Data Structure For Testers – Implement Linked List in Java

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: