(java)
Write an algorithm that displays all the characters in a stack in the order in which they were pushed onto the stack. After all the characters are displayed, the stack should have the same contents as when you started. In your method, you can only use the stack’s operation, and NO explicit index can be declared to locate the items in the stack. Also, write the main() function to test your algorithm.
Hi,
Please refer screenshots for more clarity.
public static Stack printInOrder(Stack<Character>st): Recursive function
Note:
Please refer recursive calls concept.
Java Code:
//Program starts here
import java.io.*;
import java.util.*;
import java.util.Scanner;
class Main {
public static Stack printInOrder(Stack<Character>st)
{
if(st.empty() == false)//when stack not
empty
{
Character ch = st.pop();//popping the
character
printInOrder(st);//calling recursively
System.out.println(ch);//Printing the
characters
st.push(ch);//Pushing the element
}
return st;//At the end returning the stack(After pushing
the all characters)
}
public static void main(String[] args)
{
System.out.println("Enter number of characters to be pushed in
stack:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stack<Character> st = new
Stack<Character>();//Stack declaration
for(int i = 0; i < n; i++)
{
char ch = sc.next().charAt(0);//Reading each
character
st.push(ch);
}
System.out.print("Printing the stack values in pushed
order: \n");
st = printInOrder(st);//Calling function
System.out.println("Stack top to bottom: \n");
//Checking the order(Validation purpose)
while(st.empty() == false)
{
System.out.println(st.pop());//Printing stack element one
by one
}
}
}
//End of the Program


If you have any doubts please feel free to ask.
Thank you. All the best..!
(java) Write an algorithm that displays all the characters in a stack in the order in...
(Java - Stack) Describe a nonrecursive algorithm for enumerating all permutations of the numbers {1,2, . . . ,n} using an explicit stack.
Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...
Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...
Program in Netbeans
Write a method Stack Merge (Stack p1, Stack p2) to return a stack with merged elements from both stacks without duplicate. Elements of the stack are objects. Complete the main program Q2.java that uses two stacks with random numbers and displays all the stacks p0, p1 and p2 before and after calling the method including the resulted stack p0.
Write a Java program called Histogram.java that displays a list of distinct characters in an input tile and the occurrence of each eharacte. Your iogram should 1ead an input file name from a use. After that, your program should read characters in the file and display a list of distinct characters and their occurTeces. Finally, your program should draw a veril bafo the occuences For the assignment, your program has.to display the result exactly as the sample run. For instance,...
Java: Code a class encapsulating a stack of clothes using an array. A clothing item has the following attributes: names, color, and whether it can be washed at high temperature. Limit your stack to 20 clothing items. In addition to creating your push, pop, and peek methods; create two additional methods: a method that return all of the clothing items of a given color; a method that returns how many clothing items in the stack can be washed at high...
Write a program that uses a stack to reverse its inputs. Your
stack must be generic and you must demonstrate that it accepts both
String and Integer types. Your stack must implement the following
methods:
push,
pop,
isEmpty (returns true if the stack is empty and false
otherwise), and
size (returns an integer value for the number of items in the
stack).
You may use either an ArrayList or a LinkedList to implement
your stack. Also, your pop method must...
Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...
Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop. We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...
I need java code for the following problem.
Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...