Implementation single linked list.

 package LinkedList;

class Sll{
    Node head;
    Node tail;
    int size;
    void length(){
        System.out.println("length is = "+size);
    }
    void insertAtStart(int val){
    Node temp = new Node(val);
    if(head==null){
        head=tail=temp;
        size++;
        return;
    }
    temp.next=head;
    head=temp;
    size++;
    }
    void insertAtEnd(int val){
    Node temp = new Node(val);
    if(tail==null){
        head=tail=temp;
        size++;
        return;
    }
    tail.next=temp;
    tail=temp;
    size++;
    }
    void insertAtIndex(int ix , int val){
        if(ix<0 || ix>size){
            throw new IndexOutOfBoundsException("invalid index");
        }
        if(ix==0){
            insertAtStart(val);
            return;
        }
        if(ix==size){
            insertAtEnd(val);
            return;
        }
        Node temp = new Node(val);
        Node x=head;
        for(int i=0; i<ix-1; i++){
        x=x.next;
        }
        temp.next=x.next;
        x.next=temp;
        size++;
    }
    void print(){
        if(head==null){
            throw new RuntimeException("linked list is empty");
        }
        Node temp=head;
        while(temp!=null){
        System.out.print(temp.val+" ");
        temp=temp.next;
        }
        System.out.println();
    }
    void deleteAtStart(){
        if(head==null){
            throw new RuntimeException("linked list is empty");
        }
        if(head.next==null){
        head=tail=null;
        size--;
        return;
    }
        head=head.next;
        size--;
    }
    void deleteAtEnd(){
    if(tail==null){
       throw new RuntimeException("linked list is empty");
    }
    if(head.next==null){
        head=tail=null;
        size--;
        return;
    }
    Node x=head;
    while(x.next!=tail){
    x=x.next;  
    }
    x.next=null;
    tail=x;
    size--;
    }
    void deleteAtIndex(int ix){
    if(ix<0 || ix>=size){
        throw new IndexOutOfBoundsException("invalid index");
    }
    if(ix==0){
        deleteAtStart();
        return;
    }
    if(ix==size-1){
        deleteAtEnd();
        return;
    }
    Node x = head;
    for(int i=0; i<ix-1; i++){
        x=x.next;
    }
    x.next=x.next.next;
    size--;
    }
    int get(int ix){
    if(ix<0 || ix>=size){
        throw new IndexOutOfBoundsException("invalid index");
    }
    if(ix==0){
        return head.val;
    }
    if(ix==size-1){
        return tail.val;
    }
    Node x=head;
    for(int i=0; i<ix; i++){
        x=x.next;
    }
    return x.val;
    }
    void set(int ix , int val){
    if(ix<0 || ix>=size){
        throw new IndexOutOfBoundsException("invalid index");
    }
    if(ix==0){
        head.val=val;
        return;
    }
    if(ix==size-1){
        tail.val=val;
        return;
    }
    Node x=head;
    for(int i=0; i<ix; i++){
        x=x.next;
    }
    x.val=val;
    }
}
public class ImplementationSingleLinkedList {
public static void main(String[] args){
Sll l =  new Sll();
l.insertAtEnd(90);
l.insertAtEnd(80);
l.print();
l.insertAtStart(10);
l.print();
l.length();
l.insertAtIndex(1, 50);
l.print();
l.length();
l.deleteAtEnd();
l.print();
System.out.println(l.tail.val);
l.length();
System.out.println(l.get(2));
l.insertAtEnd(100);
l.insertAtStart(5);
l.print();
l.length();
l.deleteAtStart();
l.print();
l.length();
System.out.println(l.head.val);
l.deleteAtEnd();
l.print();
l.length();
l.deleteAtIndex(1);
l.print();
l.length();
l.deleteAtEnd();
l.deleteAtEnd();
l.deleteAtEnd();
}
}

listNodeclass-


package LinkedList;
class Node{
    int val;
    Node next;
    Node(int val){
    this.val=val;
    }
}
public class ListNodeClass{
    public static void disp_rev_recursion(Node head){
    if(head==null)
    return;
    disp_rev_recursion(head.next);
    System.out.print(head.val+" ");
    }
    public static void disp_recursion(Node head){
    if(head==null)
    return;
    System.out.print(head.val+" ");
    disp_recursion(head.next);
    }
    public static void print(Node head){
    Node temp=head;
    while(temp!=null){
        System.out.print(temp.val+" ");
        temp=temp.next;
    }
    System.out.println();
    }
   public static void main(String[] args) {
    Node a = new Node(10);
    Node b = new Node(20);
    Node c = new Node(30);
    Node d = new Node(40);
    Node e = new Node(50);
    a.next=b;
    b.next=c;
    c.next=d;
    d.next=e;
    // System.out.println(a.val);
    // System.out.println(b.val);
    // System.out.println(c.val);
    // System.out.println(d.val);
    // System.out.println(e.val);
    // Node temp=a;
    // for(int i=0; i<5; i++){
    // System.out.print(temp.val+" ");
    // temp=temp.next;
    //print(a);
    //disp_recursion(a);
    disp_rev_recursion(a);
    }
}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.