
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Implement LinkedList
In this article, we will understand how to implement linked-list. The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
Below is a demonstration of the same −
Suppose our input is −
Run the program
The desired output would be −
The elements of the linked list are: 100 150 200 250
Algorithm
Step 1 - START Step 2 - Create a class with the required members. Step 3 - Define an ‘insert’ function to add elements to the list. Step 4 - In the ‘main’ method, create a new instance of the class. Step 5 - Create a list, and add elements to it using the ‘insert’ method. Step 6 - Iterate over the list, and display the value present in the current node. Step 7 - Move on to the next node and perform the same operation. Step 8 - Do this until the end of the list is reached. Step 9 - Display the result Step 10 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class Demo { Node head; static class Node { int data; Node next_element; Node(int element){ data = element; next_element = null; } } public static Demo insert(Demo input_list, int data){ Node new_node = new Node(data); new_node.next_element = null; if (input_list.head == null) { input_list.head = new_node; } else { Node last = input_list.head; while (last.next_element != null) { last = last.next_element; } last.next_element = new_node; } return input_list; } public static void main(String[] args){ Demo input_list = new Demo(); System.out.print("A linked list is declared: \n"); input_list = insert(input_list, 100); input_list = insert(input_list, 150); input_list = insert(input_list, 200); input_list = insert(input_list, 250); Node current_node = input_list.head; System.out.print("The elements of the linked list are: \n"); while (current_node != null) { System.out.print(current_node.data + " "); current_node = current_node.next_element; } } }
Output
A linked list is declared: The elements of the linked list are: 100 150 200 250
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
public class Demo { Node head; static class Node { int data; Node next_element; Node(int element){ data = element; next_element = null; } } public static Demo insert(Demo input_list, int data){ Node new_node = new Node(data); new_node.next_element = null; if (input_list.head == null) { input_list.head = new_node; } else { Node last = input_list.head; while (last.next_element != null) { last = last.next_element; } last.next_element = new_node; } return input_list; } public static void print_list(Demo input_list){ Node current_node = input_list.head; System.out.print("The elements of the linked list are: \n"); while (current_node != null) { System.out.print(current_node.data + " "); current_node = current_node.next_element; } } public static void main(String[] args){ Demo input_list = new Demo(); System.out.print("A linked list is declared: \n"); input_list = insert(input_list, 100); input_list = insert(input_list, 150); input_list = insert(input_list, 200); input_list = insert(input_list, 250); print_list(input_list); } }
Output
A linked list is declared: The elements of the linked list are: 100 150 200 250
- Related Articles
- Implement a stack from a LinkedList in Java
- Program to convert ArrayList to LinkedList in Java
- Java Program to Add elements to a LinkedList
- Java Program to Remove elements from the LinkedList
- Java Program to Access elements from a LinkedList
- Java Program to Detect loop in a LinkedList
- Java program to implement bubble sort
- Java program to implement selection sort
- Java program to implement insertion sort
- Java program to implement binary search
- Java program to implement linear search
- Java Program to Implement Multiple Inheritance
- Java Program to implement private constructors
- LinkedList in Java
- C# Program to create a LinkedList

Advertisements