Tuesday, January 26, 2016

Linked List Implementation

Linked List Impl in Java

public class Node {
            private int data;
            private Node nextNode;

            public Node(int data){
                        this.data=data;
            }
           
            public int getData() {
                        return data;
            }
           
            public void setData(int data) {
                        this.data = data;
            }
           
            public Node getNextNode() {
                        return nextNode;
            }
           
            public void setNextNode(Node nextNode) {
                        this.nextNode = nextNode;
            }
           
            @Override
            public String toString(){
                        return ""+this.data;
            }
}
           

public class LinkedList {

            public Node head;

            public Node reverseLinkedList(Node currentNode) {
                        // For first node, previousNode will be null
                        Node previousNode = null;
                        Node nextNode;
                        while (currentNode != null) {
                                    nextNode = currentNode.getNextNode();
                                    // reversing the link
                                    currentNode.setNextNode(previousNode);
                                    // moving currentNode and previousNode by 1 node
                                    previousNode = currentNode;
                                    currentNode = nextNode;
                        }
                        return previousNode;
            }

            public void add(int data) {
                        if (head == null)
                                    insertAtHead(data);
                        else
                                    insertAtEnd(data);
            }

            public void insertAtHead(int data) {
                        Node newNode = new Node(data);
                        newNode.setNextNode(this.head);
                        this.head = newNode;
            }

            public int length() {
                        Node current = this.head;
                        int count = 0;
                        while (current != null) {
                                    current = current.getNextNode();
                                    count++;
                        }
                        return count;
            }

            public void deleteAtHead() {
                        this.head = this.head.getNextNode();
            }

            public String find(int data) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getData() == data)
                                                return data + " Found";
                                    current = current.getNextNode();
                        }
                        return data + " Not Found ";
            }

            public void insertAfterData(int dataPresent, int dataAdd) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getData() == dataPresent) {
                                                Node newNode = new Node(dataAdd);
                                                newNode.setNextNode(current.getNextNode());
                                                current.setNextNode(newNode);
                                                break;
                                    }
                                    current = current.getNextNode();
                        }
            }

            public void insertBeforeData(int dataPresent, int dataAdd) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getNextNode().getData() == dataPresent) {
                                                Node newNode = new Node(dataAdd);
                                                newNode.setNextNode(current.getNextNode());
                                                current.setNextNode(newNode);
                                                break;
                                    }
                                    current = current.getNextNode();
                        }
            }

            public void insertAtEnd(int dataAdd) {
                        Node current = this.head;
                        while (current.getNextNode() != null) {
                                    current = current.getNextNode();
                        }
                        Node newNode = new Node(dataAdd);
                        newNode.setNextNode(current.getNextNode());
                        current.setNextNode(newNode);
            }

            public void deleteNode(int dataPresent) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getNextNode().getData() == dataPresent) {
                                                current.setNextNode(current.getNextNode().getNextNode());
                                                break;
                                    }
                                    current = current.getNextNode();
                        }
            }

            @Override
            public String toString() {
                        String result = "{";
                        Node current = this.head;
                        while (current != null) {
                                    result += current.toString() + "-->";
                                    current = current.getNextNode();
                        }
                        result += "null }";
                        return result;
            }
}

public class LinkedNodeDemo {

            public static void main(String[] args) {
                        LinkedList list = new LinkedList();
                        list.insertAtHead(10);
                        list.insertAtHead(20);
                        list.insertAtHead(15);
                        list.insertAtHead(25);
                        list.insertAtHead(40);
                        System.out.println("List after insertAtHead Operation" + list);
                        System.out.println("Length is " + list.length());
                        list.deleteAtHead();
                        System.out.println("List after deleteAtHead Operation" + list);
                        System.out.println(list.find(100));
                        list.insertAfterData(20, 12);
                        System.out.println("List after insertAfterData Operation " + list);
                        list.insertBeforeData(20, 6);
                        System.out.println("List after insertBeforeData Operation" + list);
                        list.insertAtEnd(19);
                        System.out.println("List after insertAtEnd Operation" + list);
                        list.deleteNode(20);
                        System.out.println("List after deleteNode Operation" + list);
                       
                        LinkedList list1 = new LinkedList();
                        list1.add(10);
                        list1.add(20);
                        list1.add(20);
                        System.out.println(list1);
            }
}

Output :
List after insertAtHead Operation{40-->25-->15-->20-->10-->null }
Length is 5
List after deleteAtHead Operation{25-->15-->20-->10-->null }
100 Not Found
List after insertAfterData Operation {25-->15-->20-->12-->10-->null }
List after insertBeforeData Operation{25-->15-->6-->20-->12-->10-->null }
List after insertAtEnd Operation{25-->15-->6-->20-->12-->10-->19-->null }
List after deleteNode Operation{25-->15-->6-->12-->10-->19-->null }
New List is(Like Build-In) {10-->20-->20-->null }

Pictorial representation of LinkedList




No comments:

Post a Comment