Skip to main content

Linkedlist Insertion and Deletion Details



class Node {
    Node next;
    int num;
    public Node(int val) {
        num = val;
        next = null;
    }
}

class LinkedlistImp {

    private Node head = null;

    public void append(int val) {
        Node lastNode = getLastNode();
        if (lastNode == null) {
            head = new Node(val);
        } else {
            lastNode.next = new Node(val);
        }
    }

    public void delete(int val) {
        if(head == null){
            return;
        }

        Node prevNode = null;
        Node currNode = head;
        while (currNode != null && currNode.num != val) {
            prevNode = currNode;
            currNode = currNode.next;
        }
        if(prevNode == null){
            head = head.next;
            return;
        }
        if (currNode == null) {
            System.out.println("A node with that value does not exist.");
            return;
        }
        prevNode.next = currNode.next;
    }

    public void print() {
        System.out.println("");
        if(head == null){
            System.out.print("EMPTY");
            return;
        }
        Node tmpNode = head;
        while (tmpNode != null) {
            System.out.print(tmpNode.num + " -> ");
            tmpNode = tmpNode.next;
        }
    }

    private Node getLastNode() {
        if (head == null) {
            return null;
        }
        Node tmpNode = head;
        while (tmpNode.next != null) {
            tmpNode = tmpNode.next;
        }
        return tmpNode;
    }

    public static void main(String[] args) {
        LinkedlistImp myList = new LinkedlistImp();
        myList.print();
        myList.append(35);
        myList.append(33);
        myList.print();
        myList.delete(33);
        myList.delete(35);
        myList.delete(35);
        myList.print();
    }


}

Comments

Popular posts from this blog

Find 2nd Largest Number in java ?

import java.util.Scanner; public class SocondLargest {     public static void main(String[] args) {         int secondlargest = Integer.MIN_VALUE;         int largest = Integer.MIN_VALUE;         System.out.println(largest);         Scanner input = new Scanner(System.in);         System.out.println("Enter array values: ");         int arr[] = new int[5];         for (int i = 0; i < arr.length; i++) {             arr[i] = input.nextInt();             if (largest < arr[i]) {                 secondlargest = largest;                 largest = arr[i];  ...

Check the number is Armstrong or not ??

import java.util.Scanner; public class Armstrong { public static void main(String[] args) { System.out.println("Enter the number ..."); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int temp=n; int sum=0 ; while(n>0){  int d=n%10;  sum=sum+d*d*d;  n=n/10; } if(temp==sum){ System.out.println("armstrong"); } else{ System.out.println("not armstrong"); }       sc.close(); } }

Custom exceptional Handling in java

class InvalidAgeException extends Exception { private static final long serialVersionUID = 1L; InvalidAgeException(String s) { super(s); } } public class Custom { public static void age(int age) throws InvalidAgeException { if (age < 18) throw new InvalidAgeException("not valid"); else System.out.println("welcom to vote"); } public static void main(String[] args) { try { age(19); } catch (Exception ex) { System.out.println("exception occured " + ex); } } }