In Java Write a method stutter that accepts a Stack of integers as a parameter a
ID: 3575970 • Letter: I
Question
In Java
Write a method stutter that accepts a Stack of integers as a parameter and replaces every value in the stack with two occurrences of that value. For example, suppose a stack stores these values:
Then the stack should store the following values after the method terminates:
Notice that you must preserve the original order. In the original stack the 9 was at the top and would have been popped first. In the new stack the two 9s would be the first values popped from the stack. Also, you must not use any auxillary data structures to solve this problem. If the original stack is empty, the result should be empty as well.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class A
{
void stutter(int a[],int b[])
{
int top1=5,top2=5*2;
if((top1>0)&&(top2>0))
{
for(int i=5;i>0;i--)
{
System.out.println(" array1 top most element is poped"+a[top1]);
top1--;
for(int j=2;j>0;j--)
{
System.out.println(" array2 top most element is poped"+b[top2]);
top2--;
}
}
}
System.out.println(" stack a and stac b are empty");
}
public static void main (String[] args) throws java.lang.Exception
{
A stac=new A();
int arry1[]=new int[6];
int arry2[]=new int[11];
arry1[1]=3;
arry1[2]=7;
arry1[3]=1;
arry1[4]=14;
arry1[5]=9;
arry2[1]=3;
arry2[2]=3;
arry2[3]=7;
arry2[4]=7;
arry2[5]=1;
arry2[6]=1;
arry2[7]=14;
arry2[8]=14;
arry2[9]=9;
arry2[10]=9;
stac.stutter(arry1,arry2);
}
}
out put:
array1 top most element is poped9
array2 top most element is poped9
array2 top most element is poped9
array1 top most element is poped14
array2 top most element is poped14
array2 top most element is poped14
array1 top most element is poped1
array2 top most element is poped1
array2 top most element is poped1
array1 top most element is poped7
array2 top most element is poped7
array2 top most element is poped7
array1 top most element is poped3
array2 top most element is poped3
array2 top most element is poped3
stack a and stac b are empty
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.