Programs Sssssssss
Programs Sssssssss
Programs Sssssssss
import java.util.*;
/**
*
* @author hp <
*/
public class ListAdtArray {
public static void main(String args[])
{
int n,pos,x, del, count=0,elem;
int a[] = new int[50];
Scanner s=new Scanner(System.in);
System.out.println("Enter the array length:");
n=s.nextInt();
System.out.println("Enter "+n+" elements:");
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
System.out.println("ListADT Array");
char ch;
/* Perform list operations */
do
{
System.out.println("ListADT Array Operations\n");
System.out.println("1. insert");
System.out.println("2. delete");
System.out.println("3. display");
System.out.println("4. Search");
System.out.println("Enter your choice");
int choice = s.nextInt();
switch (choice)
{
case 1 :
System.out.print("Enter the index value where you want to insert
element:");
pos = s.nextInt();
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
n++;
for(int i=n; i>pos; i--)
{
a[i] = a[i-1];
}
a[pos] = x;
System.out.print("Element inserted Successfully..!!\n");
System.out.print("Array elements are :\n");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+ " ");
}
break;
case 2 :
System.out.print("Enter Element to be Delete : ");
del = s.nextInt();
for(int i=0; i<n; i++)
{
if(a[i] == del)
{
for(int j=i; j<(n-1); j++)
{
a[j] = a[j+1];
}
count++;
break;
}
}
if(count==0)
{
System.out.print("Element Not Found..!!");
}
else
{
System.out.print("Element Deleted Successfully..!!\n");
n=n-1;
System.out.print("Array elements are :\n");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+ " ");
}
}
break;
case 3 :
System.out.print("Array elements are :\n");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+ " ");
}
break;
case 4:
int i;
System.out.println("enter the element you want to search :");
elem=s.nextInt();
for(i=0; i<n; i++)
{
if(a[i]==elem)
{
System.out.println("Element found at position : "+(i+1));
}
}
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
Output:
Enter the array length:
5
Enter 5 elements:
32 21 45 34 65
ListADT Array
ListADT Array Operations
1. insert
2. delete
3. display
4. Search
Enter your choice
1
Enter the index value where you want to insert element:3
Enter the element you want to insert:46
Element inserted Successfully..!!
Array elements are :
32 21 45 46 34 65
Do you want to continue (Type y or n)
y
ListADT Array Operations
1. insert
2. delete
3. display
4. Search
Enter your choice
2
Enter Element to be Delete : 21
Element Deleted Successfully..!!
Array elements are :
32 45 46 34 65
Do you want to continue (Type y or n)
y
ListADT Array Operations
1. insert
2. delete
3. display
4. Search
Enter your choice
4
enter the element you want to search :
34
Element found at position : 4
1. insert
2. delete
3. display
4. Search
Enter your choice
3
Array elements are :
32 45 46 34 65
Do you want to continue (Type y or n)
n
BUILD SUCCESSFUL (total time: 1 minute 4 seconds)
Program:
import java.util.Scanner;
/* Class Node */
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size ;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void insertAtStart(int val)
{
Node nptr = new Node(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print("\nSingly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ "->");
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ "\n");
}
}
/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
System.out.println("\nSingly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Output:
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
23
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
35
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
67
Enter position
2
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = false
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
6
Size = 2
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
6
Invalid position
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true
n
BUILD SUCCESSFUL (total time: 2 minutes 19 seconds)
Program:
import java.util.*;
class Queen
{
//number of queens
private static int N;
//chessboard
private static int[][] board = new int[100][100];
int i,j;
//calling the function
nQueen(N);
//printing the matix
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
System.out.print(board[i][j]+"\t");
System.out.print("\n");
}
}
}
Output:
First run:
Enter the value of N for NxN chessboard
4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
BUILD SUCCESSFUL (total time: 12 seconds)
Second run:
Enter the value of N for NxN chessboard
6
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 1
1 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 1 0
BUILD SUCCESSFUL (total time: 39 seconds)
Program:
import java.io.*;
class QueueImpl
{
static int i,front,frontelem,rear,item,max=5,ch;
static int a[]=new int[5];
QueueImpl()
{
front=-1;
rear=-1;
}
public static void main(String args[])throws IOException
{
System.out.println("--Queue Operations--");
while((boolean)true)
{
try
{
System.out.println("Select Option 1.Enqueue 2.Dequeue 3.display
4.Rear 5.front 6.Exit");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception e)
{ }
if(ch==6)
break;
else
{
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
rear();
break;
case 5:
front();
break;
}
}
}
}
static int rear(){
item=a[rear];
System.out.println("Rear element is : "+item);
return a[rear];
}
static int front(){
frontelem=front+1;
item=a[frontelem];
System.out.println("Front element is : "+item);
return a[front];
}
// static boolean isQueueFull(){
// return (maxSize == currentSize);
// }
//
// static boolean isQueueEmpty(){
// return (currentSize == 0);
// }
static void insert()
{
if(rear>=(max-1))
{
System.out.println("Queue is Full");
}
else
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Element: ");
item=Integer.parseInt(br.readLine());
}
catch(Exception e)
{}
rear=rear+1;
a[rear]=item;
}
}
static void delete()
{
if(front==-1)
{
System.out.println("Queue is Empty");
}
else
{
front=front+1;
item=a[front];
System.out.println("Deleted Item: "+item);
}
}
static void display()
{
System.out.println("Elements in the Queue are:");
for(int i=front+1; i<=rear; i++)
{
System.out.println(a[i]);
}
}
}
Output:
--Queue Operations--
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
1
Enter the Element:
14
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
1
Enter the Element:
63
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
1
Enter the Element:
75
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
3
Elements in the Queue are:
14
63
75
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
4
Rear element is : 75
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
5
Front element is : 14
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
2
Deleted Item: 14
Select Option 1.Enqueue 2.Dequeue 3.display 4.Rear 5.front 6.Exit
6
BUILD SUCCESSFUL (total time: 43 seconds)
Program:
Output:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.Scanner;
}
Output:
First run:
Enter string: radar
That is a palindrome.
BUILD SUCCESSFUL (total time: 5 seconds)
Second run:
Enter string: abbadbf
That is not a palindrome.
BUILD SUCCESSFUL (total time: 4 seconds)
Program:
import java.lang.*;
import java.util.*; //to read user input
public class StackLinkedList
{
public static void main(String args[])
{
int max=10,top,ch,x; //variables declaration
top=-1; //intially stack empty
StackLinkedList l=new StackLinkedList();
do
{
System.out.println("1:push 2:pop 3:stacktop 4:display"
+ " 5:exit" + "\n" + "enter ur choice:");
Scanner s=new Scanner(System.in); //to read input
ch=s.nextInt(); //reading value
if(ch==5)
{
System.out.println("exiting..."); //to exit from loop
break;
}
else
{
switch(ch) //for choosing one of the options
{
case 1:
System.out.println("enter element:");
x=s.nextInt();
if(top==max) //reached maxsize
System.out.println("stack is full");
else
l.push(x);
top++; //increment top
break;
case 2:
if(top==-1) //reached minsize
System.out.println("stack is empty");
else
l.pop();
break;
case 3:
System.out.println("stack top element:"+(l.stacktop()));
break;
case 4:
System.out.println("stack elements are:");//displaying all elements
l.display();
break;
default: //if incorrect option
System.out.println("enter correct choice");
break;
}
}
}
public void push(int x)
{
Node temp=new Node();
temp.data=x;
temp.link=top;
top=temp;
}
public void pop()
{
top=(top).link;
}
public int stacktop()
{
return top.data;
}
public void display()
{
Node temp=top;
while(temp!=null)
{
System.out.println(temp.data);
temp=temp.link;
}
}
} //class
Output:
import java.io.*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void displayNode()
{
System.out.print(data+" ");
}
}
class LinkList
{
private Node first;
private Node last;
public LinkList()
{
first=null;
last=null;
}
public void insertLast(int x)
{
Node newNode=new Node(x);
newNode.next=null;
if(isEmpty())
first=newNode;
else
last.next=newNode;
last=newNode;
}
public int deleteFirst()
{
int t=first.data;
if(first.next==null)
last=null;
first=first.next;
return t;
}
public int peekFirst()
{
return(first.data);
}
public boolean isEmpty()
{
return(first==null);
}
public void displayList()
{
Node current=first;
while(current!=null)
{
current.displayNode();
current=current.next;
}
}
}
class Queue
{
private LinkList l;
public Queue()
{
l=new LinkList();
}
public void insert(int x)
{
l.insertLast(x);
System.out.println("Inserted");
}
public int delete()
{
return l.deleteFirst();
}
public boolean isQueueEmpty()
{
return l.isEmpty();
}
public void display()
{
l.displayList();
}
public int peek()
{
return l.peekFirst();
}
}
class QueueList
{
public static void main(String args[])throws IOException
{
Queue q=new Queue();
int ch,d;
while((boolean)true)
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("MENU");
System.out.println("--------");
System.out.println("1.Insert");
System.out.println("2.Delete");
System.out.println("3.Peek");
System.out.println("4.Display");
System.out.println("5.Exit");
System.out.println("Enter Your Choice: ");
ch=Integer.parseInt(br.readLine());
if(ch==5)
break;
else
{
switch(ch)
{
case 1:
System.out.println("Enter Number of Elements");
int n1=Integer.parseInt(br.readLine());
System.out.println("\nEnter elements: ");
for(int i=0; i<n1; i++)
{
d=Integer.parseInt(br.readLine());
q.insert(d);
}
break;
case 2:
if(q.isQueueEmpty())
System.out.println("Queue is Empty ");
else
{
d=q.delete();
System.out.println("Deleted data:- "+d);
}
break;
case 3:
if(q.isQueueEmpty())
System.out.print("Queue is Empty ");
else
{
d=q.peek();
System.out.println("First item:- "+d);
}
break;
case 4:
if(q.isQueueEmpty())
System.out.println("Queue is Empty ");
else
{
System.out.println("Datas in Queue ");
q.display();
}
break;
default:
System.out.println("Invalid choice ");
}
}
System.out.println(" ");
}
}
}
Output:
MENU
--------
1.Insert
2.Delete
3.Peek
4.Display
5.Exit
Enter Your Choice:
1
Enter Number of Elements
3
Enter elements:
14
Inserted
53
Inserted
98
Inserted
MENU
--------
1.Insert
2.Delete
3.Peek
4.Display
5.Exit
Enter Your Choice:
2
Deleted data:- 14
MENU
--------
1.Insert
2.Delete
3.Peek
4.Display
5.Exit
Enter Your Choice:
3
First item:- 53
MENU
--------
1.Insert
2.Delete
3.Peek
4.Display
5.Exit
Enter Your Choice:
4
Datas in Queue
53 98
MENU
--------
1.Insert
2.Delete
3.Peek
4.Display
5.Exit
Enter Your Choice:
5
Program:
import java.io.*;
class Dequeue
{
int arr[];
int lim;
int front;
int rear;
public Dequeue(int l)
{
lim=l;
front=rear=0;
arr=new int[(l+1)];
}
void addfront(int val)
{
if(front==0 && rear ==0)
{
front=1;
rear=1;
arr[1]=val;
}
else if(rear==lim)
{
System.out.println("Overflow");
}
else
{
arr[++rear]=val;
}
}
void addrear(int val)
{
if(front==0 && rear ==0)
{
front=1;
rear=1;
arr[1]=val;
}
else if(front==-1)
{
System.out.println("Overflow");
}
else
{
arr[--front]=val;
}
}
void popfront()
{
if(front==rear)
{
System.out.println("The Front element removed is : "+arr[front]);
front=0;
rear=0;
}
else if(front==0 && rear==0)
{
System.out.println("Underflow");
}
else
{
System.out.println("The Front element removed is : "+arr[rear]);
rear--;
}
void poprear()
{
if(front==rear)
{
System.out.println("The Rear element removed is : "+arr[front]);
front=0;
rear=0;
}
else if(front==0 && rear==0)
{
System.out.println("Underflow");
}
else
{
System.out.println("The Rear element removed is : "+arr[front]);
front++;
}
}
void display()
{
System.out.print("The elements in Deque are: ");
for(int i=front;i<=rear;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println(" ");
}
public static void main(String args[])throws IOException
{
int flag=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of array");
Dequeue obj=new Dequeue(Integer.parseInt(br.readLine()));
while(flag==0)
{
System.out.println("1 for addrear");
System.out.println("2 for addfront");
System.out.println("3 for poprear");
System.out.println("4 for popfront");
System.out.println("5 for exit");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter element");
obj.addrear(Integer.parseInt(br.readLine()));
obj.display();
break;
case 2:System.out.println("Enter element");
obj.addfront(Integer.parseInt(br.readLine()));
obj.display();
break;
case 3:obj.poprear();
obj.display();
break;
case 4:obj.popfront();
obj.display();
break;
case 5:flag=1;
break;
}
}
}
}
Output:
import java.io.*;
class Node
{
public int data;
public Node next;
public Node previous;
public Node(int x)
{
data=x;
}
public void displayNode()
{
System.out.print(data+" ");
}
}
class DoublyLinkList
{
private Node first;
private Node last;
public DoublyLinkList()
{
first=null;
last=null;
}
public void insertFirst(int x)
{
Node newNode=new Node(x);
newNode.next=null;
if(isEmpty())
last=newNode;
else
first.previous=newNode;
newNode.next=first;
first=newNode;
}
public void insertLast(int x)
{
Node newNode=new Node(x);
newNode.next=null;
if(isEmpty())
first=newNode;
else
{
last.next=newNode;
newNode.previous=last;
}
last=newNode;
}
public int deleteFirst()
{
int t=first.data;
if(first.next==null)
last=null;
else
first.next.previous=null;
first=first.next;
return t;
}
public int deleteLast()
{
int t=last.data;
if(first.next==null)
first=null;
else
last.previous.next=null;
last=last.previous;
return t;
}
public boolean isEmpty()
{
return(first==null);
}
public void displayForward()
{
Node current=first;
while(current!=null)
{
current.displayNode();
current=current.next;
}
}
public void displayBackward()
{
Node current=last;
while(current!=null)
{
current.displayNode();
current=current.previous;
}
}
}
class Deque
{
private DoublyLinkList l;
public Deque()
{
l=new DoublyLinkList();
}
public void insertLeft(int x)
{
l.insertFirst(x);
System.out.print("Inserted to Front ");
}
public void insertRight(int x)
{
l.insertLast(x);
System.out.print("Inserted to Rear ");
}
public int deleteLeft()
{
return l.deleteFirst();
}
public int deleteRight()
{
return l.deleteLast();
}
public boolean isQueueEmpty()
{
return l.isEmpty();
}
public void displayFromFront()
{
l.displayForward();
}
public void displayFromRear()
{
l.displayBackward();
}
}
class DequeApp
{
public static void main(String args[])throws IOException
{
String ch="y";
DataInputStream inp=new DataInputStream(System.in);
int n,d;
Deque q=new Deque();
while(ch.equals("y"))
{
System.out.println("MENU");
System.out.println("--------");
System.out.println("1.Insert at Front");
System.out.println("2.Insert at Rear");
System.out.println("3.Delete at Front");
System.out.println("4.Delete at Rear");
System.out.println("5.Display From Front");
System.out.println("6.Display From Rear");
System.out.println("Enter your choice ");
n=Integer.parseInt(inp.readLine());
switch(n)
{
case 1: System.out.println("Enter the data ");
d=Integer.parseInt(inp.readLine());
q.insertLeft(d);
break;
case 2: System.out.println("Enter the data ");
d=Integer.parseInt(inp.readLine());
q.insertRight(d);
break;
case 3: if(q.isQueueEmpty())
System.out.print("Deque is Empty ");
else
{
d=q.deleteLeft();
System.out.print("Deleted data:- "+d);
}
break;
case 4: if(q.isQueueEmpty())
System.out.print("Deque is Empty ");
else
{
d=q.deleteRight();
System.out.print("Deleted data:- "+d);
}
break;
case 5: if(q.isQueueEmpty())
System.out.print("Deque is Empty ");
else
{
System.out.print("Elements in Deque From Front:- ");
q.displayFromFront();
}
break;
case 6: if(q.isQueueEmpty())
System.out.print("Deque is Empty ");
else
{
System.out.print("Elements in Deque From Rear:- ");
q.displayFromRear();
}
break;
default: System.out.print("Invalid choice ");
}
System.out.println("");
System.out.print("Enter y to continue ");
ch=inp.readLine();
}
}
}
Output:
MENU
--------
1.Insert at Front
2.Insert at Rear
3.Delete at Front
4.Delete at Rear
5.Display From Front
6.Display From Rear
Enter your choice
1
Enter the data
23
Inserted to Front
Enter y to continue y
MENU
--------
1.Insert at Front
2.Insert at Rear
3.Delete at Front
4.Delete at Rear
5.Display From Front
6.Display From Rear
Enter your choice
2
Enter the data
22
Inserted to Rear
Enter y to continue y
MENU
--------
1.Insert at Front
2.Insert at Rear
3.Delete at Front
4.Delete at Rear
5.Display From Front
6.Display From Rear
Enter your choice
1
Enter the data
24
Inserted to Front
Enter y to continue y
MENU
--------
1.Insert at Front
2.Insert at Rear
3.Delete at Front
4.Delete at Rear
5.Display From Front
6.Display From Rear
Enter your choice
5
Elements in Deque From Front:- 24 23 22
Enter y to continue y
MENU
--------
1.Insert at Front
2.Insert at Rear
3.Delete at Front
4.Delete at Rear
5.Display From Front
6.Display From Rear
Enter your choice
4
Deleted data:- 22
Enter y to continue y
MENU
--------
1.Insert at Front
2.Insert at Rear
3.Delete at Front
4.Delete at Rear
5.Display From Front
6.Display From Rear
Enter your choice
6
Elements in Deque From Rear:- 23 24
Enter y to continue n
BUILD SUCCESSFUL (total time: 1 minute 1 second)
Program:
case 3:
if(r==-1) //reached minsize
System.out.println("queue is empty");
else
d.dequeuerear(); //decrement rear
break;
case 4:
if(r==-1) //reached minsize
System.out.println("queue is empty");
else
d.dequeuefront(); //decrement rear
break;
case 5:
System.out.println("queue rear element:"+d.dequerear());
break;
case 6:
System.out.println("queue front element:"+d.dequefront());
break;
case 7:
System.out.println("queue elements are:");
d.display(); //displaying all elements
break;
default: //if incorrect option
System.out.println("enter correct choice");
break;
}
}
}
} //class9
Output:
Output:
import java.util.*;
class BSTNode
{
int data;
BSTNode left;
BSTNode right;
BSTNode( int d ) // constructor
{ data = d; }
}
class BinarySearchTree
{
public BSTNode insertTree(BSTNode p, int key) // create BST
{
if( p == null )
p = new BSTNode(key);
else if( key < p.data)
p.left = insertTree( p.left, key);
else p.right = insertTree( p.right, key);
return p; // return root
}
int arr[],n;
Scanner s=new Scanner(System.in);
System.out.println("Enter array length: ");
n=s.nextInt();
arr=new int[n];
System.out.println("enter "+n+" elements: ");
for(int i=0;i<n;i++)
{
arr[i]=s.nextInt();
}
BinarySearchTree bst = new BinarySearchTree();
BSTNode root = null;
// build tree by repeated insertions
for( int i = 0; i < arr.length; i++ )
root = bst.insertTree( root, arr[i]);
}
}
Output:
return hashVal;
}
{
++hashVal; // go to next cell
hashVal %= size; // wraparound if necessary
}
hashArray[hashVal] = new Entry(theKey, str);
count++; // update count
}
else
System.out.println("Table is full");
}
}
}
Output:
insert put in
help lend a hand
man gentleman
watch observe
format arrangement
run sprint
wish desire
arrange put together
hope expect
count add up
found: wish desire
deleted: hope expect
size: 9
Program:
import java.util.Scanner;
Node(int value){
this.value = value;
left = null;
right = null;
}
}
}
}
Output:
import java.util.Scanner;
Node(int value){
this.value = value;
left = null;
right = null;
}
}
}
}
Output:
Node(int value){
this.value = value;
left = null;
right = null;
}
}
}
}
Output:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
Output:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.Scanner;
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
System.out.println("Enter " + n + " integers");
int arr[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
int n = in.nextInt();
arr = new int[n];
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
Output:
import java.util.Scanner;
class InsertionSort {
/*Function to sort array using insertion sort*/
void sort(int arr[])
{
int n = arr.length;
System.out.println("Enter " + n + " integers");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
int n = in.nextInt();
arr = new int[n];
Output:
import java.util.Scanner;
class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
return i+1;
}
// Driver program
public static void main(String args[])
{
int arr[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
int n = in.nextInt();
arr = new int[n];
System.out.println("Enter " + n + " integers");
for (int c = 0; c < n; c++)
arr[c] = in.nextInt();
System.out.println("sorted array");
printArray(arr);
}
}
Output:
import java.util.Scanner;
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
// Driver method
public static void main(String args[])
{
int arr[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
int n = in.nextInt();
arr = new int[n];
System.out.println("Enter " + n + " integers");
System.out.println("\nSorted array");
printArray(arr);
}
}
Output:
Sorted array
3 14 15 26 28 34 75 89
Program:
import java.util.Scanner;
// Driver program
public static void main(String args[])
{
int arr[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
int n = in.nextInt();
arr = new int[n];
Output:
import java.io.*;
import java.util.*;
class Radix {
radixsort(arr, n);
print(arr, n);
}
}
Output:
import java.util.Scanner;
class BinarytreeSort
{
// Root of BST
Node root;
// Constructor
BinarytreeSort ()
{
root = null;
}
/* A recursive function to
insert a new key in BST */
Node insertRec(Node root, int key)
{
// A function to do
// inorder traversal of BST
void inorderRec(Node root)
{
if (root != null)
{
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
void treeins(int arr[])
{
for(int i = 0; i < arr.length; i++)
{
insert(arr[i]);
}
// Driver Code
public static void main(String[] args)
{
BinarytreeSort tree = new BinarytreeSort ();
int arr[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
int n = in.nextInt();
arr = new int[n];
Output:
import java.io.*;
class Edge
{
int v1,v2,wt; // wt is the weight of the edge
}
class kruskalsalgo
{
public static void main(String args[])throws IOException
{
int i,j,mincost=0;
BufferedReader br=new BufferedReader( new InputStreamReader(System.in));
System.out.println(" Enter no.of vertices:");
int v=Integer.parseInt(br.readLine());
System.out.println(" Enter no.of edges:");
int e=Integer.parseInt(br.readLine());
Edge ed[]=new Edge[e+1];
for(i=1;i<=e;i++)
{
ed[i]=new Edge();
System.out.println(" Enter the vertices and the weight of edge "+(i)+ ":");
ed[i].v1=Integer.parseInt(br.readLine());
ed[i].v2=Integer.parseInt(br.readLine());
ed[i].wt=Integer.parseInt(br.readLine());
}
for(i=1;i<=e;i++) // sorting the edges in ascending order
for(j=1;j<=e-1;j++)
{
if(ed[j].wt>ed[j+1].wt)
{
Edge t=new Edge();
t=ed[j];
ed[j]=ed[j+1];
ed[j+1]=t;
}
}
int visited[]=new int[v+1]; // array to check whether the vertex is visited or not
for(i=1;i<=v;i++)
visited[i]=0;
System.out.println("MINIMUM SPANNING TREE :");
for(i=1;i<=e;i++)
{
if(i>v)
break;
else if( visited[ed[i].v1]==0 || visited[ed[i].v2]==0)
{
System.out.println(ed[i].v1+ "-"+ ed[i].v2);
visited[ed[i].v1]=visited[ed[i].v2]=1;
mincost+=ed[i].wt;
}
}
System.out.println("MINIMUM COST = " +mincost);
}
}
Output: