Write A Java Program For Performing Various Operations On Queue Using Linked List
Write A Java Program For Performing Various Operations On Queue Using Linked List
Write A Java Program For Performing Various Operations On Queue Using Linked List
Program:
package inter;
import java.util.*;
class Node{
protected int data;
protected Node link;
public Node(){
link = null;
data = 0;
}
public Node(int d,Node n){
data = d;
link = n;
}
public void setLink(Node n){
link = n;
}
public void setData(int d){
data = d;
}
public Node getLink(){
return link;
}
public int getData() {
return data;
}
}
class linkedQueue{
protected Node front, rear;
public int size;
public linkedQueue(){
front = null;
rear = null;
size = 0;
}
case 5 :
System.out.println("Size = "+ lq.getSize());
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
lq.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:
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
1
Enter integer element to insert
855
Queue = 5 855
7. AIM: Write a java program for the following using stack
Program:
a) package adslab3;
import java.io.*;
class stack
{
char stack1[]=new char[20];
int top;
void push(char ch){
top++;
stack1[top]=ch;
}
char pop(){
char ch;
ch=stack1[top];
top--;
return ch;
}
int pre(char ch){
switch(ch){
case '-':return 1;
case '+':return 1;
case '*':return 2;
case '/':return 2;
}
return 0;
}
boolean operator(char ch){
if(ch=='/'||ch=='*'||ch=='+'||ch=='-')
return true;
else
return false;
}
boolean isAlpha(char ch){
if(ch>='a'&&ch<='z'||ch>='0'&&ch=='9')
return true;
else
return false;
}
void postfix(String str){
char output[]=new char[str.length()];
char ch;
int p=0,i;
for(i=0;i<str.length();i++){
ch=str.charAt(i);
if(ch=='('){
push(ch);
}
else if(isAlpha(ch)){
output[p++]=ch;
}
else if(operator(ch)){
if(stack1[top]==0||(pre(ch)>pre(stack1[top]))||stack1[top]=='(')
{
push(ch);
}
}
else if(pre(ch)<=pre(stack1[top])){
output[p++]=pop();
push(ch);
}
else if(ch=='('){
while((ch=pop())!='(')
{
output[p++]=ch;
}
}
}
while(top!=0){
output[p++]=pop();
}
for(int j=0;j<str.length();j++){
System.out.print(output[j]);
}
}
}
class InfixToPost
{
public static void main(String[] args)throws Exception
{
String s;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
stack b=new stack();
System.out.println("Enter input string");
s=br.readLine();
System.out.println("Input String:"+s);
System.out.println("Output String:");
b.postfix(s);
}
}
Output: Enter input string
a+b*c
Input String:a+b*c
Output String:
abc*+
b) package adslab3;
import java.util.*;
public class EvaluateExpressionUsingStacks
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Stack<Integer> op = new Stack<Integer>();
Stack<Double> val = new Stack<Double>();
Stack<Integer> optmp = new Stack<Integer>();
Stack<Double> valtmp = new Stack<Double>();
/* Accept expression */
System.out.println("Evaluation Of Arithmetic Expression Using Stacks Test\n");
System.out.println("Enter expression\n");
String input = scan.next();
input = "0" + input;
input = input.replaceAll("-","+-");
String temp = "";
try {
for (int i = 0;i < input.length();i++)
{
char ch = input.charAt(i);
if (ch == '-')
temp = "-" + temp;
else if (ch != '+' && ch != '*' && ch != '/')
temp = temp + ch;
else
{
val.push(Double.parseDouble(temp));
op.push((int)ch);
temp = "";
}
}
val.push(Double.parseDouble(temp));
char operators[] = {'/','*','+'};
for (int i = 0; i < 3; i++) {
boolean it = false;
while (!op.isEmpty()){
int optr = op.pop();
double v1 = val.pop();
double v2 = val.pop();
if (optr == operators[i]){
if (i == 0) {
valtmp.push(v2 / v1);
it = true;
break;
}
else if (i == 1){
valtmp.push(v2 * v1);
it = true;
break;
}
else if (i == 2){
valtmp.push(v2 + v1);
it = true;
break;
}
}
else{
valtmp.push(v1);
val.push(v2);
optmp.push(optr);
}
}
while (!valtmp.isEmpty())
val.push(valtmp.pop());
while (!optmp.isEmpty())
op.push(optmp.pop());
if (it)
i--;
}
System.out.println("\nResult = "+val.pop());
}
catch (Exception e) {
System.out.println("Invalid Input");
return;
}
}
}
Output: Evaluation Of Arithmetic Expression Using Stacks Test
Enter expression
3+7
Result = 10.0
c) package adslab3;
import java.util.*;
Output:
Binary Search Tree Operations
1. insert
2. delete
3. search
4. count nodes
5. check empty
1
Enter integer element to insert
5
Post order : 5
Pre order : 5
In order : 5
Do you want to continue (Type y or n)
1. insert
2. delete
3. search
4. count nodes
5. check empty
1
Enter integer element to insert
65
Post order : 65 5
Pre order : 5 65
In order : 5 65
9.AIM: Write a java program to implement the following for a graph.
a) BFS b) DFS
Program:
a) package adslab3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
b) package adslab3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
output:
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class mergeheap {
static void merge(int[] array,int low,int mid,int high){
int i,j,k;
int[] c= new int[high-low+1];
k = 0;
i=low;
j=mid+1;
while(i<=mid && j<=high){
if(array[i]<=array[j]){
c[k++] = array[i++];
}
else{
c[k++] = array[j++];
}
}
while(i<=mid){
c[k++] = array[i++];
}
while(j<=high){
c[k++] = array[j++];
}
k=0;
for(i = low; i<=high; i++){
array[i] = c[k++];
}
}
// Function implementing the merge sort
static void mergeSort(int[] array,int low, int high){
if(high-low+1>1){
int mid = (low+high)/2;
mergeSort(array,low,mid);
mergeSort(array,mid+1,high);
merge(array,low,mid,high);
}
}
public static void Heapsort(int arr[])
{
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
Output:
Enter the size of the array
3
Enter array elements
25
4
154
select choice 1.merge 2.heap
1
The initial array is
[25, 4, 154]
The sorted array is
[4, 25, 154]