stock market

What is a Linked List? Types, Operations, and Real-World Examples

Spread the love

Why Do We Need Linked Lists?

In real programming, data size is often unknown or frequently changing. Arrays create problems in such situations. Arrays Issues are .

  1. Fixed size problems
    • Array size must be decided at creation
    • Can not easily grow or shrink .
  2. Memory Wastage
    • if array size is large but data is small -> unused memory wastage .
  3. Costly Insertion & Deletion
    • Inserting or deleting in the middle requires shifting elements.
    • Time complexity becomes high.
  4. Continuous Memory Requirement
    • Requires contiguous memory block.
    • Large arrays may fail if continuous space not available.

To overcome the above issues LinkedList comes into the pictures .

Linked Lists are fundamental data structure in computer science and they offer several advantages .

  1. Dynamic size : Unlike Arrays , linked lists can grow and shrink dynamically for large datasets . this is because Linked lists do not require shifting elements .
  2. Memory utilization: Linked Lists use memory for more efficient for certain applications. they allocated memory as needed which can be beneficial when dealing with large or unpredictable data .
  3. Efficient Insertions/Deletions : Adding or removing elements from Linked List is more efficient than Arrays , especially for large datasets . this is because Linked Lists do not require shifting .
  4. Ease of Concatenation : Concatenating two Linked Lists is straight forward and efficient , unlike arrays where it can be more complex and time consuming .

Types of LinkedList

  1. Singly Linked List
  2. Doubly Linked List
  3. Circular Linked List
  4. Doubly Linked List

1. Singly Linked List

A Singly Linked List is a type of linked list data structure in which each node contains data and a reference (pointer) to the next node only. It is called singly because every node connects in one direction — from the current node to the next node.

Structure of Singly Linked List

Each node has two parts:

  1. Data → stores the actual value
  2. Next Pointer → stores the address of the next node
package com.test.rkdigital.school.linkedlist;
public class Node {

	int data; // store the data 
	Node next;  //refrence to the next node 

	public Node(int data) {
		// TODO Auto-generated constructor stub
		this.data=data;
		this.next=null;   // initialize the next pointer to null
	}
}
1. Linked List Insertion at Beginning: Algorithm

This Java program demonstrates how to insert data at the beginning of a Singly Linked List and display all elements using traversal.

Example :

package com.test.rkdigital.school.linkedlist;

public class LinkedList {

	Node head;
	static class Node{
		int data;
		Node next;
		Node(int data){
			this.data=data;
			this.next=null;
		}
	}
	
	// Method to insert new Node 
	public void insertAtBegining(int newData) {
		
		// Create a new node with the given data 
		Node newNode= new Node(newData);
		
		//Make the new node point to the current head 
		newNode.next=head;
		
		//update the Head to be the new node 
		head=newNode;
		
	}
	
	//Method to print the Linked list 
	public void printList() {
		Node currentNode=head;
		while(currentNode !=null) {
			System.out.println(currentNode.data);
			currentNode=currentNode.next;
		}
	}
	
	public static void main(String[] args) {
		
		LinkedList list= new LinkedList();
		
		//Insert Node at the Beginning 
		list.insertAtBegining(10);
		list.insertAtBegining(20);
		list.insertAtBegining(30);

		list.printList();
	}
}

Leave a Reply

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