Sunday, 24 November 2019

LinkList internal working in java


LinkedList in JavaLinked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node.




Dear developers, here you can easily understand the internal working of the Linklist.


public class MyLinkedList {

    private Node head;
    private int count;

    public MyLinkedList(){
        head = new Node(null);
        count = 0;
    }

    public void add(Object data) {
        Node temp = new Node(data);
        Node current = head;
        while (current.getNext() != null) {
            current = current.getNext();
        }
        current.setNext(temp);
        count++;
    }

    public void add(Object data, int index) {
        Node temp = new Node(data);
        Node current = head;
        for (int i = 0; i < index && current.getNext() != null ; i++) {
            current = current.getNext();
        }
        temp.setNext(current.getNext());
        current.setNext(temp);
        count++;
    }

    public Object get(int index) {
        if (index < 0) {
            return null;
        }
        Node current = head.getNext();
        for (int i = 0; i<index; i++) {
            if (current.getNext() == null)
                return null;
            current = current.getNext();
        }
        return current.getData();
    }

    public boolean remove(int index) {
        if(index < 0 && index > count) {
            return false;
        }
        Node current = head;
        for (int i = 0; i < index; i++) {
            if (current.getNext() == null)
                return false;
            current = current.getNext();
        }
        current.setNext(current.getNext().getNext());
        count--;
        return true;
    }

    public boolean contains(Object obj) {
        Node current = head;
        for (int i = 0; i< count; i++) {
            if (current.getNext().getData().equals(obj))
                return true;
            current = current.getNext();
        }
        return false;
    }

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

    public int size() {
        return count;
    }
}

class Node {
    Node next;
    Object data;
    public Node(Object datas) {
        this.data = datas;
        this.next = null;
    }
    public Node(Object data, Node next) {
        this.data = data;
        this.next = next;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
}


public class MainClass {
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.add("Sam");
        list.add("Samset");
        System.out.println(list.size());
        System.out.println(list.isEmpty());
        System.out.println(list.contains("Sam"));
        System.out.println(list.get(1));
    }
}

No comments:

Post a Comment