Linked List Implementation In Java

Chapter: Data Structures Last Updated: 09-03-2018 16:48:13 UTC

Program:

            /* ............... START ............... */
                
class Node {
	public int data;
	public Node next;

	public void displayNode() {
		System.out.println("{ " + data + " } ");
	}
}

public class SinglyLinkedList {
	private Node first;
	private Node last;

	public SinglyLinkedList() {

	}

	public boolean isEmpty() {
		return (first == null);
	}

	// used to insert at the beginning of the list
	public void insertFirst(int data) {
		Node newNode = new Node();
		newNode.data = data;
		newNode.next = first;
		first = newNode;
	}

	public Node deleteFirst() {
		Node temp = first;
		first = first.next;
		return temp;
	}

	public void displayList() {
		System.out.println("List (first --> last) ");
		Node current = first;
		while (current != null) {
			current.displayNode();
			current = current.next;
		}
		System.out.println();
	}

	public void insertLast(int data) {
		Node current = first;
		while (current.next != null) {
			current = current.next; // we'll loop until current.next is null
		}
		Node newNode = new Node();
		newNode.data = data;
		current.next = newNode;
	}

	public static void main(String[] args) {
		SinglyLinkedList mylist = new SinglyLinkedList();
		mylist.insertFirst(100);
		mylist.insertFirst(50);
		mylist.insertFirst(99);
		mylist.insertFirst(88);
		mylist.insertLast(9999999);

		mylist.displayList();
	}
}
                /* ............... END ............... */
        

Output

List (first --> last) 
{ 88 } 
{ 99 } 
{ 50 } 
{ 100 } 
{ 9999999 }

Notes:

  • We can assume java linked list structure as a train with bogie and each bogie is connected. For each bogie we can call it as Node in Java. Each node has a data element and pointer to the next element. Variable data stores the value of the element and next stores the reference to the next node.
  • In the above program please go the Node class with data and next reference variable for better understanding about Node creation.
  • SinglyLinkedList mylist = new SinglyLinkedList() will create a linkedlist with empty reference of first and last.
  • isEmpty() will check whether linkedlist is empty or not (If first == null list will be empty).
  • insertFirst(int data) using the insertFirst function we can insert data in the front of linkedlist. For inserting data first we create a new node and initialize data value and reference to the new node will the next first.
  • insertLast(int data) like wise insert in front insertlast will insert data at the last of linkedlist. First we will create a new node and data variable fill with a value, and point the last current node next value to the reference of new node created.
  • In singly linkedlist nodes are connected to its immediate next node with reference variable (Node next). If the last node links to null, it shows that the list is empty. For this kind of list Only one-way traversal is possible.

Tags

Linkedlist Implementation, Java linkedlist,singly linkedlist, Data Structures

Similar Programs Chapter Last Updated
HashMap Implementation In Java Data Structures 07-07-2018
Queue Implementation In Java Data Structures 22-09-2018
Stack Implementation In Java Data Structures 22-09-2018
Binary Search Tree Java Data Structures 11-02-2018
Insertion Sort In Java Data Structures 10-02-2018
Java Stack Example Data Structures 16-10-2017
Java String Array Sort Data Structures 31-03-2017
Java Hashmap Add Key And Value Data Structures 25-03-2017
Java Binary Tree Vertical Sum Data Structures 11-11-2016
Java Binary Tree Boundary Traversal Data Structures 11-11-2016
Java Binary Tree Lowest Common Ancestor (LCA) Data Structures 11-11-2016
Java Binary Tree Maximum Element Data Structures 11-11-2016
Java Three Dimensional Array Data Structures 28-10-2016
Java Infix Expression To Postfix Expression Data Structures 25-10-2016
Java Linked List Add Element First And Last Data Structures 24-10-2016
Java Maximum Element From Vector Data Structures 24-10-2016
Java Binary Search On Vector Data Structures 24-10-2016
Java Get Elements Of LinkedList Data Structures 23-10-2016
Java Linked List Update Element Data Structures 23-10-2016
Java Delete Elements From LinkedList Data Structures 23-10-2016
Java Double Ended Queue Data Structures 09-10-2016
Java Dynamic Queue Using Array Data Structures 26-09-2018
Java Queue Array Based Implementation Data Structures 07-10-2016
Java Sort Short Array Data Structures 25-09-2016
Java Sort Long Array Data Structures 25-09-2016
Java Sort Int Array Data Structures 19-09-2016
Java Sort Float Array Data Structures 19-09-2016
Java Sort Double Array Data Structures 19-09-2016
Java Sort Char Array Data Structures 19-09-2016
Java Sort Byte Array Data Structures 19-09-2016

1 2 3