Singly Linked List Implemenation in Java ( Java LinkedList ) - JavaTechWorld

Monday, 29 May 2017

Singly Linked List Implemenation in Java ( Java LinkedList )

    0

Introduction - Linked Lists are a very common way of storing arrays of data. The major benefit of linked lists is that you do not specify a fixed size for your list. The more elements you add to the chain, the bigger the chain gets.
There is more than one type of a linked list, although for the purpose of this tutorial, we'll stick to singly linked lists (the simplest one). If for example you want a doubly linked list instead, very few simple modifications will give you what you're looking for. Many data structures (e.g. Stacks, Queues, Binary Trees) are often implemented using the concept of linked lists. Some different types of linked lists are shown below:
Singly Linked List - Root nodes linked one way through all the nodes last node link to null.
Implemenation of Singly Linked List in Java -

package linked_list.singly_linked_list;

/** * @author namdeva *  */public class Singly_list {
    Node head;
    Node root;
    static int count = 0;

    public void addAtBegin(int begin) {
        Node beginNode = new Node();
        beginNode.setData(begin);
        beginNode.setNext(head);
        head = beginNode;
        count++;
    }

    public void addAtPosition(int position, int data) {
        if (position == 1) {
            addAtBegin(data);
        } else if (position == count + 1) {
            add(data);
        } else if (position > count + 2) {
            System.out.println("No such position exit");
            return;
        } else {
            int localCount = 2;
            Node newNode = head;
            Node temp = head; // 5            while (temp != null) { // 5                newNode = temp.getNext();// 20                if (localCount == position) {
                    Node newNodeCreation = new Node();
                    newNodeCreation.setData(data);
                    newNodeCreation.setNext(newNode);
                    temp.setNext(newNodeCreation);
                    count++;
                }
                temp = temp.getNext();// 10                localCount++;// 3            }
        }

    }

    public void add(int data) {
        Node newNode = new Node();
        newNode.setData(data);
        if (head == null) {
            head = newNode;
            root = newNode;
            count++;
        } else {
            root.setNext(newNode);
            root = newNode;
            count++;
        }
    }

    public void display() {
        Node display = head;
        while (display != null) {
            System.out.print(display.getData() + " ");
            display = display.getNext();
        }
        System.out.println("");
        System.out.println("count = " + count);

    }

    public void deleteFirst() {
        Node nextHead;
        nextHead = head.getNext();
        head.setNext(null);
        head = nextHead;
        count--;

    }

    public void deleteLast() {
        Node prevNode = head;
        Node tempNode = head;
        while (prevNode.getNext() != null) {
            tempNode = tempNode.getNext();
            if (tempNode.getNext() == null) {
                prevNode.setNext(null);
                count--;
            }
            prevNode = tempNode;

        }
    }

    public void deletePosition(int index) {
        Node prevNode = head;
        Node currentNode = head;
        Node nextNode = head;
        int localCount = 2;
        int initCount = count;
        if (index == 1) {
            deleteFirst();
        } else if (index == count) {
            deleteLast();
        } else if (index > count + 1) {
            System.out.println("No such position exit");
            return;
        } else {
            while (initCount > 0) {
                prevNode = currentNode;// 10                currentNode = currentNode.getNext();// 15                nextNode = currentNode.getNext();// 20                if (index == localCount) {
                    prevNode.setNext(nextNode);
                    currentNode.setNext(null);
                    count--;
                    return;
                }
                localCount++;
                initCount--;

            }
        }
    }

    public boolean contain(int data) {
        boolean flag = false;
        Node temp = head;
        while (temp != null) {
            if (temp.getData() == data) {
                flag = true;
                System.out.println(flag);
                return flag;
            } else {
                temp = temp.getNext();
            }
        }
        if (!flag)
            System.out.println(flag);
        return flag;
    }

    public void replace(int index, int data) {
        Node temp = head;
        int localCount = 1;
        if (index > count) {
            System.out.println("No such index exit");
            return;
        }
        while (temp != null) {
            if (localCount == index) {
                temp.setData(data);
                return;
            } else {
                temp = temp.getNext();
                localCount++;
            }
        }

    }

    public static void main(String args[]) {
        Singly_list ls = new Singly_list();
        ls.add(10);
        ls.add(20);
        ls.add(30);
        ls.add(40);
        ls.add(50);
        ls.addAtBegin(5);
        ls.addAtPosition(3, 15);
        ls.display();
        ls.deleteFirst();
        ls.display();
        ls.deleteLast();
        ls.display();
        ls.deletePosition(2);
        ls.display();
        ls.contain(40);
        ls.display();
        ls.replace(5, 15);
        ls.display();   

    }

}

class Node {

    int data;

    /**     * @return the data     */    public final int getData() {
        return data;
    }

    /**     * @param data     *            the data to set     */    public final void setData(int data) {
        this.data = data;
    }

    /**     * @return the next     */    public final Node getNext() {
        return next;
    }

    /**     * @param next     *            the next to set     */    public final void setNext(Node next) {
        this.next = next;
    }

    Node next;

}

© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.