DSA Lab Report - Hari Rijal
DSA Lab Report - Hari Rijal
class Passarray{
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}
public static void main(String args[]){
int a[]={10,50,18,22,28,6};
min(a);
}
}
2. Write a program to ask a number and power to be calculated for that number.
import java.util.Scanner;
public class PowerUsingRecursion {
public static int power(int base, int exp){
if (exp !=0){
return (base * power(base, exp-1));
}else {
return 1;
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base number :");
int base = sc.nextInt();
System.out.println("Enter the exponent number::");
int exp = sc.nextInt();
System.out.println(power(base, exp));
}
}
8. Write a program to multiply two matrices and get the transpose of the third one.
public class MatrixMultiplicationExample{
public static void main(String args[]){
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
Prepared By: Hari Rijal Study Notes Nepal www.studynotesnepal.com
}
System.out.println();
}
int transpose[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}
System.out.println("Printing Matrix After Transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
public static void main(String args[]){
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
int transpose[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}
System.out.println("Printing Matrix After Transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static String infixToPostfix(String exp)
{
String result = new String("");
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i)
{
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c))
result += c;
else if (c == '(')
stack.push(c);
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
}
Prepared By: Hari Rijal Study Notes Nepal www.studynotesnepal.com
// pop all the operators from the stack
while (!stack.isEmpty()){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
return result;
}
public static void main(String[] args)
{
String exp = "a+b*(c^d-e)^(f+g*h)-i";
System.out.println(infixToPostfix(exp));
}
}
11.Write a program to evaluate a prefix expression.
import java.io.*;
import java.util.*;
class GFG {
static Boolean isOperand(char c)
{
if (c >= 48 && c <= 57)
return true;
else
return false;
}
static double evaluatePrefix(String exprsn)
{
Stack<Double> Stack = new Stack<Double>();
class GFG {
else {
double o1 = Stack.peek();
Stack.pop();
double o2 = Stack.peek();
Stack.pop();
switch (exprsn.charAt(j)) {
case '+':
Stack.push(o1 + o2);
break;
case '-':
Stack.push(o1 - o2);
break;
case '*':
Stack.push(o1 * o2);
break;
case '/':
Stack.push(o1 / o2);
break;
}
}
}
return Stack.peek();
}
public static void main(String[] args)
12. Write a program to multiply two matrices and get the transpose of the third one
14. Write a menu driven program to implement linear queue using array.
17.Write a menu driven program to illustrate basic operations of singly linked list with
following operations:
//insertion
import java.util.Scanner;
class LinkedList{
Node head;
class Node {
int data;
Node next;
Node(int d) {data = d; next = null; }
}
public void push(int new_data) {
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void insertAfter(Node prev_node, int new_data) {
if (prev_node == null) {
System.out.println("The given previous node cannot be null");
return;
}
Node new_node = new Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
Prepared By: Hari Rijal Study Notes Nepal www.studynotesnepal.com
public void append(int new_data) {
Node new_node = new Node(new_data);
if (head == null){
head = new Node(new_data);
return;
}
new_node.next = null;
Node last = head;
while (last.next != null)
last = last.next;
last.next = new_node;
return;
}
public void printList() {
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
LinkedList llist = new LinkedList();
System.out.print("Enter the size of data you want to add at beginning:");
int n1=sc.nextInt();
for(int i=0;i<n1;i++){
llist.push(sc.nextInt());
}
llist.printList();
System.out.print("\nEnter the size of data you want to add at end:");
int n2=sc.nextInt();
for(int i=0;i<n2;i++){
llist.append(sc.nextInt());
}
llist.printList();
llist.insertAfter(llist.head.next, 8);
System.out.println("\nCreated Linked list is: ");
llist.printList();
}
}
//deletion
import java.util.Scanner;
public class SinglyLinkedListDeletion {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
22.Write a menu driven program to implement Binary search tree operation (insertion,
deletion and searching).
class BinarySearchTree {
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
Node root;
BinarySearchTree() {
root = null;
}
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
Prepared By: Hari Rijal Study Notes Nepal www.studynotesnepal.com
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
return root;
}
void inorder() {
inorderRec(root);
}
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.inorder();
}
}
class BinarySearchTree {
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
Node root;
BinarySearchTree() {
root = null;
}
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
System.out.println();
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Array Size : ");
int size_arr= scan.nextInt();
int arr[] = new int[size_arr];
System.out.print("Enter Array Elements : ");
for(int i=0; i<size_arr; i++){
arr[i] = scan.nextInt();
}
System.out.print("Array are :\n");
for (int i = 0; i < arr.length; ++i){
System.out.print(arr[i] + " ");
}
System.out.println();
InsertionSort ob_arr = new InsertionSort();
ob_arr.sort(arr);
printArray(arr);
System.out.println("MiLan,B,10");
}
}
import java.util.Scanner;
class InsertionSort {
void sort(int arr[]) {
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[]) {
int n = arr.length;
System.out.print("Now the Array after Sorting is :\n");
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");