Documente Academic
Documente Profesional
Documente Cultură
Member functions :
Dequeue(int L) : constructor to initialize the lim=L, front=0 and
rear=0.
Algorithm:
void display()
{
if(rear==-1 || front>rear)
System.out.println("UNDERFLOW DEQUEUE");//prints if the
dequeue is empty or not
else
{
//dequeue is printed
System.out.println("The elements in the dequeue are as:");
for(int j=front;j<=rear;j++)
System.out.println(arr[j]+"\t");
}
}
public static void main(String args[])
{
Dequeue obj=new Dequeue(5);
//the required member methods are called accordingly
obj.display();
obj.addrear(12);
obj.addrear(56);
obj.addrear(7);
obj.display();
obj.addfront(74);
obj.display();
int x=obj.popfront();
obj.display();
int r=obj.poprear();
obj.display();
}
}
Variable description:
Output:
Question 2:
Write a program to count the number of nodes present in an existing linked list.
Algorithm:
1. Create a class.
2. Assign initial values to instant variables using a constructor.
3. Declare a function to create a linked list.
4. The elements of the linked list are taken as input.
5. Declare a function to count the number of nodes.
6. The nodes are counted by using a while loop until the list comes to null.
7. Declare the display function to display the linked list.
8. Create the main function and call the member methods accordingly.
9. End the class.
import java.util.*;
class node2
{
int data;
node2 link;
node2()
{
data=0;
link=null;
}
void create()
{
//creating linked list structure
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the first data");
this.data=in.nextInt();
System.out.println("Enter number of nodes to be created");
n=in.nextInt();
node2 temp;
node2 ptr=this;
for(int i=1;i<n;i++)
{
temp=new node2();
System.out.println("Enter next data");
temp.data=in.nextInt();
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}
void countnodes(node2 start)
{
int count=0;
node2 temp=new node2();
temp=start;
while(temp!=null)
{
count++;
temp=temp.link;
}
System.out.println("Total number of nodes in linked list are = "+count);
}
void display()
{
node2 ptr=this;
while(ptr!=null)
{
System.out.println(ptr.data);
ptr=ptr.link;
}
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
node2 first=new node2();
first.create();
System.out.println("The linked list is");
first.display();
first.countnodes(first);
}
}
Variable description:
Output:
Question 3:
Write a program to insert elements at the beginning of a linked list.
Algorithm:
1. Create a class.
2. Assign initial values to instant variables using a constructor.
3. Declare a function to create a linked list.
4. The elements of the linked list are taken as input.
5. Declare a function to insert elements at the beginning of linked list.
6. The element is inserted with the procedure.
7. Declare the display function to display the linked list.
8. Create the main function and call the member methods accordingly.
9. End the class.
import java.util.*;
class node
{
int data;
node link;
node()
{
data=0;
link=null;
}
void create()
{
//creating linked list structure
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the first data");//first data is inputted
this.data=in.nextInt();
System.out.println("Enter number of nodes to be created");
n=in.nextInt();
node temp;
node ptr=this;
for(int i=1;i<n;i++)
{
temp=new node();
System.out.println("Enter next data");
temp.data=in.nextInt();
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}
void insertbeg(node start,int x)
{
//inserting a list in the beginning
node temp=new node();//allocate memory to new node to object temp
temp=start;
node NewNode,num;//objects NewNode and num are declaredfor the
class node
NewNode=new node();//allocate memory to the object NewNode
NewNode.data=x;//assign x to variable data through NewNode
NewNode.link=temp;//link object temp containing linked list to
the next of object NewNode
temp=NewNode;//establish address link of NewNode to temp
start=temp;//refresh list in start
display(start);
}
void display()
{
//displaying the linked list
System.out.println("The linked list is:");
node NewNode=this;
while(NewNode!=null)
{
System.out.println(NewNode.data);
NewNode=NewNode.link;
}
}
public static void main(String args[])
{
int k;
Scanner in=new Scanner(System.in);
node first=new node();
first.create();
System.out.println("Enter data for new node");
k=in.nextInt();
first.insertbeg(first,k);
}
}
Variable description:
Output:
Question 4:
Write a program to delete elements at the given node number of a linked list.
Algorithm:
1. Create a class.
2. Assign initial values to instant variables using a constructor.
3. Declare a function to create a linked list.
4. The elements of the linked list are taken as input.
5. Declare a function to delete elements at the given node number of
linked list.
6. The element is deleted with the procedure.
7. Declare the display function to display the linked list.
8. Create the main function and call the member methods accordingly.
9. End the class.
import java.util.*;
class node1
{
int data;
node1 link;
node1()
{
data=0;
link=null;
}
void create()
{
//creating linked list structure
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the first data");
this.data=in.nextInt();
System.out.println("Enter number of nodes to be created");
n=in.nextInt();
node1 temp;
node1 ptr=this;
for(int i=1;i<n;i++)
{
temp=new node1();
System.out.println("Enter next data");
temp.data=in.nextInt();
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}
void delete(node1 start,int m)
{
//deleting a list from linked list structure
node1 ptr=start;
node1 ptr1=ptr;
int c=0;
while(c<=m)
{
ptr1=ptr;
ptr=ptr.link;
c++;
}
ptr1.link=ptr.link;
ptr.link=null;
ptr=null;
ptr1=null;
}
void display()
{
//displaying the linked list
System.out.println("The linked list is");
node1 ptr=this;
while(ptr!=null)
{
System.out.println(ptr.data);
ptr=ptr.link;
}
}
public static void main(String args[])
{
int k;
Scanner in=new Scanner(System.in);
node1 first=new node1();
first.create();
System.out.println("Enter node number to be deleted");
k=in.nextInt();
first.delete(first,k);
first.display();
}
}
Variable description:
Output:
Question 5:
1. Create a class.
2. Declare the main function.
3. Take the number of rows and columns as input.
4. The matrix is declared.
5. The elements of the matrix are taken input.
6. The inputted matrix is printed
7. The mirror image of the matrix is printed by running the inner loop
in the opposite direction from that of time of input.
8. End the function.
9. End the class.
//matrix mirror image
import java.util.*;
public class mirror
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n ,i,j;
System.out.println("Enter number of rows");//number of rows are inputted
m=in.nextInt();
System.out.println("Enter number of columns");//number of
columns are inputted
n=in.nextInt();
int arr[][]=new int [m][n];//matrix is declared
//the array elements are inputted
System.out.println("Enter matrix elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
arr[i][j]=in.nextInt();
}
//inputted array is printed
System.out.println("The inputted matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println("The mirror image of the matrix is");
//mirror image of the matrix is found and printed
for(i=0;i<m;i++)
{
for(j=n-1;j>=0;j--)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Variable desceiption:
Write a program in java to check whether two words are anagrams or not.
Algorithm:
1. Create a class.
2. Declare the main function.
3. Take two words as input.
4. Convert them to uppercase and find their lengths.
5. Arrange the letters of each word in alphabetical order using two for loops.
6. Also find the sum of ASCII values of each of the words.
7. If the words are same after arranging them in alphabetical order
and the sum of ASCII values of the two words are also equal, they are
anagrams else not.
8. End the function.
9. End the class.
import java.util.*;
public class anagram
{
public static void main(String args[])
{
int p1,p2,i,j,as1,as2,s1=0,s2=0;
String str1,str2,str3="",str4="";
char chr1,chr2;
Scanner in=new Scanner(System.in);
System.out.println("Enter first word");//first word is inputted
str1=in.next();
str1=str1.toUpperCase();//inputted word is converted to uppercase
System.out.println("Enter second word");//second word is inputted
str2=in.next();
str2=str2.toUpperCase();//inputted word is converted to uppercase
p1=str1.length();//length of first word is found out
p2=str2.length();//length of second word is found out
if(p1==p2)
{
for(i=65;i<=90;i++)
{
for(j=0;j<p2;j++)
{
//characters are extracted from each word
chr1=str1.charAt(j);
chr2=str2.charAt(j);
as1=(int)chr1;
as2=(int)chr2;
//the letters of each word are arranged in
alphabetical order and the sum of the ASCII values of each word is
found out
if(as1==i)
{
str3=str3+chr1;
s1=s1+as1;
}
if(as2==i)
{
str4=str4+chr2;
s2=s2+as2;
}
}
}
if(str3.equals(str4)&&(s1==s2))//required conditions are checked
System.out.println(str1+" & "+str2+" are Anagram words");
else
System.out.println(str1+" & "+str2+" are not Anagram words");
}
else
System.out.println("Wrong input");
}
}
Variable description:
Output:
For anagram words:
Write a program in java to enter a word in mixed case and display the
new token after deleting all the vowels. Finally arrange the token in
alphabetical order.
Algorithm:
1. Create a class.
2. Declare the main function.
3. Take the word as input.
4. Each character of the word is extracted and added to a new string
if it is not a vowel.
5. The new string is printed.
6. The new string is alphabetically arranged by using two for loops.
7. The alphabetically arranged string is printed.
8. End the function.
9. End the class.
import java.util.*;
public class dvowels
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,p,q;
char ch,ch1;
String st,st2="",st3="";
System.out.println("Enter a word");//word is taken as input
st=in.next();
p=st.length();
for(i=0;i<p;i++)
{
ch=st.charAt(i);//each character is extracted
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
continue;
st2=st2+ch;//letters except the vowels are added to the new string
}
System.out.println("The new token formed after deleting vowels "+st2);
q=st2.length();
//the new string is arranged alphabetically
for(i=65;i<=90;i++)
{
for(j=0;j<q;j++)
{
ch1=st2.charAt(j);
if(ch1==(char)i||ch1==(char)(i+32))
st3=st3+ch1;
}
}
System.out.println("The new token after arranging in
alphabetical order: "+st3);
}
}
Variable description:
Output:
Question 8:
Write a program in java to take a sentence as input and print the word
having maximum number of vowels.
Algorithm:
1. Create a class.
2. Declare the main function.
3. Take a sentence as input.
4. Store the number of characters in the sentence in a variable.
5. Run a for loop from 0 to length-1 and obtain each word of the
sentence using : word=word+ch
6. Check the number of vowels in each word and store the word having
maximum number of vowels by comparing.
7. Print the word having maximum number of vowels.
8. End the function.
9. End the class.
import java.util.*;
public class maxvowel
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String st;int l,l1,i,j,c=0,max=0;String maxword="",word="";char ch,ch1;
System.out.println("Enter a sentence");// sentence is inputted
st=in.next();
st=st+" ";
l=st.length();//length of sentence is found out
for(i=0;i<l;i++)
{
ch=st.charAt(i);
if(ch==' ')
{
l1=word.length();
for(j=0;j<l1;j++)
{
ch1=word.charAt(j);//characters of sentence are extracted
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||ch1=='a'||ch1=='e'||ch1=='i'||ch1
=='o'||ch1=='u')
c++;//number of vowels are counted
}
if(c>max)
{
max=c;//maximum number of vowels is checked
maxword=word;//storing the word having maximum number of vowels
}
word="";
c=0;
}
else
word=word+ch;//each word of sentence is obtained
}
System.out.println("Word containing maximum number of vowels
is: "+maxword);
}
}
Variable description:
1. Create a class.
2. Declare the main function.
3. Take a number as input.
4. Using two for loops, find the sum of consecutive numbers till it is
equal to the inputted number or exceeds it.
5. If the condition is satisfied, print that it is a sum of
consecutive numbers else not.
6. End the function.
7. End the class.
import java.util.*;
public class consequtive
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,n,s=0,f=0;int a=0,b=0;
System.out.println("Enter a number to check");//number is inputted
n=in.nextInt();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
s=s+j;//sum of consecutive numbers is found out
if(s==n)
{
f=1;
a=i;
b=j;
break;
}
}
}
if(f==1)
{
//sum of consecutive numbers is found out
System.out.println("The number "+n+" is sum of consecutive numbers:");
while(a<=b)
{
System.out.println(a);//the consecutive numbers are printed
a++;
}
}
else
System.out.println("The number "+n+" is not sum of consecutive
numbers");
}
}
Variable description:
Output:
Question 10:
Algorithm:
1. Create a class.
2. Declare the main function.
3. Enter the number of rows and columns.
4. Take the matrix as input.
5. Print the original matrix.
6. A while loop is used to go through each element of the matrix.
7. Using four for loops, we shift the boundary elements by one space
and then due to the while loop, the control enters the inner part of
matrix and shifts the inner elements by one space.
8. Print the shifted matrix.
9. End the function.
10. End the class.
import java.util.*;
class boundary
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int row=0,col=0,m,n,prev,curr,R,C,i,j;
System.out.println("Enter row and column");
m=in.nextInt();
n=in.nextInt();
R=m;
C=n;
int mat[][]=new int[m][n];
System.out.println("Enter matrix elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
mat[i][j]=in.nextInt();
}
System.out.println("Original matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
}
while(row<m&&col<n)
{
if(row+1==m||col+1==n)
break;
//store the first element of next row and this element
will replace the first element of current row
prev=mat[row+1][col];
//move elements of first row from the remaining rows
for(i=col;i<n;i++)
{
curr=mat[row][i];
mat[row][i]=prev;
prev=curr;
}
row++;
//move elements of last column from the remaining columns
for(i=row;i<m;i++)
{
curr=mat[i][n-1];
mat[i][n-1]=prev;
prev=curr;
}
n--;
//move elements of last row from the remaining rows
if(row<m)
{
for(i=n-1;i>=col;i--)
{
curr=mat[m-1][i];
mat[m-1][i]=prev;
prev=curr;
}
}
m--;
//move elements of first column from the remaining rows
if(col<n)
{
for(i=m-1;i>=row;i--)
{
curr=mat[i][col];
mat[i][col]=prev;
prev=curr;
}
}
col++;
}
System.out.println("Shifted matrix is");//print new matrix
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
}
}
}
Variable description:
Output: