0% found this document useful (0 votes)
21 views

Programs Java 1

The document contains code for implementing a queue data structure in Java. It defines a Queue class with methods to add elements to the rear (enque), remove elements from the front (deque), and a constructor to initialize the queue capacity and variables. The main method takes user input for size and values, creates a queue object, adds the values using enque and prints the queue after removing one element with deque.

Uploaded by

Savitha Dhilip
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)
21 views

Programs Java 1

The document contains code for implementing a queue data structure in Java. It defines a Queue class with methods to add elements to the rear (enque), remove elements from the front (deque), and a constructor to initialize the queue capacity and variables. The main method takes user input for size and values, creates a queue object, adds the values using enque and prints the queue after removing one element with deque.

Uploaded by

Savitha Dhilip
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
You are on page 1/ 9

// Merge

import java.util.*;
class Merge_
{
public static Scanner sc=new Scanner(System.in);
public static void main(String args[])
{
int i,k=0,l=0,z=0;
System.out.println("Enter size of 1st array");
int n1=sc.nextInt();
int a[]=new int[n1];
System.out.println("Enter 1st array");
for(i=0;i<n1;i++)
a[i]=sc.nextInt();

System.out.println("Enter size of 2nd array");


int n2=sc.nextInt();
int b[]=new int[n2];
System.out.println("Enter 2nd array");
for(i=0;i<n2;i++)
b[i]=sc.nextInt();

int c[]=new int[n1+n2]; Output:

while(k<n1 && l<n2) Enter size of 1st array


{ 5
if(a[k]<b[l]) Enter 1st array
c[z++]=a[k++]; 12546
else Enter size of 2nd array
c[z++]=b[l++]; 3
} Enter 2nd array
if(k<n1) 164
while(k<n1 && l<n2) Merged array :
c[z++]=a[k++]; 1125464
else
while(l<n2)
c[z++]=b[l++];

System.out.println("Merged array :");

for(i=0;i<z;i++)
System.out.print(c[i]+" ");
}
}
// Pendulum Sort

import java.util.*;
class PendulumSort_
{
public static Scanner sc=new Scanner(System.in);
public static void main(String args[])
{
int a[],n,b[],i,j,k,lp=0,cnt;
System.out.println("Enter n"); n=sc.nextInt();
a=new int[n]; b =new int[n];
if(n%2==0)
cnt=(n/2)-1;
else
cnt=n/2;
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=0;i<n;i++)
{
if(n%2==0)
cnt=(n/2)-1; Output:
else
cnt=n/2; Enter n
} 6
for(j=0;j<n;j++) 456487
{ Pendulum Sort :
lp=0; 468754
for(k=0;k<n;k++)
{
if(a[lp]!=0)
if(a[lp]<a[k])
lp=k;
}
if(j%2==0)
{
b[cnt-j]=a[lp];
a[lp]=-1; cnt-=j;
}
else
{
b[cnt+j]=a[lp];
cnt+=j;
a[lp]=-1;
}}
System.out.println(" Pendulum Sort :");
for(j=0;j<n;j++)
System.out.print(b[j]+" ");
}}
// Words Mix

import java.util.*;
class mix
{
public static Scanner sc=new Scanner(System.in);
String w,fw;
int len;
mix()
{
w="";fw="";
len=0;
}

void feedword()
{
System.out.println(" Enter the word ");
w=sc.next();
len=w.length();
}

void mixWord(mix P,mix Q)


{
int i,j,small,pos=0;
char c;
if(P.len<Q.len)
{
small=P.len;
c='p';
}
else
{
small=Q.len;
c='q';
}

for(i=0;i<small;i++)
{
fw+=(P.w).charAt(i);
fw+=(Q.w).charAt(i);
pos=i;
}
if(c=='p')
fw+=(Q.w).substring(pos+1);
else if(c=='q')
fw+=(P.w).substring(pos+1);
}
void display()
{
System.out.println(" MIXED WORD : \n "+fw);
}

public static void main(String args[])


{
mix P=new mix();
P.feedword();
mix Q=new mix();
Q.feedword();

mix v=new mix();


v.mixWord(P,Q);
v.display();
}
}

Output:

Enter the word


jump
Enter the word
stroll
MIXED WORD :
jsutmrpoll
// Diagonal

importjava.util.*;
class arrayDiag
{
public static Scanner sc=new Scanner(System.in);
public static void main(String args[])
{
System.out.println("Enter n");
int n=sc.nextInt();
int a[][]=new int[n][n];
inti,j,l=n-1;
System.out.println("Enter array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Diagonal Print:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Output:
if(i==j || (i+j)==n-1)
System.out.print(a[i][j]);
Enter n
else
4
System.out.print(" ");
Enter array
}
1234
System.out.println();
1234
}
1234
System.out.println();
1234
}
Diagonal Print:
}
1 4
23
23
1 4
// Insertion Sort

import java.util.*;
class InsertionSort
{
public static Scanner sc=new Scanner(System.in);
public static void main(String args[])
{
int n,i,j,key;
System.out.println(" Enter N");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter array");
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=1;i<n;i++) Output:
{
key=a[i]; Enter N
j=i-1;
7
while(j>-1 && a[j]>key)
{
Enter array
a[j+1]=a[j];
j--; 5679842
}
a[j+1]=key; Sorted array :
}
System.out.println("Sorted array :"); 2456789
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
// Stack

import java.util.*;
class stack
{
int n,top;
int stack[];
stack(int m)
{
n=m;
top=-1;
stack=new int[n];
}
void push(int val)
{
if(top==(n-1))
System.out.println("Stack Overflow");
else
stack[++top]=val;
}
int pop()
{
if(top<0)
{
System.out.println("Stsck Underflow");
return -99;
}
else
{
int value=stack[top];
--top;
return value;
}
}
void peek()
{
int i;
for(i=0;i<=top;i++)
System.out.print(stack[i]+" ");
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int v,i;
System.out.println("Input number of elements");
int n=sc.nextInt();

stack s=new stack(n);


System.out.println("Value");
for(i=0;i<n;i++)
{
v=sc.nextInt();
s.push(v);
}
System.out.println("Array:");
s.peek();
System.out.println();
System.out.println("Value that is deleted = "+ s.pop());
System.out.println("Array after delete:");
s.peek();
}
}

Output:

Input number of elements


5
Value
45762
Array:
45762
Value that is deleted = 2
Array after delete:
4576
// Queue

import java.util.*;
class Queue
{
public static Scanner sc=new Scanner(System.in);
int a[],rear,front,cap,n;
Queue(int num)
{
n=num;
cap=n;
a=new int[n];
front=0;
rear=0;
}
void enque(int value)
{
if(rear>=cap)
System.out.println("Overflow");
else
a[rear++]=value;
}
int deque()
{
if(rear==front) Output:
{
System.out.print("Underflow"); Enter n
return -99; 5
} Enter values
else 56735
{ Array after deque:
int v=a[front]; 5
front++;
return v;
}}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter n");
int nm=sc.nextInt();
Queue b=new Queue(nm);
System.out.println("Enter values");
for(int i=0;i<nm;i++)
b.enque(sc.nextInt());
System.out.println("Array after deque:");
System.out.println(b.deque());
}}

You might also like