0% found this document useful (0 votes)
2 views55 pages

Java Excercise

The document contains Java programs for implementing various data structures including stacks, queues, circular queues, binary search trees (BST), and infix to postfix expression conversion. Each program provides methods for basic operations such as push, pop, enqueue, dequeue, insertion, deletion, and traversal. The outputs demonstrate the functionality of these data structures through user interaction and example expressions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views55 pages

Java Excercise

The document contains Java programs for implementing various data structures including stacks, queues, circular queues, binary search trees (BST), and infix to postfix expression conversion. Each program provides methods for basic operations such as push, pop, enqueue, dequeue, insertion, deletion, and traversal. The outputs demonstrate the functionality of these data structures through user interaction and example expressions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program:

import [Link].*;
import [Link].*;
public class Stack_LinkedList
{
private Node head;
public Stack_LinkedList()
{
head = null;
}
public void pop()
{
if (head==null)
{
[Link]("No element is available in Stack to pop\n");
}
else
{
int value=[Link];
head=[Link];
[Link]("\n Top value removed from Stack is ;"+value);
}
}
public void push (int value)
{
Node oldHead=head;
head=new Node();
[Link]=value;
[Link]=oldHead;
}
public static void main(String args[])
{
stack_LinkedList sls=new stack_LinkedList();
while(true)
{
[Link]("\n1....push\n2....pop\n3....DisplayList\n4....Exit");
Scanner myobj=new Scanner([Link]);
[Link]("\n Enter your choice:");
int choice=[Link]([Link]());
Switch(choice)
{
case 1:
[Link]("\n Enter the value to push:");
int val=[Link]([Link]());
[Link](val);
break;
case 2:
[Link]();
break;
case 3:
DisplayList([Link]);
break;
case 4:
[Link](0);
}
}
}
public static void DisplayList(Node head)
{
Node temp=head;
while (temp != null)
{
[Link]("%d", [Link]);
temp=[Link];
}
[Link]();
}
private class Node
{
int data;
Node next;
}
}
OUTPUT:
1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


1

Enter the value to push:


10

1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


1

Enter the value to push:


20

1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


1

Enter the value to push:


30

1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


3
302010

1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


2

Top value removed from Stack is ;30

1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


3
2010

1....push
2....pop
3....DisplayList
4....Exit

Enter your choice:


4
Program:
import [Link].*;
import [Link].*;
public class Queue_LinkedList
{
Node front,rear;
int currsize;
public Queue_LinkedList()
{
front=null;
rear=null;
currsize=0;
}
public boolean isEmpty()
{
return( currsize==0);
}
public int dequeue()
{
if(isEmpty())
{
rear=null;
[Link]("No element available in the Queue");
return-1;
}
else
{
int data=[Link];
front=[Link];
currsize--;
[Link](data+"removed from the queue");
return data;
}
}
void DisplayList()
{
if(isEmpty())
{
[Link]("No element available in the Queue");
}
else
{
Node ptr = front;
while(ptr !=[Link]() )
{
[Link]([Link]()+"");
ptr=[Link]();
}
}
}
public void enqueue(int data)
{
Node oldRear=rear;
rear=new Node();
[Link]=data;
[Link]=null;
if(isEmpty())
{
front=rear;
}
else
{
[Link]=rear;
}
currsize++;
[Link](data+"added to the queue");
}
public static void main(String a[])
{
Queue_LinkedList queue=new Queue_LinkedList();
while(true)
{
[Link]("\n1....Enqueue\n2....Dequeue\n3....DisplayElement\
n4....Exit");
Scanner myObj=new Scanner([Link]);
[Link]("\nEnter your choice:");
int choice = [Link]([Link]() );
switch(choice)
{
case 1:
[Link]("\nEnter the value to insert:");
int val= [Link]([Link]());
[Link](val);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link](0);
}
}
}
private class Node
{
int data;
Node next;
public Node getNext()
{
return next;
}
public int getData()
{
return data;
}
}
}
Output:
1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


1

Enter the value to insert:


10
10added to the queue

1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


1

Enter the value to insert:


20
20 added to the queue
1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


1

Enter the value to insert:


30
30added to the queue

1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


3
10 20 30
1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit
Enter your choice:
3
10 20 30
1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


2
10 removed from the queue

1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


2
20 removed from the queue

1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit
Enter your choice:
2
30removed from the queue

1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


2
No element available in the Queue

1....Enqueue
2....Dequeue
3....DisplayElement
4....Exit

Enter your choice:


4
Program:
package cirqularqueue;
import [Link].*;
public class CircularQueue
{
Note front,rear;
public CircularQueue()
{
front=null;
rear=null;
}
public void enQueue(int value)
{
Node temp=new Node();
[Link]=value;
if(front==null)
front=temp;
else
[Link]=temp;
rear=temp;
[Link]=front;
}
public int deQueue()
{
if(front==null)
{
[Link]("Queue is empty");
return Integer.MIN_VALUE;
}
int value;
if(front==rear)
{
value=[Link];
front=null;
rear=null;
}
else
{
Note temp=font;
value=[Link];
front=[Link];
[Link]=font;
}
return value;
}
public void displayCirQueue()
{
if(front==null)
{
[Link]("Queue is empty");
}
else
{
Node temp=front;
[Link]("\nElements in Circular Queue are:\n");
while([Link]!=front)
{
[Link]([Link]+"");
temp=[Link];
}
[Link]([Link]);
}
}
public static void main(string[]args)
{
CircularQueue queue=new CircularQueue();
while(true)
{
[Link]("\n1.....Enqueue\n2....Dequeue\n3....DisplayElement\
n4....Exit");
Scanner myObj=new Scanner([Link]);
[Link]("\nEnter your choice:");
int choice=[Link]([Link]());
switch(choice)
{
case 1:
[Link]("\nEnter the value to insert:");
int val=[Link]([Link]());
[Link](val);
break;
case 2:
[Link] ();
break;
case 3:
[Link] ();
break;
case 4:
[Link] (0);
}
}
}
public class Node
{
int data;
Note next;
public Note getNext()
{
return next;
}
public int getDate()
{
return data;
}
}
}

OUTPUT:
1....Enqueue
2....Dequeue....DisplayElement
4....Exit

Enter your choice:


1

Enter the value to insert:


10

1....Enqueue
2....Dequeue...DisplayElement
4....Exit

Enter your choice:


1

Enter the value to insert:


20

1....Enqueue
2....Dequeue....DisplayElement
4....Exit

Enter your choice:


3
10
10 20

1....Enqueue
2....Dequeue....DisplayElement
4....Exit

Enter your choice:


2

1....Enqueue
2....Dequeue...DisplayElement
4....Exit

Enter your choice:


2

1....Enqueue
2....Dequeue....DisplayElement
4....Exit

Enter your choice:


2
Queue is empty
1....Enqueue
2....Dequeue....DisplayElement
4....Exit

Enter your choice:


3
Queue is empty
1....Enqueue
2....Dequeue....DisplayElement
4....Exit

Enter your choice:


4

Program:
import [Link];
import [Link];

public class InfixPostfixConversion


{
static int Precedance(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
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<[Link]();++i)
{
char c = [Link] (i);

if ([Link] (c))
result += c;
else if (c=='(')
[Link](c);
else if (c==')')
{
while (![Link]() &&
[Link]() !='(')
result += [Link]();

[Link]();
}
else
{
while (![Link]()&& Precedance(c)<= Precedance ([Link]()))
{
result += [Link]();
}
[Link](c);
}

}
while (![Link]())
{
if([Link]() =='(')
return"Invalid Expression";
result += [Link]();
}
return result;
}
public static void main (String [] args)
{
Scanner obj=new Scanner ([Link]);
[Link]("Enter the infix expression");
String exp = [Link]();
[Link](infixToPostfix (exp));
}
}

Output:
Enter the infix expression
(a+b*c)/d
abc*+d/

Program:
import [Link].*;
import [Link].*;
public class EvaluatePostfix
{
static int evaluateExpression(String expression)
{
Stack<Integer>stack=new Stack<>();
for(int i=0;i<[Link]();i++)
{
char c=[Link](i);
if([Link](c))
[Link](c - 'o');
else
{
int value1=[Link]();
int value2=[Link]();
switch(c)
{
case'+':
[Link](value2+value1);
break;
case'-':
[Link](value2-value1);
break;
case'/':
[Link](value2/value1);
break;
case'*':
[Link](value2*value1);
break;
}
}
}
return [Link]():
}
public static void main(String args[])
{
Scanner obj=new Scanner([Link]);
[Link]("Enter the postfix expression(Operands are integers of width
1):”);
String exp = [Link]();
[Link]("Postfix Evalution Result: "+ evaluateExpression(exp));
}
}

Output:
Enter the postfix expression(Operands are integers of width1):
24*
Postfix Evaluation Result:8
Program:
import [Link].*;
public class BST_Insert
{
public Node root;
BST_Insert()
{
root=null;
}
void insertKey(int key)
{
root=insert(root, key);
}
Node insert(Node root, int key)
{
if ( root==null)
{
root=new Node(key);
return root;
}
if (key<[Link])
{
[Link]=insert([Link],key);
}
if(key>[Link])
{
[Link]=insert([Link],key);
}
return root;
}
void inOrder()
{
inOrderTraverse(root);
}
void inOrderTraverse(Node root)
{
if (root!=null)
{
inOrderTraverse([Link]);
[Link]([Link]);
inOrderTraverse([Link]);
}
}
public static void main(String[] args)
{
BST_Insert bst=new BST_Insert();
[Link](4);
[Link](35);
[Link](25);
[Link](38);
[Link](65);
[Link](55);
[Link](90);
[Link](1);
[Link]();
}
class Node
{
int key;
Node left,right;

public Node(int element)


{
key=element;
left=right=null;
}
}
}

OUTPUT:
1
4
25
35
38
55
65
90

Program:
import [Link].*;
public class BST_Delete
{
public Node root;
BST_Delete()
{
root=null;
}
void deleteKey(int key)
{
root=delete(root, key);
}
Node delete(Node root,int key)
{
if(root==null)
return root;
if(key<[Link])
[Link]=delete([Link],key);
else if(key>[Link])
[Link]=delete([Link],key);
else
{
if([Link]==null)
return [Link];
else if([Link]==null)
return [Link];
[Link] = minimum([Link]);
[Link]=delete([Link],[Link]);
}
return root;
}
int minimum(Node root)
{
int minvalue=[Link];
while([Link]!=null)
{
minvalue=[Link];
root=[Link];
}
return minvalue;
}
void insertKey(int key)
{
root=insert(root,key);
}
Node insert(Node root,int key)
{
if(root==null)
{
root=new Node(key);
return root;
}
if(key<[Link])
{
[Link]=insert([Link],key);
}
if (key>[Link])
{
[Link]=insert([Link],key);
}
return root;
}
void inOrder()
{
inOrderTraverse(root);
}
void inOrderTraverse(Node root)
{
if(root!=null)
{
inOrderTraverse([Link]);
[Link]([Link]);
inOrderTraverse([Link]);
}
}
public static void main(String[]args)
{
BST_Delete bst=new BST_Delete();
bst .insertKey(4);
bst .insertKey(35);
bst .insertKey(25);
bst .insertKey(38);
bst .insertKey(65);
bst .insertKey(55);
bst .insertKey(90);
bst .insertKey(1);
bst .inOrder();
[Link]("Tree after Deleting key 65");
[Link](65);
[Link]();
}
class Node
{
int key;
Node left,right;

public Node(int element)


{
key=element;
left=right=null;
}
}
}
Output:
1
4
25
35
38
55
65
90
Tree after deleting key 65
1
4
25
35
38
55
90

Program:
import [Link].*;
public class BST_Search
{
public Node root;
BST_Search()
{
root=null;
}
void insertKey(int key)
{
root=insert(root,key);
}
Node insert(Node root,int key)
{
if(root==null)
{
root=new Node(key);
return root;
}
if(key<[Link])
{
[Link]=insert([Link],key);
}
if(key>[Link])
{
[Link]=insert([Link],key);
}
return root;
}
void inOrder()
{
inOrderTraverse(root);
}
void inOrderTraverse(Node root)
{
if(root!=null)
{
inOrderTraverse([Link]);
[Link]([Link]);
inOrderTraverse([Link]);
}
}
public int searchKey(int key)
{
Node result=null;
result=search(root,key);
if(result==null)return-1;
else return [Link];
}
public Node search(Node root,int key)
{
if(root==null||[Link]==key)
return root;
if([Link]<key)
return search([Link],key);
return search([Link],key);
}
public static void main(String args[])
{
BST_Search bst=new BST_Search();
[Link](4);
[Link](35);
[Link](25);
[Link](38);
[Link](65);
[Link](55);
[Link](90);
[Link](1);
[Link]("Searching the key value 65 in BST(If not found return-1)");
[Link]([Link](65));
[Link]("Searching the key value 66 in BST(If not found return-1)");
[Link]([Link](66));
}
class Node
{
int key;
Node left,right;
public Node(int element)
{
key=element;
left=right=null;
}
}
}

Output:
1
4
25
35
38
55
65
90
Searching the key value 65 in BST(If not found return-1)
65
Searching the key value 66 in BST(If not found return-1)
-1

Program:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
public class BFSgraph
{
private int v;.[Link](
private Linkedlist<integer>=adj[];
BFS(int v)
{
V=v;
adj[]=new LinkedList[v];
For(int i=0;i<n;++i);
adj[i]=new LinkedList();
}
void [Link]=(int v,int w);
{
adj (v).add(w);
}
void BFS[s];
{
boolean visited[]=new boolean[v]
Linkedlist<integer>Queue=new Linkedlist adj[];
visited[s]+true;
Queue,add;
while([Link])
s=[Link]();
[Link](v+" ");
Iterator<integer>i=adj[v]new List Iterator;
while(i has next)
n=[Link];
if(!visited n= true)
void BFS ;
public static void main (String arg[]);
{
BFS graph=new BFS graph
}
[Link](0,1);
[Link](0,2);
[Link](1,2);
[Link](1,3);
[Link](2,3);
[Link](3,3);
[Link](:Following Breadth First Traversal");
[Link](4);
}

Output:
program to create a graph and traverse in BFS manner
Enter number of nodes
4
Enter the edges with vertex no. starting from 1
enter the vertices v1&v2:
1
2
Want to add more edges(y/n)?
y
enter the vertices v1&v2:
1
3
Want to add more edges(y/n)?
y
enter the vertices v1&v2:
1
4
Want to add more edges(y/n)?
y
enter the vertices v1&v2:
2
3
Want to add more edges(y/n)?
n
Adjacency Matrix for the graph is
0111
1010
1100
1000
Breadth first traversal for the above graph is
1
2
3
4

Program:
import [Link];
public class DFS_Graph
{
final int TRUE=1;
final int FALSE=0;
final int MAX=20;
int [] [] G=new int [MAX] [MAX];
int [] visit=new int [MAX];
int [] q=new int [MAX];
int n,v1,v2;
int front,rear;

DFS_Graph()
{
[Link]("Enter number of nodes");
Scanner obj=new Scanner([Link]);
n=[Link]([Link]());
for(int i=1; i<=n; i++)
visit[i]=FALSE;
for(v1=1; v1<=n; v1++)
{
for(v2=1; v2<=n; v2++)
G[v1][v2]=FALSE;
}
front=rear=-1;
}
void createGraph()
{
char ans;
[Link]("Enter the edges with vertex [Link] from 1");

do
{
[Link]("Enter the vertices v1 & v2:");
Scanner obj=new Scanner([Link]);
v1=[Link]();
v2=[Link]();
if(v1>n||v2>n)
[Link]("Invalid Vertex Number");
else
{
G[v1][v2]=TRUE;
G[v2][v1]=TRUE;
}
[Link]("want to add more edges(y/n)?");
ans=[Link]().charAt(0);
}
while((ans=='y')||(ans=='y'));
}
void matrix()
{
[Link]("Adjacency Matrix for the graph is");
for(int v1=1; v1<=n; v1++)
{
for(int v2=1; v2<=n; v2++)
[Link](G[v1][v2]+"");
[Link]();
}
}
void dfs(int v)
{
int i;
[Link](v+"");
visit[v]=TRUE;
for(i=1; i<=n; i++)
{
if(G[v][i]==TRUE&&visit[i]==FALSE)
dfs(i);
}
}
public static void main(String[] args)
{
[Link]("Program to create a Graph and traverse in DFS manner");
DFS_Graph g=new DFS_Graph();
[Link]();
[Link]();
[Link]("Depth first traversal for the above graph is");
[Link](1);
}
}
Output:
Program to create a Graph and traverse in DFS manner
Enter number of nodes
4
Enter the edges with vertex [Link] from 1
Enter the vertices v1 & v2:
1
2
want to add more edges(y/n)?
y
Enter the vertices v1 & v2:
1
4
want to add more edges(y/n)?
y
Enter the vertices v1 & v2:
3
4
want to add more edges(y/n)?
n
Adjacency Matrix for the graph is
0101
1000
0001
1010
Depth first traversal for the above graph is
1
2
4
3

You might also like