Java454 PDF
Java454 PDF
LAB MANUAL
R20
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
1. Write Java programs that use both recursive and non-recursive functions for implementing the
following searching methods:
(a) Linear search
(b) Binary search
Linear search
(a) : Iterative Linear search
class LinearSearchDemo {
static Object[] a = { 89, 45, 175, 7, 50, 43, 126, 90 };
static Object key = 126;
public static void main(String args[])
{ if( linearSearch() )
System.out.println(key + " found in the list");
else System.out.println(key + " not found in the list");
}
static boolean linearSearch(){
for( int i=0; i<a.length; i++) if(key == a[i]) return true;
return false; }
}
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Binary search
Iterative Binary search
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
2. Write Java programs to implement the List ADT using arrays and linked lists.
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
if( p == -1 || list[p].next == -1 ) {
System.out.println("No deletion");
return null;
}
int q = list[p].next; Object tmp =
list[q].data; list[p].next =
list[q].next;
count--;
return
tmp;
}
public void display() { int p = head;
System.out.print("\nList: [ "
); while( p != -1) {
System.out.print(list[p].data + " "); // print data p
= list[p].next; // advance to next node
}
System.out.println("]\n");//
}
public boolean isEmpty() { if(count == 0)
return true;
else return false;
} public int size() { return
count;
}
}
class ArrayListDemo {
public static void main(String[] args) { ArrayList linkedList
= new ArrayList(10);
linkedList.initializeList();
linkedList.createList(4); // create 4 nodes
linkedList.display(); // print the list
System.out.print("InsertFirst 55:");
linkedList.insertFirst(55); linkedList.display();
System.out.print("Insert 66 after 33:");
linkedList.insertAfter(66, 33); // insert 66 after 33
linkedList.display();
Object item = linkedList.deleteFirst(); System.out.println("Deleted node: " + item);
linkedList.display();
System.out.print("InsertFirst 77:");
linkedList.insertFirst(77); linkedList.display();
item = linkedList.deleteAfter(22); // delete node after node 22
System.out.println("Deleted node: " + item); linkedList.display();
System.out.println("size(): " + linkedList.size());
}
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
List: [ 11 22 33 44 ] InsertFirst
55: List: [ 55 11 22 33 44 ] Insert
66 after 33:
List: [ 55 11 22 33 66 44 ]
Deleted node: 55
List: [ 11 22 33 66 44 ] InsertFirst 77:
List: [ 77 11 22 33 66 44 ]
Deleted node: 33 List: [ 77 11 22
66 44 ]
size(): 5
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
}
public int size() { return count;
}
} // end of LinkeList class
Output:
Linked List: 11 -> 22 -> 33 -> 44 -> null
Linked List: 55 -> 11 -> 22 -> 33 -> 44 -> null
Linked List: 55 -> 11 -> 22 -> 33 -> 66 -> 44 -> null deleteFirst():
55
Linked List: 11 -> 22 -> 33 -> 66 -> 44 -> null deleteAfter(22): 33
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
size(): 3 C is
deleted
Stack is full
E is deleted
G is on top of stack
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Queue ADT
public interface Queue {
public void insert(Object ob);
public Object remove(); public
Object peek(); public boolean
isEmpty(); public int size();
}
class ArrayQueue implements Queue { private
int maxSize; // maximum queue size private
Object[] que; // que is an array
private int front;
private int rear;
private int count; // count of items in queue (queue size) public
ArrayQueue(int s) { // constructor
maxSize = s; que = new
Object[maxSize]; front = rear = -1;
count = 0; }
public void insert(Object item) // add item at rear of queue
{ if( count == maxSize )
{ System.out.println("Queue is Full");
return;
}
if(rear == maxSize-1 || rear == -1) { que[0]
= item; rear = 0;
if( front == -1) front = 0;
} else que[++rear] = item;
count++; // update queue size
}
public Object remove(){ // delete item from front of queue
if( isEmpty() ){
System.out.println("Queue is Empty"); return 0;
}
Object tmp = que[front]; // save item to be deleted que[front] =
null; // make deleted item‟s cell empty
if( front == rear ) rear = front
= -1;
else if( front == maxSize-1 ) front = 0;
else front++;
count--; // less one item from the queue size
return tmp;
}
public Object peek() // peek at front of the queue
{ return que[front];
}
public boolean isEmpty() // true if the queue is empty
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
class InfixToPostfixDemo
{ public static void main(String args[])
{ InfixToPostfix obj = new InfixToPostfix();
String infix = "A*(B+C/D)-E";
System.out.println("infix: " + infix );
System.out.println("postfix:"+obj.toPostfix(infix)); }
}
Output:
infix: A*(B+C/D)-E postfix:
ABCD/+*E-
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
5.Write Java programs to implement the following using a singly linked list.
Output:
Contents of Stack: [ 40 35 20 ]
Popped item: 40
Contents of Stack: [ 35 20 ]
Contents of Stack: [ 75 70 65 35 20 ]
Popped item: 75 peek(): 70
Contents of Stack: [ 70 65 35 20 ]
Contents of Stack: [ 90 70 65 35 20 ]
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
class LinkedQueueDemo {
public static void main(String[] args) { LinkedQueue q = new
LinkedQueue();
q.display();
q.insert('A');
q.insert('B');
q.insert('C');
q.insert('D');
q.display();
System.out.println("delete(): " + q.remove()); q.display();
System.out.println("peek(): " + q.peek()); q.insert('E');
q.insert('F');
System.out.println("delete(): " + q.remove()); q.display();
System.out.println("size(): " + q.size());
}
}
Output:
Linked Q: empty Linked Q: A
B C D remove(): A Linked Q:
B C D
peek(): B remove(): B
Linked Q: C D E F size(): 4
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
6. Write Java programs to implement the deque (double ended queue) ADT using
(a) Array
(b) Doubly linked list.
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
deleteFirst(): A
first:1, last:3, count: 3
0 1 2 3 4 5 Deque: B C D
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
Deque is empty
removeFirst(): null Deque: [
C B A ] getFirst(): C
getLast(): E Deque: [ C B A
D E ] removeFirst(): C
removeLast(): E Deque: [ B A
D ] size(): 3
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Node head; // “head” refers to first node public void insert(String item, int
pkey){ // insert item after pkey Node newNode = new Node(item, pkey); //
create new node int k; if( head == null ) k = 1;
else if( newNode.prn < head.prn ) k = 2;
else k = 3;
switch( k ){
case 1: head = newNode; // Q is empty, add head node head.next
= null; break; case 2: Node oldHead = head; // add one
item before head head = newNode; newNode.next = oldHead;
break; case 3: Node p = head; // add item before a node
Node prev = p;
Node nodeBefore = null; while( p !=
null ) {
if( newNode.prn < p.prn ){ nodeBefore = p;
break;
}
else {
prev = p; // save previous node of current node p =
p.next; // move to next node
}
} // end of while
newNode.next = nodeBefore;
prev.next = newNode;
} // end of switch
} // end of insert() method
public Node delete(){
if( isEmpty() ){
System.out.println("Queue is empty"); return null;
} else {
Node tmp = head; head =
head.next; return tmp;
}
}
public void displayList() {
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
Queue: Kim(1) Nitin(2) Laxmi(2) Babu(3) Jimmy(3) delete(): Kim(1)
Queue: Nitin(2) Laxmi(2) Babu(3) Jimmy(3)
Queue: Anu(1) Nitin(2) Laxmi(2) Scot(2) Babu(3) Jimmy(3) Lehar(4)
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
8. Write Java program that use recursive and non-recursive functions to traverse the given binary tree in
(a) Preorder (b) In order and (c) Post
order.
Program:
import java.util.*;
// Driver code
public static void main(String[] args)
{ Node root = new Node(1);
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
System.out.println();
System.out.print("Post Order : "); for (int i :
post) {
System.out.print(i + " ");
}
System.out.println();
System.out.print("In Order : "); for (int i : in) {
System.out.print(i + " ");
}
}
}
Output:
Pre Order : 1 2 4 8 12 5 9 3 6 7 10 11 Post Order : 12 8 4 9 5 2
6 10 11 7 3 1
In Order : 8 12 4 2 9 5 1 6 3 10 7 11
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
9. Write a Java program that displays node values in a level order traversal (Traverse the tree one level at a
time, starting at the root node) for a binary tree.
Program:
import java.util.LinkedList;
import java.util.Queue;
while (!queue.isEmpty()) {
// Dequeue a node from the front of the queue
TreeNode currentNode = queue.poll();
System.out.print(currentNode.data + " "); // Print the node's value
}
}
}
Output:
Level-order traversal of the binary
tree: 1 2 3 4 5 6 7
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
10. Write a java program that uses recursive functions, (a) To Create a binary
search tree.
(b) To count the number of leaf nodes.
(c) To copy the above binary search tree.
(a) Program:
// Class representing a node in the binary search tree class TreeNode
{ int data; TreeNode left, right;
// Method to insert a new node (wrapper for recursive insert) void insert(int data)
{ root = insert(root, data);
}
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
In-order traversal of the binary search tree:
20 30 40 50 60 70 80
(b) Program:
// Class representing a node in the binary search tree class TreeNode { int
data;
TreeNode left, right;
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
Number of leaf nodes: 4
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
(c) Program:
// Class representing a node in the binary search tree class TreeNode
{ int data;
TreeNode left, right;
// Create a new node and recursively copy the left and right subtrees
TreeNode newNode = new TreeNode(node.data);
newNode.left = copyTree(node.left);
newNode.right = copyTree(node.right);
return newNode;
}
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
In-order traversal of the copied BST:
20 30 40 50 60 70 80
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
11. Write Java Programs for the implementation of bfs and dfs for a given graph
Program:
import java.util.LinkedList;
import java.util.Queue;
public class Main { static class TreeNode { int
data; TreeNode left, right;
public TreeNode(int key)
{ data = key;
left = right = null;
}
}
static void preorder(TreeNode TreeNode) {
if (TreeNode == null)
return;
System.out.print(TreeNode.data + "
"); preorder(TreeNode.left);
preorder(TreeNode.right);
}
static void inorder(TreeNode TreeNode)
{ if (TreeNode == null)
return;
inorder(TreeNode.left);
System.out.print(TreeNode.data + "
");
// Traverse right
inorder(TreeNode.right);
}
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
{
TreeNode root = new TreeNode(0); root.left = new
TreeNode(1); root.right = new TreeNode(2);
root.left.left = new TreeNode(3); root.left.right =
new TreeNode(4);System.out.println("Inorder traversal");
inorder(root);
System.out.println("\nPostorder traversal");
postorder(root);
Output :
In order traversal
31402
Preorder traversal
01342
Post order
traversal 3 4 1 2 0
Level order
traversal 0 1 2 3 4
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
12. Write Java Programs for implementing the following sorting methods:
(a) Bubble sort
(b) Selection sort
(c) Insertion sort
(d) Radix sort
Output:
Before sorting array elements are - 35 10 31 11 26
After sorting array elements are -
10 11 26 31 35
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
printArray(arr);
}
}
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
Before Sorting array elements are-
12, 11, 13, 5, 6
After Sorting array elements are-
5, 6, 11, 12, 13
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Output:
Radix Sort Test
Enter number of integer elements 5
Enter 5 integer elements
16
32
7
5
25
Elements after sorting
5 7 16 25 32
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
13. Write a Java Program for implementing KMP pattern matching algorithm.
Programs :
// JAVA program for implementation of KMP pattern
// searching algorithm class KMP_String_Matching
{ void KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
22Q71A0454
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
int i = 1;
lps[0] = 0; // lps[0] is always 0
Output:
Found pattern at index 10
22Q71A0454