DS Lab Programs
DS Lab Programs
import java.io.*;
import java.util.Scanner;
class ArrayOperation
{
private int[] theArray;
private int capacity;
private int size;
public ArrayOperation()
{
capacity=30;
size=1;
theArray=new int[capacity];
}
public void insertAtLast(int ele)
{
if(size==capacity)
{
return;
}
else
{
theArray[size++]=ele;
}
}
public void print()
{
for(int i=1;i<size;i++)
{
System.out.print(" "+theArray[i]+" ");
}
}
public void delete(int pos)
{
int i=size;
while(pos<i)
{
theArray[pos]=theArray[pos+1];
pos++;
}
size=size-1;
}
public void deleteAtLast()
{
int del_ele=theArray[size-1];
System.out.println("Last element in the array is "+del_ele+"deleted");
size--;
}
public void addAtMiddle(int ele,int pos)
{
int i=size;
while(i>=pos)
{
theArray[i+1]=theArray[i];
i--;
}
size++;
theArray[pos]=ele;
}
public void sort()
{
for(int i=1;i<size-1;i++)
for(int j=i+1;j<size;j++)
{
if(theArray[i]>theArray[j])
{
int temp=theArray[i];
theArray[i]=theArray[j];
theArray[j]=temp;
}
}
}
public void search(int ele)
{
boolean flag=true;
for(int i=1;i<size;i++)
if(theArray[i]==ele)
{
System.out.println("\n\tsearch is successfull");
System.out.println("\n\tElement found at\t "+i+"\t location");
flag=false;
break;
}
if(flag)
{
System.out.println("\n\tSearch is unsuccessfull");
System.out.println("\n\tElement is not there in an array");
}
}
}
class ArrayOperationImplementation
{
public static void main(String[] args)
{
ArrayOperation ao=new ArrayOperation();
int ele,pos;
while(true)
{
Scanner s=new Scanner(System.in);
System.out.println("*******MENU FOR ARRAY OF
OPERATION*******");
System.out.println("\n \t 1. Insert Element At Last \n \t 2. Insert Element
At Middle \n \t 3. Delete At Last \n \t 4. Delete Element At Middle \n \t 5. Sorting \n \t 6.
Searching \t\n \t 7. Traversing");
System.out.println("Enter your choice");
int ch=s.nextInt();
switch(ch)
{
Source Code:
class Stack {
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
}
// Driver code
class Main {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
}
Output : E:\ javac Stack.java
E:\ java Stack
import java.io.*;
import java.util.Scanner;
class Stack
{
int item;
Stack next;
public Stack()
{
this.item=0;
this.next=null;
}
public Stack(int item)
{
this.item=item;
this.next=next;
}
}
class StackImpleByList
{
Stack head,top;
int size;
public StackImpleByList()
{
this.head=null;
this.size=0;
}
public void push(int item)
{
Stack newItem=new Stack(item);
if(head==null)
{
head=newItem;
top=head;
size++;
}
else
{
Stack current=head;
while(current.next!=null)
current=current.next;
current.next=newItem;
top=current.next;
size++;
}
}
public void pop()
{
if(head==null)
System.out.println("Stack is under flow");
else
{
Stack current=head;
while(current.next.next!=null)
current=current.next;
current.next=null;
top=current;
size--;
}
}
public void print()
{
if(head==null)
System.out.println("Stack is empty");
else
{
Stack current=head;
while(current.next!=null)
{
System.out.println(" "+current.item+ " ");
current=current.next;
}
System.out.println(" "+current.item+ " ");
}
}
public int getStackSize()
{
return size;
}
public void peep()
{
System.out.println("Item on top of the stack is"+top.item);
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
StackImpleByList sbi=new StackImpleByList();
while(true)
{
System.out.println(".....Menu....");
System.out.println("\n \n \t 1. push (Adding the element into stack at top position \n \n \t 2. pop
(Deleting a element from stack at top of the stack) \n \n \t 3. Peep (Accessing the top element of
stack) \n \n \t 4. Print Stack \n \n \t 5. Get size of the stack");
System.out.println("\n \n \t Enter your choice");
int choice=s.nextInt();
switch(choice)
{
case 1: int ele;
System.out.println("Enter the item to be added into the queue");
ele=s.nextInt();
sbi.push(ele);
break;
case 2:
sbi.pop();
break;
case 3:
sbi.peep();
break;
case 4:
System.out.println("Stack items are:");
sbi.print();
break;
case 5:
System.out.println("Size of the stack is::"+sbi.getStackSize());
break;
default: System.out.println("Wrong choice");
}
System.out.println("\n\n \t Do you want continue...");
char ch=s.next().charAt(0);
if(ch=='N' || ch=='n')
{
System.out.println("\n \n \t Thank you and see you again...");
break;
}
}
}
}
Output:
import java.util.Scanner;
class DLink
{
private int data;
public DLink prev;
public DLink next;
public DLink(int data)
{
this.prev=null;
this.data=data;
this.next=null;
}
public DLink(DLink prev,int data,DLink next)
{
this.prev=prev;
this.data=data;
this.next=next;
}
public String Data()
{
return data+"-->";
}
}
class DoubleLinkedList
{
private DLink head;
private int size;
public DoubleLinkedList()
{
head=null;
size=0;
}
public boolean isEmpty()
{
return head==null;
}
public void addFront(int data)
{
if(head==null)
head=new DLink(null,data,null);
else
{
DLink newLink=new DLink(null,data,null);
head.prev=newLink;
newLink.next=head;
head=newLink;
}
size++;
}
public void addLast(int data)
{
if(head==null)
head=new DLink(null,data,null);
else
{
DLink current=head;
while(current.next!=null)
current=current.next;
DLink newLink=new DLink(null,data,null);
current.next=newLink;
newLink.prev=current;
}
size++;
}
public void deleteFront()
{
if(head==null)
return;
else
{
head=head.next;
head.prev=null;
size--;
}
}
public void deleteLast()
{
if(head==null)
return;
if(head.next==null)
{
head=null;
size--;
}
DLink current=head;
while(current.next.next!=null)
current=current.next;
current.next=null;
size--;
}
public void addAny(int data,int pos)
{
if(head==null)
head=new DLink(null,data,null);
DLink current=head;
for(int i=1;i<pos;i++)
current=current.next;
DLink newLink=new DLink(current,data,null);
newLink.next=current.next;
current.next.prev=newLink;
current.next=newLink;
size++;
}
public void deleteAny(int pos)
{
if(head==null)
return;
if(head.next==null)
{
head=null;
size--;
}
DLink current=head;
if(size==2)
{
current.next=null;
size--;
}
else
{
for(int i=1;i<pos;i++)
current.next=current.next.next;
current.next.prev=current;
size--;
}
}
public void reverseTraversal()
{
int i,j;
DLink current=head;
for(i=0;i<size-1;i++)
current=current.next;
for(j=i;j>=0;j--)
{
System.out.print(current.Data());
current=current.prev;
}
System.out.print("null");
}
public void print()
{
if(head==null)
System.out.println("List is empty...");
DLink current=head;
while(current.next!=null)
{
System.out.print(current.Data());
current=current.next;
}
System.out.print(current.Data()+"null");
System.out.println();
}
public static void main(String...args)
{
Scanner sc=new Scanner(System.in);
DoubleLinkedList dll=new DoubleLinkedList();
int info,pos;
while(true)
{
System.out.println("........Menu........");
System.out.println("\n\t\t 1. Add Node At Front \n\t\t 2. Add Node At Rear \n\t\t 3.
Remove Node At Front \n\t\t 4. Remove At Last \n\t\t 5. Add Node At Position \n\t\t 6. Remove
Node At Any pos\n\t\t 7. Display elements in list \n\t\t 8. ReverseTraversal \n\n ");
System.out.println("\n Enter your choice\n");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter data part to a node");
info=sc.nextInt();
dll.addFront(info);
break;
case 2: System.out.println("Enter data part to a node");
info=sc.nextInt();
dll.addLast(info);
break;
case 3: dll.deleteFront();
System.out.println("\n Front node is deleted");
break;
case 4: dll.deleteLast();
System.out.println("\n Last node is deleted");
break;
case 5: System.out.println("Enter the data part to the node");
info=sc.nextInt();
System.out.println("Enter the postion where you insert a node in
list");
pos=sc.nextInt();
dll.addAny(info,pos);
break;
case 6: System.out.println("Enter the postion where you delete a node in
list");
pos=sc.nextInt();
dll.deleteAny(pos);
break;
case 7: dll.print();
break;
case 8: dll.reverseTraversal();
break;
default: System.out.println("You are selection is not there in menu\n");
}
System.out.println("\n \n Do you want to contine....\n Press y or Y for(Yes)|| n or N
for(No)");
char ch=sc.next().charAt(0);
if(ch=='n'||ch=='N')
{
System.out.println("\n\n Thank you see you again..\n\n");
break;
}
}
}
}
Output:
E:\ javac Queue.java 3. Print elements of Queue
4. Display Head
E:\javac QueueImpleByList.java 5. Display Tail
Node class:
import java.util.Scanner;
public class BinarySearchTreeDeletion
{
Node root;
public void add(int data)
{
Node nodeToAdd=new Node(data);
if(root==null)
root=nodeToAdd;
else
traverseAndAddNode(root,nodeToAdd);
}
public void traverseAndAddNode(Node node,Node nodeToAdd)
{
if(nodeToAdd.getData()<node.getData())
{
if(node.leftChild==null)
{
nodeToAdd.parent=node;
node.leftChild=nodeToAdd;
}
else
{
traverseAndAddNode(node.leftChild,nodeToAdd);
}
}
else if(nodeToAdd.data>node.data)
{
if(node.rightChild==null)
{
nodeToAdd.parent=node;
node.rightChild=nodeToAdd;
}
else
{
traverseAndAddNode(node.rightChild,nodeToAdd);
}
}
}
public static void preOrderTraversal(Node root)
{
if(root==null)
return;
System.out.println(root.getData()+" ");
preOrderTraversal(root.leftChild);
preOrderTraversal(root.rightChild);
}
public static void inOrderTraversal(Node root)
{
if(root==null)
return;
inOrderTraversal(root.leftChild);
System.out.println(root.getData()+" ");
inOrderTraversal(root.rightChild);
}
public static void postOrderTraversal(Node root)
{
if(root==null)
return;
postOrderTraversal(root.leftChild);
postOrderTraversal(root.rightChild);
System.out.println(root.getData()+" ");
}
public boolean delete(int val)
{
//case 1: node has no childrens
//case 2:node has one child;
//case 3: node has two childrens
Node nodeTobeDeleted=find(val);
if(nodeTobeDeleted!=null)
{
if(nodeTobeDeleted.leftChild==null && nodeTobeDeleted.rightChild==null)
{
deleteCase1(nodeTobeDeleted);
}
else if(nodeTobeDeleted.leftChild!=null && nodeTobeDeleted.rightChild!=null)
{
deleteCase3(nodeTobeDeleted);
}
else if(nodeTobeDeleted.leftChild!=null)
{
deleteCase2(nodeTobeDeleted);
}
else if(nodeTobeDeleted.rightChild!=null)
{
deleteCase2(nodeTobeDeleted);
}
}
return false;
}
public Node find(int val)
{
if(root!=null)
{
return findNode(root,new Node(val));
}
return null;
}
private Node findNode(Node search,Node node)
{
if(search==null)
return null;
if(search.data==node.data)
{
return search;
}
else
{
Node returnNode=findNode(search.leftChild,node);
if(returnNode==null)
{
returnNode=findNode(search.rightChild,node);
}
return returnNode;
}
}
private void deleteCase1(Node nodeTobeDeleted)
{
if(nodeTobeDeleted.parent.leftChild==nodeTobeDeleted)
{
nodeTobeDeleted.parent.leftChild=null;
}
else if(nodeTobeDeleted.parent.rightChild==nodeTobeDeleted)
{
nodeTobeDeleted.parent.rightChild=null;
}
}
private void deleteCase2(Node nodeTobeDeleted)
{
if(nodeTobeDeleted.parent.leftChild==nodeTobeDeleted)
{
if(nodeTobeDeleted.leftChild!=null)
{
nodeTobeDeleted.parent.leftChild=nodeTobeDeleted.leftChild;
}
else if(nodeTobeDeleted.rightChild!=null)
{
nodeTobeDeleted.parent.rightChild=nodeTobeDeleted.rightChild;
}
}
else if(nodeTobeDeleted.parent.rightChild==nodeTobeDeleted)
{
if(nodeTobeDeleted.leftChild!=null)
{
nodeTobeDeleted.parent.rightChild=nodeTobeDeleted.leftChild;
}
else if(nodeTobeDeleted.rightChild!=null)
{
nodeTobeDeleted.parent.rightChild=nodeTobeDeleted.rightChild;
}
}
}
private Node minLeftTraversal(Node node)
{
if(node.leftChild==null)
{
return node;
}
return minLeftTraversal(node.leftChild);
}
private void deleteCase3(Node nodeTobeDeleted)
{
Node minNode=minLeftTraversal(nodeTobeDeleted.rightChild);
deleteCase1(minNode);
minNode.parent=nodeTobeDeleted.parent;
minNode.leftChild=nodeTobeDeleted.leftChild;
minNode.rightChild=nodeTobeDeleted.rightChild;
if(nodeTobeDeleted.parent==null)
{
root=minNode;
}
else
{
if(nodeTobeDeleted.parent.leftChild==nodeTobeDeleted)
{
minNode.parent.leftChild=minNode;
}
else if(nodeTobeDeleted.parent.rightChild==nodeTobeDeleted)
{
minNode.parent.rightChild=minNode;
}
}
}
public static void main(String...args)
{
Scanner s=new Scanner(System.in);
BinarySearchTreeDeletion bst=new BinarySearchTreeDeletion();
bst.add(50);
bst.add(25);
bst.add(75);
bst.add(10);
bst.add(30);
bst.add(60);
bst.add(55);
bst.add(53);
bst.add(85);
bst.add(80);
bst.add(76);
bst.add(1);
bst.add(100);
while(true)
{
System.out.println("\n \t 1. In - Order Traversal \n \t 2. Pre - Order Traversal \n \t 3. Post - Order
Traversal");
int choice;
System.out.println("Enter your choice::");
choice=s.nextInt();
switch(choice)
{
case 1:
System.out.println("In-Order Traversal");
inOrderTraversal(bst.root);
System.out.println();
break;
case 2:
System.out.println("Pre-Order Traversal");
preOrderTraversal(bst.root);
break;
case 3:
System.out.println("post-Order Traversal");
postOrderTraversal(bst.root);
break;
}
System.out.println("Do you want to delete a node enter a node value else press 0");
int no_to_del=s.nextInt();
bst.delete(no_to_del);
if(no_to_del==0)
System.out.println("Node"+no_to_del+"is not there");
else
System.out.println("Node"+no_to_del+"is deleted please select any traversal technique to see the
result");
System.out.println("do you want to continue... \n Press Y||y for 'Yes' OR N || n for 'No'");
char ch=s.next().charAt(0);
if(ch=='N' || ch=='n')
{
System.out.println("Thank you and see you again.....");
break;
}
}
}
}
Output:
E:\ javac Node.java Press Y||y for 'Yes' OR N || n for 'No'
E:\ javac BinarySearchTreeDeletion.java y
heap.print();
}
}
Output:
E:\javac MaxHeapImplementation.java
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;
class InsertionSort
{
int []a;
int capacity;
int size;
public InsertionSort()
{
capacity=20;
a=new int[capacity];
size=1;
}
public void fillArray(int ele)
{
a[size++]=ele;
}
public void print()
{
for(int i=1;i<size;i++)
System.out.println("\n \t a["+i+"]:"+a[i]+" ");
}
public void sort()
{
for(int i=2;i<size;i++)
{
int key=a[i];
int j=i-1;
while(j>0 && a[j]>key)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
}
public static void main(String[] args)
{
InsertionSort is=new InsertionSort();
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println("\n \t ....Menu...");
System.out.println("\n \t 1.Inserting Element \n \t 2. Sort \n \t 3. Display ");
System.out.println("\n \tEnter your choice");
int ch=s.nextInt();
switch(ch)
{
case 1:
System.out.println("\n \tEnter the element to be insert");
int ele=s.nextInt();
is.fillArray(ele);
System.out.println("\n \tEnter"+ele+"is inserted");
break;
case 2: is.sort();
System.out.println("\n \tElements are sorted, See the result select third option");
break;
E:\java InsertionSort
....Menu...
Enter your choice
1.Inserting Element 1
2. Sort Enter the element to be insert
3. Display 10
Enter your choice
1 Enter10is inserted
Enter the element to be insert
-96 Do you want to continue...
Enter-96is inserted y
a[1]:-100 a[3]:10
Output:
E:\java MergesortExample
Before sorting the elements are:
58 95 44 76 60 60 42 90 70 0
After sorting the elements are:
0 42 44 58 60 60 70 76 90 95
Quick Sort Program
import java.util.Scanner;
public class QuickSort
{
public static void sort(int[] arr)
{
quickSort(arr, 0, arr.length - 1);
}
public static void quickSort(int arr[], int low, int high)
{
int i = low, j = high;
int temp;
int pivot = arr[(low + high) / 2];
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if (low < j)
quickSort(arr, low, j);
if (i < high)
quickSort(arr, i, high);
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Quick Sort Test\n");
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr);
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
Output:
E:\java QuickSort
Enter number of integer elements
5
Enter 5 integer elements
-10
-20
9
6
3
Elements after sorting
-20 -10 3 6 9
Linear Search
import java.io.*;
import java.util.Scanner;
class LinearSearch
int []theArray;
int size;
int pos;
int capacity;
public LinearSearch()
size=0;
pos=1;
capacity=30;
theArray=new int[capacity];
{
theArray[pos++]=ele;
size++;
int status=0;
for(int i=1;i<=size;i++)
if(theArray[i]==x)
status=1;
break;
if(status==0)
for(int i=1;i<=size;i++)
}
public static void main(String[] args)
while(true)
int ele=s.nextInt();
ls.insertElementIntoArray(ele);
char ch=s.next().charAt(0);
if(ch=='n'||ch=='N')
break;
ls.print();
int s_ele=s.nextInt();
ls.linearSearch(s_ele);
}
Output:
E:\java LinearSearch
import java.io.*;
import java.util.Scanner;
class BinarySearch
{
int []theArray;
int size;
int pos;
int capacity;
public BinarySearch()
{
size=0;
pos=1;
capacity=30;
theArray=new int[capacity];
}
public void insertElementIntoArray(int ele)
{
theArray[pos++]=ele;
size++;
}
public void binarySearch(int x)
{
int low=1,high=size;
while(low<=high)
{
int mid=(low+high)/2;
if(theArray[mid]==x)
{
System.out.println("Element found at" +mid + "position");
break;
}
else if(x<theArray[mid])
{
high=mid-1;
}
else if(x>theArray[mid])
{
low=mid+1;
}
}
if(low>high)
{
System.out.println("Element is not found");
}
}
public void sort()
{
for(int i=1;i<size;i++)
for(int j=i+1;j<=size;j++)
{
if(theArray[i]>theArray[j])
{
int temp=theArray[i];
theArray[i]=theArray[j];
theArray[j]=temp;
}
}
}
public void print()
{
for(int i=1;i<=size;i++)
{
System.out.print(" " +theArray[i]+ " ");
}
}
public static void main(String[] args)
{
BinarySearch bs=new BinarySearch();
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println("\n \n \tEnter the element into array");
int ele=s.nextInt();
bs.insertElementIntoArray(ele);
System.out.println("\n \n \tDo you want to enter another element press y or n");
char ch=s.next().charAt(0);
if(ch=='n'||ch=='N')
{
break;
}
}
System.out.println("\n \n \tThe elements in array are");
bs.print();
System.out.println("\n \n \tPlz enter the element to be search");
int s_ele=s.nextInt();
bs.sort();
bs.binarySearch(s_ele);
}
}
Output:
Successful case:
Failure Case:
E:\java Binary Search