You are on page 1of 54

Q1) WAP to Multiply 2 Matrix.

Program:

import java.util.Arrays;

public class raj01 {

public static void main(String[] args) {

int[][] a={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};

int[][] b={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};

int[][] ans= Multiplication(a,b);

for(int[] i:ans)

System.out.println(Arrays.toString(i));

static int[][] Multiplication(int[][] a,int[][] b)

if(a[0].length!=b.length)

return null;

int[][] ans=new int[a.length][b[0].length];

for(int i=0;i<a.length;i++)

for(int j=0;j<a[0].length;j++)

{ ans[i][j]=0;

for(int k=0;k<b.length;k++) {

ans[i][j] +=a[i][k]*b[k][j];

}
}

return ans;

Output:

Q2) WAP for Linear Search.

Program:

import java.util.List;

public class raj02 {

public static void main(String[] args) {

int[] a={0,1,2,3,4,5,6};

System.out.println(LinearSearch(a,5));

System.out.println(LinearSearch(a,9));

static boolean LinearSearch(int[] a,int target)

for(int i=0;i<a.length;i++)

if(a[i]==target)

return true;
}

return false;

Output:

Q3) WAP for BInarySearch.

Program:

public class raj03 {

public static void main(String[] args) {

int[] a={0,1,2,3,4,5,6};

System.out.println(BinarySearch(a,4));

System.out.println(BinarySearch(a,9));

static int BinarySearch(int[] a,int target)

int start=0;

int end=a.length-1;

while(start<=end)

int mid=start+(end-start)/2;

if(a[mid]==target)
return mid;

if(a[mid]>target)

end=mid-1;

else

start=mid+1;

return -1;

Output:
Q4) WAP to implement Stack using array.

Program:

class Stack

private static int[] a=new int[10];

private static int i=-1;

static void push(int n)

a[++i]=n;

if(i==a.length-1)

int[] aa=new int[2*a.length];

for(int j=0;j<a.length;j++)

aa[j]=a[j];

a=aa;

static void pop()

System.out.println(a[i]);

a[i]=0;

i--;

static void Display()


{

for(int k=0;k<i;k++)

System.out.println(a[k]);

public class StackUsingArray {

public static void main(String[] args) {

Stack s=new Stack();

s.push(1);

s.push(1);

s.pop();

s.push(1);

s.push(1);

s.push(1);

s.pop();

s.push(1);

s.Display();

}
Q5) WAP to create a Singly LinkedList.

Program:

Class LinkedList{

Class Node{

Int val;

Node next;

Node(int val){

this.val=val;

public static void main(String[] args) {

LinkedList list=new LinkedList();

// now a singly Linked List is created

Q6)WAP to insert an element in a singly LinkedList at given position.

Program:

Class Solution{

private Node head;

private Node tail; // By taking tail we can insert element at last in constant time;

private int size;

public LL(){ // When we initiliazed a linked list it will create a empty LL

this.size=0;

}
private class Node{

private int value;

private Node next;

public Node(int value)

this.value=value;

public Node(int value,Node next){

this.value=value;

this.next=next;

//This will insert element at start

public void insert(int val){

Node node=new Node(val);

node.next=head;

head=node;

if(tail==null)

tail=head;

size++;

//This will insert element at end

public void inserLast(int val)

if(tail==null) // what if the user use it fo the first element

{
insert(val);

return;

Node node =new Node(val);

tail.next=node;

tail=node;

size++;

//This will insert element at given position

public void inserAt(int indx,int val){

if(size==0)

insert(val);

return;

if(indx==0) {

insert(val);

return;

if(indx==size) {

inserLast(val);

return;

Node temp=head;

for(int i=1;i<indx;i++){ // we cant start from 0 as temp is already at head or 0

temp=temp.next;
}

Node node =new Node(val,temp.next);

temp.next=node;

size++;

public static void main(String[] args) {

LL list=new LL();

list.inserAt(0,5);

list.inserAt(1,2);

list.inserAt(0,0);

list.Display();

Q7) WAP to delete an element from a singly LinkedList.

Program:

Class Solution{

private Node head;

private Node tail; // By taking tail we can insert element at last in constant time;

private int size;

public LL(){ // When we initiliazed a linked list it will create a empty LL

this.size=0;

}
private class Node{

private int value;

private Node next;

public Node(int value)

this.value=value;

public Node(int value,Node next){

this.value=value;

this.next=next;

public int DeletFirst(){

int val=head.value;

head=head.next;

if(head==null)

tail=null;

size--;

System.out.println(val);

return val;

public int DeletLast(){

int val=tail.value;

if(size==1) {

DeletFirst();

return val;
}

Node temp=head;

for(int i=1;i<size-1;i++){ // as my temp is already at head

temp=temp.next;

tail=temp;

tail.next=null;

size--;

System.out.println(val);

return val;

public int DeletAt(int indx)

if( indx==0){

return DeletFirst();

if(indx==(size-1)){

return DeletLast();}

Node temp=head;

for(int i=1;i<indx;i++){

temp=temp.next;

temp.next=(temp.next).next;

return (temp.next).value;

public static void main(String[] args) {


LL list=new LL();

list.inserAt(0,5);

list.inserAt(1,2);

list.inserAt(0,0);

list.Display();

list.DeletFirst();

list.Display();

list.DeletLast();

list.Display();

Q8) A) WAP to traverse a Singly LinkedList;

Program:

Class Solution{

private Node head;

private Node tail; // By taking tail we can insert element at last in constant time;

private int size;

public LL(){ // When we initiliazed a linked list it will create a empty LL

this.size=0;
}

private class Node{

private int value;

private Node next;

public Node(int value)

this.value=value;

public Node(int value,Node next){

this.value=value;

this.next=next;

public void Display(Node reverse)

Node temp;

temp=reverse;

while(temp!=null)

System.out.print(temp.value+"->");

temp=temp.next;

System.out.println("END");

public static void main(String[] args) {

LL list=new LL();
list.inserAt(0,5);

list.inserAt(1,2);

list.inserAt(0,0);

list.Display();

Q8)B) WAP to Search an element in a Singly LInkedList.

Program:

Class Solution{

private Node head;

private Node tail; // By taking tail we can insert element at last in constant time;

private int size;

public LL(){ // When we initiliazed a linked list it will create a empty LL

this.size=0;

private class Node{

private int value;

private Node next;

public Node(int value)

{
this.value=value;

public Node(int value,Node next){

this.value=value;

this.next=next;

public boolean Search(int val){

if(head==null)

return false;

Node temp=head;

while(temp!=null){

if(temp.value==val)

return true;

temp=temp.next;

return false;

public static void main(String[] args) {

LL list=new LL();

list.inserAt(0,5);

list.inserAt(1,2);

list.inserAt(0,0);

list.Display();

System.out.println(list.Search(0));

System.out.println(list.Search(5));
}

Q9) WAP to create an Doubly Linked List.

Class Solution{

private node head;

class Node{

int val;

Node prev;

Node next;

Node(int val){

this.val=val;

}
Q10) WAP to insert an element in a Doubly LinkedList at a given position.

Program:

Class Solution{

private node head;

DLL (){

this.size=0;

class Node{

int val;

Node prev;

Node next;

Node(int val){

this.val=val;

public void insertFirst(int val){

size++;

Node node =new Node(val);

node.next=head;

node.prev=null;

if(head!=null){ // we need this check as first time head has no previous

head.prev=node;}

head=node;

public void insertLast(int val){

size++;
Node node =new Node(val);

Node tail=head;

if(head==null)

node.prev=null;

head=node;

return ;

while(tail.next!=null){

tail=tail.next;

tail.next=node;

node.prev=tail;

node.next=null;

public void insertAt(int indx,int val){

if(indx==0) {

insertFirst(val);

return;

if(indx==size-1){

insertLast(val);

return;

Node temp=head;

for(int i=1;i<indx;i++){
temp=temp.next;

Node node=new Node(val);

node.next=temp.next;

temp.next.prev=node;

temp.next=node;

node.prev=temp;

public static void main(String[] args) {

DLL list=new DLL();

list.insertFirst(0);

list.insertFirst(1);

list.insertLast(2);

list.insertLast(3);

list.insertAt(1,6);

list.insertAt(4,10);

list.Display();

Q11) WAP to delete an element from a Doubly Linked List.


Program:

Class Solution{

private node head;

DLL (){

this.size=0;

class Node{

int val;

Node prev;

Node next;

Node(int val){

this.val=val;

public void DeleteFirst(){

if(size==0)

return;

head=head.next;

public void DeleteLast(){

if(size==0)

return;

if(size==1)

DeleteFirst();

return;
}

Node temp=head;

while(temp.next!=null){

temp=temp.next;

temp.prev.next=null;

public void DeleteAt(int indx){

if(size==0)

return;

if(indx==1)

DeleteFirst();

return;

if(indx==size-1){

DeleteLast();

return;

Node temp=head;

for(int i=1;i<indx;i++){

temp=temp.next;

temp.next=temp.next.next;

temp.next.prev=temp;

}
public static void main(String[] args) {

DLL list=new DLL();

list.insertFirst(0);

list.insertFirst(1);

list.insertLast(2);

list.insertLast(3);

list.insertAt(1,6);

list.insertAt(4,10);

list.Display();

list.DeleteLast();

list.Display();

list.DeleteFirst();

list.Display();

list.DeleteAt(2);

list.Display();

Output:
Q12)A) WAP to traverse a Doubly Linked List.

Program:

Class Solution{

private node head;

DLL (){

this.size=0;

class Node{

int val;

Node prev;

Node next;

Node(int val){

this.val=val;

public void Display(){

Node temp=head;

while(temp!=null){

System.out.print(temp.val+"->");

temp=temp.next;

System.out.println("END");

public static void main(String[] args) {

DLL list=new DLL();

list.insertFirst(0);
list.insertFirst(1);

list.insertLast(2);

list.insertLast(3);

list.insertAt(1,6);

list.insertAt(4,10);

list.Display();

Output:

Q12) B) WAP to search an element in a Doubly LinkedList.

Program:

Class Solution{

private node head;

DLL (){

this.size=0;

class Node{

int val;

Node prev;

Node next;

Node(int val){

this.val=val;
}

public boolean Search(int val){

Node temp=head;

while(temp!=null){

if(temp.val==val)

return true;

temp=temp.next;

return false;

public static void main(String[] args) {

DLL list=new DLL();

list.insertFirst(0);

list.insertFirst(1);

list.insertLast(2);

list.insertLast(3);

list.Display();

System.out.println(list.Search(3));

System.out.println(list.Search(12));

Output:
Q13) WAP to create a circular LinkedList and perform insert ,delete and Search.

Program:

public class CLL {

private Node head;

private Node tail;

public CLL(Node head, Node tail) {

this.head = null;

this.tail = null;

public CLL() {

public void insert(int val)

Node node=new Node(val);

if(head==null){

head=node;

tail=node;

return;

tail.next=node;
node.next=head;

tail=node;

public void delet(int val)

Node temp=head;

while(temp.next.val!=val){

temp=temp.next;

temp.next=temp.next.next;

public void display()

Node temp=head;

do

System.out.print(temp.val+"->");

temp=temp.next;

}while(temp!=head);

public boolean Search(int val)

Node temp=head;

do

if(temp.val==val)
return true;

temp=temp.next;

}while(temp!=head);

return false;

private class Node{

int val;

Node next;

Node (int val){

this.val=val;

public static void main(String[] args) {

CLL list=new CLL();

list.insert(1);

list.insert(2);

list.insert(3);

list.insert(4);

list.display();

list.delet(4);

System.out.println();

list.display();

System.out.println();

System.out.println(list.Search(3));

System.out.println(list.Search(12));

}
}

Output:

Q14) WAP to implement queue using array.

Program:

public class CircularQueue {

private int[] data;

private static final int DEFAULT_SIZE=10;

protected int end=0;

protected int front=0;

private int size=0;

public CircularQueue()

this(DEFAULT_SIZE); // using this we are calling it as a constructor

public CircularQueue(int size)

this.data=new int[size];

public boolean isFull() {

return size==data.length-1;
}

public boolean isEmpty() {

return size==0;

public boolean insert(int item){

if(isFull())

return false;

data[end++]=item;

end=end% data.length;

size++;

return true;

public int remove() throws Exception {

if(isEmpty()){

throw new Exception("Queue is Empty");

int item=data[front++];

front=front% data.length;

size--;

return item;

public int front() throws Exception {

if(isEmpty()){

throw new Exception("Queue is empty");

return data[front];
}

void Display(){

for(int i=front;i<end;i++){

System.out.print(data[i]+" ");

System.out.println("END");

public static void main(String[] args) throws Exception {

CircularQueue q=new CircularQueue();

q.insert(1);

q.insert(2);

q.insert(3);

q.Display();

q.remove();

q.Display();

System.out.println(q.isEmpty());

System.out.println(q.isFull());

Output:
Q15) WAP to implement queue using linked list.

Program:

package com.o11Stack;

public class QueueUsingLinkedList {

int size=0;

Node head;

class Node {

int val;

Node next;

Node(int val) {

this.val = val;

Node(int val, Node next) {

this.val = val;

this.next = next;

public void push(int val){

Node node=new Node(val);

node.next=head;

head=node;

size++;

public int pull()

// if stack is Empty
if(isEmpty())

return -1;

// if only one element is there

if(head.next==null){

int val= head.val;

head=head.next;

size--;

return val;

Node temp=head;

while(temp.next.next!=null)

temp=temp.next;

int val= temp.next.val;

temp.next=temp.next.next;

return val;

public boolean isEmpty()

if(size==0)

return true;

else

return false;

public void Display() {


Node temp = head;

while (temp != null) {

System.out.print(temp.val+"->");

temp=temp.next;

System.out.println("END");

public static void main(String[] args) {

QueueUsingLinkedList q=new QueueUsingLinkedList();

q.push(1);

q.push(2);

q.push(3);

q.push(4);

q.push(5);

q.Display();

q.pull();

q.pull();

q.Display();

System.out.println(s.isEmpty());

Output:
Q16) WAP to implement Stack using linked list.

Program:

package com.o12Queue;

public class QueueUsingLinkedList {

int size=0;

Node head;

class Node {

int val;

Node next;

Node(int val) {

this.val = val;

Node(int val, Node next) {

this.val = val;

this.next = next;

}
public void push(int val){

Node node=new Node(val);

node.next=head;

head=node;

size++;

public int pull()

if(isEmpty())

return -1;

int val=head.val;

head=head.next;

size--;

return val;

public boolean isEmpty()

if(size==0)

return true;

else

return false;

public void Display() {

Node temp = head;

while (temp != null) {

System.out.print(temp.val+"->");
temp=temp.next;

System.out.println("END");

public static void main(String[] args) {

StackUsingLinkedList s=new StackUsingLinkedList();

s.push(1);

s.push(2);

s.push(3);

s.push(4);

s.push(5);

s.Display();

s.pull();

s.pull();

s.Display();

System.out.println(s.isEmpty());

Output:
Q17) WAP to implement stack using Queue.

Program:

// We can implement stack using 2 or even 1 queue

package com.o11Stack;

import java.util.LinkedList;

import java.util.Queue;

public class ImplementStackUsingQueue {

// using a single queue

private Queue<Integer> queue;

int size=0;

public ImplementStackUsingQueue(){

queue=new LinkedList();

public void push(int x){

if(queue.isEmpty()){

queue.add(x);

size++;

return;

queue.add(x);

size++;

int fix=size;

while(size>1){

int remove= queue.remove();

queue.add(remove);

size--;
}

size=fix;

public int pop(){

int remove=queue.remove();

return remove;

public int top(){

int top=queue.peek();

return top;

public boolean isEmpty(){

if(queue.isEmpty())

return true;

return false;

public static void main(String[] args) {

ImplementStackUsingQueue stack=new ImplementStackUsingQueue();

stack.push(3);

stack.push(4);

stack.push(2);

System.out.println(stack.top());

System.out.println(stack.pop());

System.out.println(stack.top());

System.out.println(stack.pop());

System.out.println(stack.top());
}

Output:

Q17)B) WAP to implement Queue using Stack.

Program:

package com.o12Queue;

import java.util.Stack;

public class ImplementQueueUsingStack {

Stack<Integer> stack;

Stack<Integer> stack2;

int size=0;

public ImplementQueueUsingStack(){

stack=new Stack<>();

stack2=new Stack<>();

public void push(int x){

if(stack.isEmpty()) {

stack.push(x);

size++;
return;

// shift all from stack1 to stack 2 first

while(!stack.isEmpty()){

stack2.push(stack.pop());

// push element in stack 1

stack.push(x);

size++;

while(!stack2.isEmpty()){

stack.push(stack2.pop());

public int pop(){

int remove=stack.pop();

return remove;

public int peek(){

int top=stack.peek();

return top;

public boolean isEmpty(){

return stack.isEmpty();

public static void main(String[] args) {

ImplementQueueUsingStack queue=new ImplementQueueUsingStack();


queue.push(1);

queue.push(2);

queue.push(3);

queue.push(4);

System.out.println(queue.pop());

queue.push(5);

System.out.println(queue.pop());

System.out.println(queue.pop());

System.out.println(queue.pop());

System.out.println(queue.pop());

Output:
Q18) WAP to implement Prim’s algorithm.

Program:

package com.o15Graph;

import java.io.*;

import java.lang.*;

import java.util.*;

class MST {

private static final int V = 5;

int minKey(int key[], Boolean mstSet[])

int min = Integer.MAX_VALUE, min_index = -1;

for (int v = 0; v < V; v++)

if (mstSet[v] == false && key[v] < min) {

min = key[v];

min_index = v;

return min_index;

void printMST(int parent[], int graph[][])

System.out.println("Edge \tWeight");

for (int i = 1; i < V; i++)

System.out.println(parent[i] + " - " + i + "\t"

+ graph[i][parent[i]]);

void primMST(int graph[][])


{

int parent[] = new int[V];

int key[] = new int[V];

Boolean mstSet[] = new Boolean[V];

for (int i = 0; i < V; i++) {

key[i] = Integer.MAX_VALUE;

mstSet[i] = false;

key[0] = 0;

parent[0] = -1;

for (int count = 0; count < V - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = true;

for (int v = 0; v < V; v++)

if (graph[u][v] != 0 && mstSet[v] == false

&& graph[u][v] < key[v]) {

parent[v] = u;

key[v] = graph[u][v];

// print the constructed MST

printMST(parent, graph);

public static void main(String[] args)

MST t = new MST();


int graph[][] = new int[][] { { 0, 2, 0, 6, 0 },

{ 2, 0, 3, 8, 5 },

{ 0, 3, 0, 0, 7 },

{ 6, 8, 0, 0, 9 },

{ 0, 5, 7, 9, 0 } };

t.primMST(graph);

Output:

Q19) WAP to implement Kruskal’s algorithm.

Program:

package com.o15Graph;

import java.io.*;

import java.lang.*;

import java.util.*;

class Graph {

class Edge implements Comparable<Edge> {

int src, dest, weight;

public int compareTo(Edge compareEdge)

{
return this.weight - compareEdge.weight;

class subset {

int parent, rank;

int V, E;

Edge edge[];

Graph(int v, int e)

V = v;

E = e;

edge = new Edge[E];

for (int i = 0; i < e; ++i)

edge[i] = new Edge();

int find(subset subsets[], int i)

if (subsets[i].parent != i)

subsets[i].parent

= find(subsets, subsets[i].parent);

return subsets[i].parent;

void Union(subset subsets[], int x, int y)

int xroot = find(subsets, x);


int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)

subsets[xroot].parent = yroot;

else if (subsets[xroot].rank > subsets[yroot].rank)

subsets[yroot].parent = xroot;

else {

subsets[yroot].parent = xroot;

subsets[xroot].rank++;

void KruskalMST()

Edge result[] = new Edge[V];

int e = 0;

int i = 0;

for (i = 0; i < V; ++i)

result[i] = new Edge();

Arrays.sort(edge);

subset subsets[] = new subset[V];

for (i = 0; i < V; ++i)

subsets[i] = new subset();

for (int v = 0; v < V; ++v) {

subsets[v].parent = v;

subsets[v].rank = 0;

}
i = 0;

while (e < V - 1) {

Edge next_edge = edge[i++];

int x = find(subsets, next_edge.src);

int y = find(subsets, next_edge.dest);

if (x != y) {

result[e++] = next_edge;

Union(subsets, x, y);

System.out.println("Following are the edges in "

+ "the constructed MST");

int minimumCost = 0;

for (i = 0; i < e; ++i) {

System.out.println(result[i].src + " -- "

+ result[i].dest

+ " == " + result[i].weight);

minimumCost += result[i].weight;

System.out.println("Minimum Cost Spanning Tree "

+ minimumCost);

// Driver's Code

public static void main(String[] args)

{
int V = 4;

int E = 5;

Graph graph = new Graph(V, E);

// add edge 0-1

graph.edge[0].src = 0;

graph.edge[0].dest = 1;

graph.edge[0].weight = 10;

// add edge 0-2

graph.edge[1].src = 0;

graph.edge[1].dest = 2;

graph.edge[1].weight = 6;

// add edge 0-3

graph.edge[2].src = 0;

graph.edge[2].dest = 3;

graph.edge[2].weight = 5;

// add edge 1-3

graph.edge[3].src = 1;

graph.edge[3].dest = 3;

graph.edge[3].weight = 15;

// add edge 2-3

graph.edge[4].src = 2;
graph.edge[4].dest = 3;

graph.edge[4].weight = 4;

// Function call

graph.KruskalMST();

Output:

Q20) WAP to implement Bubble sort.

Program:

package com.O4SORTING;

import java.util.Arrays;

public class BubbleSort {

public static void main(String[] args) {

int[] b={5,4,3,2,1};

System.out.println(Arrays.toString(b));

bubble(b);
System.out.println(Arrays.toString(b));

static void bubble(int[] a)

//run the step n-1 times

for(int i=0;i<a.length;i++)

//for each step max item will come at the last index

for(int j=1;j<a.length-i;j++)

if(a[j]<a[j-1])

//swap

int temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

Output:
Q21)WAP to implement Quick Sort.

Program:

import java.lang.reflect.Array;

import java.util.Arrays;

public class Sort {

//Quick Sort (Pivot point)

public static void main(String[] args) {

int[] a={5,4,3,2,1};

System.out.println(Arrays.toString(a));

QuickSort(a,0,a.length-1);

System.out.println(Arrays.toString(a));

static void QuickSort(int[] a,int lo ,int hi)

if(lo>=hi)

return ;

int start=lo;

int end=hi;

int mid=start+(end-start)/2; // Taking pivot as mid index as it will give me the best
time complexity

int pivot =a[mid];

while(start<=end)

// also a region why if it already sorted it will not swap

while(a[start]<pivot){
start++;}

while(a[end]>pivot){

end--;}

// this both will give me to values where the condition is vilating

if(start<=end)

int temp=a[start];

a[start]=a[end];

a[end]=temp;

start++;

end--;

// now my pivot is at correct index pleae sort 2 halfs now

QuickSort(a,lo, end);

QuickSort(a,start,hi);

Output:

You might also like