0% found this document useful (0 votes)
14 views9 pages

5 Linked List

The document provides a comprehensive overview of linked lists, detailing their structure, types (singly, doubly, and circular), and various operations such as insertion, deletion, and traversal. It includes Java code examples for implementing these operations using both custom classes and the standard library's LinkedList class. Additionally, it discusses applications of linked lists in dynamic memory allocation and data structure implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

5 Linked List

The document provides a comprehensive overview of linked lists, detailing their structure, types (singly, doubly, and circular), and various operations such as insertion, deletion, and traversal. It includes Java code examples for implementing these operations using both custom classes and the standard library's LinkedList class. Additionally, it discusses applications of linked lists in dynamic memory allocation and data structure implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Linked Lists

Table of Contents

1. Introduction to Linked Lists


2. Memory Representation of Linked Lists
3. Traversing a Linked List
4. Insertion into Linked List (Sorted and Unsorted)
5. Deleting Elements from Linked List
6. Operations on Doubly Linked Lists
7. Circular Linked Lists & Applications

1. Introduction to Linked Lists

 A Linked List is a linear data structure where each element (node) contains data and
a reference (or link) to the next node in the sequence.
 Types of Linked Lists:
o Singly Linked List
o Doubly Linked List
o Circular Linked List

2. Memory Representation of Linked Lists

 Node Structure:
o Data: Holds the actual data.
o Next Pointer (Singly Linked List): Points to the next node in the sequence.
o Prev Pointer (Doubly Linked List): Points to the previous node.
 Each node in a linked list is dynamically allocated and connected using pointers.

3. Traversing a Linked List

Java Code for Traversing a Singly Linked List:

// Class to represent a Node


class Node {
int data;
Node next;

// Constructor to initialize node


public Node(int data) {
this.data = data;
this.next = null;
}
}
// Class to represent the Linked List
class LinkedList {
Node head; // Head node of the linked list

// Method to traverse the list and print each element


public void traverse() {
Node current = head; // Start at the head
while (current != null) { // Traverse till end of list
System.out.print(current.data + " "); // Print node data
current = current.next; // Move to next node
}
System.out.println(); // Move to the next line after traversal
}
}

public class Main {


public static void main(String[] args) {
LinkedList list = new LinkedList(); // Create a new linked list
list.head = new Node(1); // Add nodes
list.head.next = new Node(2);
list.head.next.next = new Node(3);

list.traverse(); // Output: 1 2 3
}
}

using the java.util.LinkedList class (standard library) to manage the list:


import java.util.*; // Import java.util package

public class Main {


public static void main(String[] args) {
// Create a new LinkedList using java.util.LinkedList
LinkedList<Integer> list = new LinkedList<>();

// Add elements to the list


list.add(1);
list.add(2);
list.add(3);

// Traverse and print the list using for-each loop


for (Integer data : list) {
System.out.print(data + " "); // Output: 1 2 3
}
System.out.println(); // Move to the next line after traversal
}
}

4. Insertion into Linked List

 Unsorted Linked List:


o Insert at the beginning, middle, or end.

Java Code for Insertion at the Beginning (Unsorted):

// Node class to represent a single node in the linked list


class Node {
int data;
Node next;

// Constructor to initialize node


public Node(int data) {
this.data = data;
this.next = null;
}
}

class LinkedList {
Node head; // Head node of the linked list

// Insert at the beginning of the list


public void insertAtBeginning(int data) {
Node newNode = new Node(data); // Create a new node
newNode.next = head; // Point new node to current head
head = newNode; // Move head to new node
}

// Sorted Insertion (Ascending Order)


public void insertSorted(int data) {
Node newNode = new Node(data); // Create a new node
if (head == null || head.data >= data) {
newNode.next = head;
head = newNode;
} else {
Node current = head;
while (current.next != null && current.next.data < data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
}

// Method to traverse and print the list


public void traverse() {
Node current = head;
while (current != null) {
System.out.print(current.data + " "); // Print node data
current = current.next; // Move to next node
}
System.out.println(); // Move to next line after traversal
}
}

public class Main {


public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insertAtBeginning(3); // Insert at the beginning
list.insertAtBeginning(2); // Insert at the beginning
list.insertAtBeginning(1); // Insert at the beginning
list.insertSorted(4); // Sorted insertion

list.traverse(); // Output: 1 2 3 4
}
}
Using java.util.LinkedList with import java.util.*;
import java.util.*; // Import the java.util package
public class Main {
public static void main(String[] args) {
// Create a new LinkedList using java.util.LinkedList
LinkedList<Integer> list = new LinkedList<>();

// Insert elements at the beginning using addFirst()


list.addFirst(3);
list.addFirst(2);
list.addFirst(1);

// Sorted insertion (ascending order)


insertSorted(list, 4);

// Traverse and print the list


for (Integer data : list) {
System.out.print(data + " "); // Output: 1 2 3 4
}
System.out.println(); // Move to the next line after traversal
}

// Method to insert a new element in sorted order (ascending)


public static void insertSorted(LinkedList<Integer> list, int data) {
ListIterator<Integer> iterator = list.listIterator();

// Traverse the list to find the position to insert


while (iterator.hasNext()) {
if (iterator.next() > data) {
iterator.previous();
iterator.add(data);
return;
}
}
// If the data is greater than all elements, add at the end
list.addLast(data);
}
}

5. Deleting Elements from Linked List

Java Code for Deletion at the Beginning:

// Node class to represent a single node in the linked list


class Node {
int data;
Node next;

// Constructor to initialize the node


public Node(int data) {
this.data = data;
this.next = null;
}
}

class LinkedList {
Node head; // Head node of the linked list

// Insert at the beginning of the list


public void insertAtBeginning(int data) {
Node newNode = new Node(data); // Create a new node
newNode.next = head; // Point new node to current head
head = newNode; // Move head to new node
}

// Delete at the beginning of the list


public void deleteAtBeginning() {
if (head != null) {
head = head.next; // Move head to the next node
}
}

// Delete at the end of the list


public void deleteAtEnd() {
if (head == null) return; // Empty list check

if (head.next == null) {
head = null; // If only one element, delete the head
} else {
Node current = head;
while (current.next != null && current.next.next != null) {
current = current.next; // Traverse to the second-last
node
}
current.next = null; // Remove the last node
}
}

// Method to traverse and print the list


public void traverse() {
Node current = head;
while (current != null) {
System.out.print(current.data + " "); // Print node data
current = current.next; // Move to next node
}
System.out.println(); // Move to next line after traversal
}
}

public class Main {


public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insertAtBeginning(1); // Insert at the beginning
list.insertAtBeginning(2); // Insert at the beginning
list.insertAtBeginning(3); // Insert at the beginning

list.deleteAtBeginning(); // Deletes 3
list.deleteAtEnd(); // Deletes 1

list.traverse(); // Output: 2
}
}
Here's the rewritten code using java.util.LinkedList:
import java.util.*; // Import java.util package

public class Main {


public static void main(String[] args) {
// Create a new LinkedList using java.util.LinkedList
LinkedList<Integer> list = new LinkedList<>();

// Insert elements at the beginning using addFirst()


list.addFirst(1);
list.addFirst(2);
list.addFirst(3);

// Delete at the beginning


list.removeFirst(); // Deletes 3

// Delete at the end


list.removeLast(); // Deletes 1

// Traverse and print the list using for-each loop


for (Integer data : list) {
System.out.print(data + " "); // Output: 2
}
System.out.println(); // Move to the next line after traversal
}
}

6. Operations on Doubly Linked Lists

Java Code for Traversing a Doubly Linked List:

// Class to represent a node in a Doubly Linked List


class DoublyNode {
int data;
DoublyNode next;
DoublyNode prev;

public DoublyNode(int data) {


this.data = data;
this.next = null;
this.prev = null;
}
}

class DoublyLinkedList {
DoublyNode head;

public void traverseForward() {


DoublyNode current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

public void traverseBackward() {


DoublyNode current = head;
if (current == null) return;

while (current.next != null) {


current = current.next; // Traverse to the last node
}

while (current != null) {


System.out.print(current.data + " ");
current = current.prev; // Traverse backward
}
System.out.println();
}
}

public class Main {


public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
DoublyNode node1 = new DoublyNode(1);
DoublyNode node2 = new DoublyNode(2);
DoublyNode node3 = new DoublyNode(3);

list.head = node1;
node1.next = node2;
node2.prev = node1;
node2.next = node3;
node3.prev = node2;

list.traverseForward(); // Output: 1 2 3
list.traverseBackward(); // Output: 3 2 1
}
}

Updated Code Using java.util.LinkedList:


import java.util.*; // Import java.util package

public class Main {


public static void main(String[] args) {
// Create a new LinkedList using java.util.LinkedList
LinkedList<Integer> list = new LinkedList<>();

// Add elements to the list


list.add(1);
list.add(2);
list.add(3);

// Traverse and print the list forward (natural order)


System.out.print("Forward Traversal: ");
for (Integer data : list) {
System.out.print(data + " ");
}
System.out.println(); // Move to the next line after traversal

// Traverse and print the list backward (reverse order)


System.out.print("Backward Traversal: ");
ListIterator<Integer> iterator = list.listIterator(list.size());
while (iterator.hasPrevious()) {
System.out.print(iterator.previous() + " ");
}
System.out.println(); // Move to the next line after traversal
}
}

7. Circular Linked Lists and Applications

Java Code for Circular Linked List Traversal:

class CircularNode {
int data;
CircularNode next;

public CircularNode(int data) {


this.data = data;
this.next = null;
}
}

class CircularLinkedList {
CircularNode head;

public void traverse() {


if (head == null) return;

CircularNode current = head;


do {
System.out.print(current.data + " ");
current = current.next;
} while (current != head);
System.out.println();
}
}

public class Main {


public static void main(String[] args) {
CircularLinkedList list = new CircularLinkedList();
CircularNode node1 = new CircularNode(1);
CircularNode node2 = new CircularNode(2);
CircularNode node3 = new CircularNode(3);

list.head = node1;
node1.next = node2;
node2.next = node3;
node3.next = node1; // Circular link

list.traverse(); // Output: 1 2 3
}
}

Updated Code Using java.util.LinkedList:


import java.util.*; // Import java.util package

public class Main {


public static void main(String[] args) {
// Create a new LinkedList using java.util.LinkedList
LinkedList<Integer> list = new LinkedList<>();

// Add elements to the list


list.add(1);
list.add(2);
list.add(3);

// Traverse the list, simulating circular traversal (once through


the list)
System.out.print("Circular Traversal: ");

// Use an iterator to traverse the list


Iterator<Integer> iterator = list.iterator();

// Traverse the list and print the elements


while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}

// Print the first element to simulate the circular nature


System.out.print(list.getFirst() + " "); // Print the first element
at the end

System.out.println(); // Move to the next line after traversal


}
}

Applications of Linked Lists

 Dynamic Memory Allocation: Efficient memory usage for dynamic data structures.
 Implementing Stacks and Queues: Linked lists provide dynamic sizing.
 Circular Linked Lists: Real-time systems and buffer management.

Conclusion

Linked lists are fundamental data structures used in a wide range of applications. They are
especially useful in dynamic memory management, and their flexibility makes them ideal for
tasks where elements are frequently added or removed.

You might also like