linkedlist

class SLL{
    private Node head;
    private Node tail;
    private int size;
    void deleteAtIndex(int ix){
    if(ix<0 || ix>=size){
    throw new IndexOutOfBoundsException("bhai wrong index mat dal");
    }
    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--;
    }
    void deleteAtEnd(){
        if(tail==null){
            System.out.println("list is already empty");
            return;
        }
        if(tail==head){
            tail=head=null;
            size--;
            return;
        }
        Node x=head;
        while(x.next!=tail){
            x=x.next;
        }
        x.next=null;
        tail=x;
        size--;
    }
    void deleteAtStart(){
        if(head==null){
            System.out.println("list is already empty");
            return;
        }
        if(head.next==null){
            head=tail=null;
            size--;
            return;
        }
        head=head.next;
        size--;
    }
    void setElement(int ix, int data){
        if(ix<0 || ix>=size)
            throw new IndexOutOfBoundsException("bhai wrong index mat dal");
        if(ix==0){
        head.data=data;
        return;
        }  
        if(ix==size-1){
        tail.data=data;
        return;
        }
        Node x=head;
        for(int i=0; i<ix; i++){
            x=x.next;
        }
        x.data=data;
    }
    int getElement(int ix){
        if(ix<0 || ix>=size)
            throw new IndexOutOfBoundsException("bhai wrong index mat dal");
        if(ix==0)
        return head.data;
        if(ix==size-1)
            return tail.data;
        Node x=head;
        for(int i=0; i<ix; i++){
            x=x.next;
        }
        return x.data;
    }
    void insertAtIndex(int ix , int data){
    if(ix<0 || ix>size){
    throw new IndexOutOfBoundsException("bhai wrong index mat dal");
    }
    if(ix==0){
        insertAtStart(data);
        return;
    }
    if(ix==size){
        insertAtEnd(data);
        return;
    }
    Node newNode = new Node(data);  
    Node x=head;
    for(int i=0; i<ix-1; i++){
        x=x.next;
    }
    newNode.next=x.next;
    x.next=newNode;
    size++;
    }
    void insertAtEnd(int data){
    Node newNode = new Node(data);  
    if(tail==null){
        tail=head=newNode;
        size++;
        return;
    }
    tail.next=newNode;
    tail=newNode;
    size++;
    }
    int size(){
    return size;
    }
    void insertAtStart(int data){
        Node newNode = new Node(data);
        if(head==null){
            head=tail=newNode;
            size++;
            return;
        }
        newNode.next=head;
        head=newNode;
        size++;
    }
    void print(){
        Node temp=head;
    if(temp==null){
        System.out.println("list is empty");
        return;
    }
    while(temp!=null){
        System.out.print(temp.data+" ");
        temp=temp.next;
    }
    System.out.println();
}
}
public class LinkedList {
public static void main(String[] args) {
SLL l = new SLL();
}
}
-------------------------------------------------------------------------------------
public class Node {
int data;
Node next;
Node(int data){
this.data=data;
}
}
------------------------------------------------------------------------------------
public class ImplementationLL {
    public static void printReverse(Node head){
        if(head==null)
            return;
        printReverse(head.next);
        System.out.print(head.data+" ");
    }
    public static void printRecursion(Node head){
        if(head==null)
            return;
        System.out.print(head.data+" ");
        printRecursion(head.next);
    }
    public static void print(Node head){
        while(head!=null){
        System.out.print(head.data+" ");
        head=head.next;
        }
    }

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);
Node f = new Node(60);
a.next=b; b.next=c;
c.next=e; e.next=f;

// System.out.print(a.data+" ");
// System.out.print(b.data+" ");
// System.out.print(c.data+" ");
// System.out.print(d.data+" ");
// System.out.print(e.data+" ");
// System.out.print(f.data+" ");

//print(a);
//printRecursion(a);
//printReverse(a);
}
}
-------------------------------------------------------------------------------------
class DLL{
    DNode head;
    DNode tail;
    int size;
}
public class DoubleLL {
public static void main(String[] args) {
   
}  
}
-------------------------------------------------------------------------------------
public class DNode{
    DNode prev;
    int val;
    DNode next;
    DNode(int val){
        this.val=val;
    }
}
-------------------------------------------------------------------------------------
public class DImplementation {
public static void display(DNode node){
DNode temp=node;
while(temp.prev!=null){
temp=temp.prev;
}
print(temp);
}
public static void PrintReverse(DNode tail){
    while(tail!=null){
        System.out.print(tail.val+" ");
        tail=tail.prev;
    }
    System.out.println();
}
public static void print(DNode head){
while(head!=null){
    System.out.print(head.val+" ");
    head=head.next;
}
System.out.println();
}
public static void main(String[] args) {
DNode a = new DNode(10);
DNode b = new DNode(20);
DNode c = new DNode(30);
DNode d = new DNode(40);
a.next=b; b.prev=a;
b.next=c; c.prev=b;
c.next=d; d.prev=c;
// print(a);
// PrintReverse(d);
display(c);
}  
--------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.