DS Partical

DS Partical

Suriya Ravichandran

Single Linked list:

class SLL{

static class Node{

int data;

Node next;

Node(int d){

    data=d;

}

}

Node head;

void add(int d){

Node n=new Node(d);

if (head==null)

head=n;

else{

Node t=head;

while (t.next!=null)

  t=t.next;

  t.next=n;

}

}


void show(){

for (Node t=head; t!=null;t=t.next)

System.out.print(t.data+"");

}


public static void main(String[] args){

SLL l = new SLL();

l.add(10);

l.add(20);

l.add(30);

l.show();

}

}


Double Linked List:

class DLL {

    // Node class

    static class Node {

        int data;

        Node prev, next;

        Node(int d) {

            data = d;

        }

    }

    // Head of the list

    Node head;

    // Add a new node to the end

    void add(int d) {

        Node n = new Node(d);

        if (head == null)

            head = n;

        else {

            Node t = head;

            while (t.next != null)

                t = t.next;

            t.next = n;

            n.prev = t;

        }

    }


    // Display list from start to end

    void show() {

        for (Node t = head; t != null; t = t.next)

            System.out.print(t.data + " ");

    }


    // Main method

    public static void main(String[] args) {

        DLL l = new DLL();

        l.add(10);

        l.add(20);

        l.add(30);

        l.show();

    }

}

Shell sort:

class Shell {
    public static void main(String[] args) {
        int arr[] = {12, 34, 54, 2, 3};
        int n = arr.length;

        // Shell Sort algorithm
        for (int g = n / 2; g > 0; g /= 2) {
            for (int i = g; i < n; i++) {
                int t = arr[i];
                int j = i;
                while (j >= g && arr[j - g] > t) {
                    arr[j] = arr[j - g];
                    j -= g;
                }
                arr[j] = t;
            }
        }

        // Print sorted array
        for (int x : arr) {
            System.out.print(x + " ");
        }
    }
}

Queue:


import java.util.*;

class Q{
public static void main(String[] args){
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(20);
q.add(30);
System.out.println(q);
q.remove();
System.out.println(q);
}

}

Stack:

import java.util.*;

class S{
public static void main(String[] args){

Stack <Integer> s = new Stack<>();
s.push(10);
s.push(20);
s.push(30);

System.out.println(s);
s.pop();
System.out.println(s);
}

}


Our website uses cookies to enhance your experience. Learn More
Accept !