Professional Documents
Culture Documents
Program:
import java.util.Arrays;
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}};
for(int[] i:ans)
System.out.println(Arrays.toString(i));
if(a[0].length!=b.length)
return null;
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:
Program:
import java.util.List;
int[] a={0,1,2,3,4,5,6};
System.out.println(LinearSearch(a,5));
System.out.println(LinearSearch(a,9));
for(int i=0;i<a.length;i++)
if(a[i]==target)
return true;
}
return false;
Output:
Program:
int[] a={0,1,2,3,4,5,6};
System.out.println(BinarySearch(a,4));
System.out.println(BinarySearch(a,9));
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
a[++i]=n;
if(i==a.length-1)
for(int j=0;j<a.length;j++)
aa[j]=a[j];
a=aa;
System.out.println(a[i]);
a[i]=0;
i--;
for(int k=0;k<i;k++)
System.out.println(a[k]);
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;
Program:
Class Solution{
private Node tail; // By taking tail we can insert element at last in constant time;
this.size=0;
}
private class Node{
this.value=value;
this.value=value;
this.next=next;
node.next=head;
head=node;
if(tail==null)
tail=head;
size++;
{
insert(val);
return;
tail.next=node;
tail=node;
size++;
if(size==0)
insert(val);
return;
if(indx==0) {
insert(val);
return;
if(indx==size) {
inserLast(val);
return;
Node temp=head;
temp=temp.next;
}
temp.next=node;
size++;
LL list=new LL();
list.inserAt(0,5);
list.inserAt(1,2);
list.inserAt(0,0);
list.Display();
Program:
Class Solution{
private Node tail; // By taking tail we can insert element at last in constant time;
this.size=0;
}
private class Node{
this.value=value;
this.value=value;
this.next=next;
int val=head.value;
head=head.next;
if(head==null)
tail=null;
size--;
System.out.println(val);
return val;
int val=tail.value;
if(size==1) {
DeletFirst();
return val;
}
Node temp=head;
temp=temp.next;
tail=temp;
tail.next=null;
size--;
System.out.println(val);
return val;
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;
list.inserAt(0,5);
list.inserAt(1,2);
list.inserAt(0,0);
list.Display();
list.DeletFirst();
list.Display();
list.DeletLast();
list.Display();
Program:
Class Solution{
private Node tail; // By taking tail we can insert element at last in constant time;
this.size=0;
}
this.value=value;
this.value=value;
this.next=next;
Node temp;
temp=reverse;
while(temp!=null)
System.out.print(temp.value+"->");
temp=temp.next;
System.out.println("END");
LL list=new LL();
list.inserAt(0,5);
list.inserAt(1,2);
list.inserAt(0,0);
list.Display();
Program:
Class Solution{
private Node tail; // By taking tail we can insert element at last in constant time;
this.size=0;
{
this.value=value;
this.value=value;
this.next=next;
if(head==null)
return false;
Node temp=head;
while(temp!=null){
if(temp.value==val)
return true;
temp=temp.next;
return false;
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));
}
Class Solution{
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{
DLL (){
this.size=0;
class Node{
int val;
Node prev;
Node next;
Node(int val){
this.val=val;
size++;
node.next=head;
node.prev=null;
head.prev=node;}
head=node;
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;
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.next=temp.next;
temp.next.prev=node;
temp.next=node;
node.prev=temp;
list.insertFirst(0);
list.insertFirst(1);
list.insertLast(2);
list.insertLast(3);
list.insertAt(1,6);
list.insertAt(4,10);
list.Display();
Class Solution{
DLL (){
this.size=0;
class Node{
int val;
Node prev;
Node next;
Node(int val){
this.val=val;
if(size==0)
return;
head=head.next;
if(size==0)
return;
if(size==1)
DeleteFirst();
return;
}
Node temp=head;
while(temp.next!=null){
temp=temp.next;
temp.prev.next=null;
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) {
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{
DLL (){
this.size=0;
class Node{
int val;
Node prev;
Node next;
Node(int val){
this.val=val;
Node temp=head;
while(temp!=null){
System.out.print(temp.val+"->");
temp=temp.next;
System.out.println("END");
list.insertFirst(0);
list.insertFirst(1);
list.insertLast(2);
list.insertLast(3);
list.insertAt(1,6);
list.insertAt(4,10);
list.Display();
Output:
Program:
Class Solution{
DLL (){
this.size=0;
class Node{
int val;
Node prev;
Node next;
Node(int val){
this.val=val;
}
Node temp=head;
while(temp!=null){
if(temp.val==val)
return true;
temp=temp.next;
return false;
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:
this.head = null;
this.tail = null;
public CLL() {
if(head==null){
head=node;
tail=node;
return;
tail.next=node;
node.next=head;
tail=node;
Node temp=head;
while(temp.next.val!=val){
temp=temp.next;
temp.next=temp.next.next;
Node temp=head;
do
System.out.print(temp.val+"->");
temp=temp.next;
}while(temp!=head);
Node temp=head;
do
if(temp.val==val)
return true;
temp=temp.next;
}while(temp!=head);
return false;
int val;
Node next;
this.val=val;
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:
Program:
public CircularQueue()
this.data=new int[size];
return size==data.length-1;
}
return size==0;
if(isFull())
return false;
data[end++]=item;
end=end% data.length;
size++;
return true;
if(isEmpty()){
int item=data[front++];
front=front% data.length;
size--;
return item;
if(isEmpty()){
return data[front];
}
void Display(){
for(int i=front;i<end;i++){
System.out.print(data[i]+" ");
System.out.println("END");
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;
int size=0;
Node head;
class Node {
int val;
Node next;
Node(int val) {
this.val = val;
this.val = val;
this.next = next;
node.next=head;
head=node;
size++;
// if stack is Empty
if(isEmpty())
return -1;
if(head.next==null){
head=head.next;
size--;
return val;
Node temp=head;
while(temp.next.next!=null)
temp=temp.next;
temp.next=temp.next.next;
return val;
if(size==0)
return true;
else
return false;
System.out.print(temp.val+"->");
temp=temp.next;
System.out.println("END");
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;
int size=0;
Node head;
class Node {
int val;
Node next;
Node(int val) {
this.val = val;
this.val = val;
this.next = next;
}
public void push(int val){
node.next=head;
head=node;
size++;
if(isEmpty())
return -1;
int val=head.val;
head=head.next;
size--;
return val;
if(size==0)
return true;
else
return false;
System.out.print(temp.val+"->");
temp=temp.next;
System.out.println("END");
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:
package com.o11Stack;
import java.util.LinkedList;
import java.util.Queue;
int size=0;
public ImplementStackUsingQueue(){
queue=new LinkedList();
if(queue.isEmpty()){
queue.add(x);
size++;
return;
queue.add(x);
size++;
int fix=size;
while(size>1){
queue.add(remove);
size--;
}
size=fix;
int remove=queue.remove();
return remove;
int top=queue.peek();
return top;
if(queue.isEmpty())
return true;
return false;
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:
Program:
package com.o12Queue;
import java.util.Stack;
Stack<Integer> stack;
Stack<Integer> stack2;
int size=0;
public ImplementQueueUsingStack(){
stack=new Stack<>();
stack2=new Stack<>();
if(stack.isEmpty()) {
stack.push(x);
size++;
return;
while(!stack.isEmpty()){
stack2.push(stack.pop());
stack.push(x);
size++;
while(!stack2.isEmpty()){
stack.push(stack2.pop());
int remove=stack.pop();
return remove;
int top=stack.peek();
return top;
return stack.isEmpty();
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 {
min = key[v];
min_index = v;
return min_index;
System.out.println("Edge \tWeight");
+ graph[i][parent[i]]);
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
parent[v] = u;
key[v] = graph[u][v];
printMST(parent, graph);
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
t.primMST(graph);
Output:
Program:
package com.o15Graph;
import java.io.*;
import java.lang.*;
import java.util.*;
class Graph {
{
return this.weight - compareEdge.weight;
class subset {
int V, E;
Edge edge[];
Graph(int v, int e)
V = v;
E = e;
if (subsets[i].parent != i)
subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
subsets[xroot].parent = yroot;
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
void KruskalMST()
int e = 0;
int i = 0;
Arrays.sort(edge);
subsets[v].parent = v;
subsets[v].rank = 0;
}
i = 0;
while (e < V - 1) {
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
int minimumCost = 0;
+ result[i].dest
minimumCost += result[i].weight;
+ minimumCost);
// Driver's Code
{
int V = 4;
int E = 5;
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = 10;
graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 6;
graph.edge[2].src = 0;
graph.edge[2].dest = 3;
graph.edge[2].weight = 5;
graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 15;
graph.edge[4].src = 2;
graph.edge[4].dest = 3;
graph.edge[4].weight = 4;
// Function call
graph.KruskalMST();
Output:
Program:
package com.O4SORTING;
import java.util.Arrays;
int[] b={5,4,3,2,1};
System.out.println(Arrays.toString(b));
bubble(b);
System.out.println(Arrays.toString(b));
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;
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));
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
while(start<=end)
while(a[start]<pivot){
start++;}
while(a[end]>pivot){
end--;}
if(start<=end)
int temp=a[start];
a[start]=a[end];
a[end]=temp;
start++;
end--;
QuickSort(a,lo, end);
QuickSort(a,start,hi);
Output: