ADS - Practical Material 2024
ADS - Practical Material 2024
Lab Manual
1332 CIS - 3
Algorithm and Data Structures
[PRACTICAL ACTIVITY BOOK]
Prepared by
Index Page
1 3
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 3
10 3
2
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
1. Write a java program that reads five integers and displays them
import javax.swing.*;
class array
{
public static void main(String args[])
{
int i;
int[] arr;
arr = new int[5];
for(i=0;i<5;i++)
arr[i]=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Array Elements: "));
}
}
Output:
3
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
import java.util.*;
public class vectors
{
public static void main(String args[]) {
System.out.println("Size: "+vec.size());
System.out.println("Capacity: "+vec.capacity());
Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}
Output:
4
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
3. Write a java program to implement the linked list using built-in java classes.
import java.util.*;
class list
{
public static void main(String[] args){
LinkedList link=new LinkedList();
link.add("a");
link.add("b");
link.add(10);
link.addFirst(20);
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());
link.addLast("c");
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());
link.add(2,"j");
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());
link.add(1,"t");
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());
link.remove(3);
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());
}
}
Output:
5
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
import java.util.*;
class stacks
{
public static void main(String args[])
{
Stack st=new Stack();
st.push("A");
st.push("B");
st.push("C");
st.push("D");
st.push("E");
st.push("F");
st.push("G");
st.push("H");
st.push("I");
st.push("J");
System.out.println(st);
System.out.println("st.size()=" +st.size());
System.out.println("st.peek()=" +st.peek());
System.out.println("st.search(B)="+st.search("B"));
System.out.println("st.pop()=" +st.pop());
System.out.println("st.pop()=" +st.pop());
System.out.println(st);
System.out.println("st.size()=" +st.size());
System.out.println("st.peek()=" +st.peek());
System.out.println("st.search(B)="+st.search("B"));
}
}
Output:
6
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
;*.import java.util
;qu.add(5)
;qu.add(6)
;qu.add(8)
;)(qu.poll
;)(qu.poll
;)(qu.poll
Output:
7
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
import java.util.*;
public class LinearSearch {
public int find(final int[] data, final int key) {
for (int i = 0; i < data.length; ++i) {
if (data[i] > key)
return -1;
else if (data[i] == key)
return i;
}
return -1;
}
public static void main(String[] args)
{
final int arr[] = new int[10];
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr.length; i++)
{
arr[i] = input.nextInt();
}
LinearSearch search = new LinearSearch();
System.out.print("Enter the element to search: ");
int num=input.nextInt();
int n = search.find(arr, num);
if ((n >= 0) && (n < arr.length))
{
System.out.println("Found at index: " + n);
}
else
{
System.out.println("Not Found");
}}}
Output:
8
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
Output:
9
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
Output:
10
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
Output:
11
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
12