100% Placement assistance on AUTOMATION TESTING WITH JAVA+selenium

Abstract Data Types for Linked Lists

Abstract Data Types for Linked Lists

Certainly! Here’s an example of implementing an abstract data type, specifically a linked list, in Java:

public class LinkedList {
private Node head;
private int size;
private static class Node {
private T data;
private Node next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
public LinkedList() {
    this.head = null;
    this.size = 0;
}
public void add(T item) {
    Node<T> newNode = new Node<>(item);
    if (head == null) {
        head = newNode;
    } else {
        Node<T> current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }
    size++;
}

public void remove(T item) {
    if (head == null) {
        System.out.println("List is empty. Cannot remove element: " + item);
        return;
    }

    if (head.data.equals(item)) {
        head = head.next;
        size--;
        return;
    }

    Node<T> current = head;
    Node<T> previous = null;
    while (current != null && !current.data.equals(item)) {
        previous = current;
        current = current.next;
    }

    if (current == null) {
        System.out.println("Element not found: " + item);
        return;
    }

    previous.next = current.next;
    size--;
}

public boolean contains(T item) {
    Node<T> current = head;
    while (current != null) {
        if (current.data.equals(item)) {
            return true;
        }
        current = current.next;
    }
    return false;
}

public int size() {
    return size;
}

public boolean isEmpty() {
    return size == 0;
}

public void printList() {
    Node<T> current = head;
    System.out.print("LinkedList: ");
    while (current != null) {
        System.out.print(current.data + " ");
        current = current.next;
    }
    System.out.println();
}

public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.printList();

    System.out.println("List size: " + list.size());

    System.out.println("Does list contain 2? " + list.contains(2));
    System.out.println("Does list contain 4? " + list.contains(4));

    list.remove(2);
    list.printList();

    System.out.println("List size: " + list.size());
    System.out.println("Is list empty? " + list.isEmpty());
}

}

Abstract Data Types for Linked Lists Explanation

In this example, we create a generic LinkedList class. It consists of a private inner class Node to represent individual nodes of the linked list. The LinkedList class has methods for adding elements, removing elements, checking if an element is present, obtaining the size, checking if the list is empty, and printing the list.
In the main method, we create an instance of the LinkedList class and perform operations like adding elements, checking if an element is present, removing elements, obtaining the size, and checking if the list is empty.
Feel free to modify and expand upon this example to suit your needs or implement other abstract data types based on linked lists.

100% Placement assistance on AUTOMATION TESTING WITH JAVA+selenium

Click Here to Leave a Comment Below 0 comments

Leave a Reply: