Write a Java Program to Implement LinkedList

Java Program to Implement LinkedList

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Implement LinkedList
Java Program to Implement LinkedList

Write a Java Program to Implement LinkedList

PROGRAM: Java Program to Implement LinkedList

/*Java Program to Implement LinkedList*/

1: Java program to implement LinkedList

class LinkedList {

// create an object of Node class
// represent the head of the linked list
Node head;

// static inner class
static class Node {
int value;

// connect each node to next node
Node next;

Node(int d) {
value = d;
next = null;
}
}

public static void main(String[] args) {

// create an object of LinkedList
LinkedList linkedList = new LinkedList();

// assign values to each linked list node
linkedList.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);

// connect each node of linked list to next node
linkedList.head.next = second;
second.next = third;

// printing node-value
System.out.print(“LinkedList: “);
while (linkedList.head != null) {
System.out.print(linkedList.head.value + ” “);
linkedList.head = linkedList.head.next;
}
}
}

2: Implement LinkedList using LinkedList class

import java.util.LinkedList;

class Main {
public static void main(String[] args){

// create a linked list using the LinkedList class
LinkedList<String> animals = new LinkedList<>();

// Add elements to LinkedList
animals.add(“Dog”);

// add element at the beginning of linked list
animals.addFirst(“Cat”);

// add element at the end of linked list
animals.addLast(“Horse”);
System.out.println(“LinkedList: ” + animals);

// access first element
System.out.println(“First Element: ” + animals.getFirst());

// access last element
System.out.println(“Last Element: ” + animals.getLast());
}
}

 



Leave a Reply

Your email address will not be published. Required fields are marked *