Exercise:
Write a Java Program to implement linkedlist.
1.Implement Linkedlist
Click Here to View the Solution!
public class LinkedList {
// create an object of Node class
Node head;
// static inner class
static class Node {
int value;
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(21);
Node second = new Node(22);
Node third = new Node(23);
Node fourth = new Node(24);
// connect each node of linked list to next node
linkedList.head.next = second;
second.next = third;
third.next = fourth;
// printing node-value
System.out.print("LinkedList: ");
while (linkedList.head != null) {
System.out.print(linkedList.head.value + " ");
linkedList.head = linkedList.head.next;
}
}
}
Click Here to View the Output!
LinkedList: 21 22 23 24
Click Here to View the Explanation!
- This program depicts the procedure of creating a Linked-List.
- For implementing linked-list, we create a class named
LinkedList
first. - After that we declare a
Node
type object namedhead
. - The following class contains another
inner-class
namedNode
which is ofstatic
type. - In compliance with these properties, we declare an
int
type variable that’ll contain the value, and a next variable ofNode
, that’ll point to the next node in the list. - The
Node constructor
initializes the values to the inputd
and makes the next Node null for the time being. - In the main method we instantiate an object of
LinkedList
; using that object we assign aNode
to theheader
, and create asecond
andthird
node. - Next step is to connect these nodes, where the second node is assigned to the next node of head, and the third node is linked to the next node to the second one.
- Finally, the
while loop
prints node values as long as the head node does not become null.
2.implement using LinkedList class
Click Here to View the Solution!
import java.util.LinkedList;
public class Linkedlist {
public static void main(String[] args){
// create using the LinkedList class
LinkedList<String> languages = new LinkedList<>();
// Add elements to LinkedList
languages.add("Java");
languages.addFirst("Swift");
languages.addLast("C++");
System.out.println("LinkedList: " + languages);
System.out.println("First Element: " + languages.getFirst());
System.out.println("Last Element: " + languages.getLast());
}
}
Click Here to View the Output!
LinkedList: [Swift, Java, C++] First Element: Swift Last Element: C++
Click Here to View the Explanation!
- This program depicts the procedure of creating a Linked-List.
- For implementing linked-list, we use Java provided class named
LinkedList
. - In the main method, we instantiate an object of
LinkedList
class, i.e. ofString
data type. - To simply add elements to the
LinkedList
, we useadd()
method. - To add an element to the start of the
LinkedList
, we calladdFirst()
method. - To add an element to the end of the
LinkedList
, we useaddLast()
method. - After inserting elements in the List, we print these elements.
- Here first element is fetched by calling
getFirst()
method, whereas the last element is retrieved by usinggetLast()
method ofLinkedList
class. - Consider this point that we have used
< >
brackets to create a generic type Linked-List.