Question

In: Computer Science

2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...

2. Using the Stack ADT:

  1. Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry.

In Java please.

Solutions

Expert Solution

import java.io.*;
import java.util.*;
  
public class Main
{
//method to insert an element on the top of the stack
static void push(Stack<Character> st, char element)
{
st.push(element);
}
  
//method to get an elemet from the top of the stack
static char pop(Stack<Character> st)
{
char p;
p = st.pop();
return p;
}
  
   public static void main (String[] args)
{
Stack<Character> st = new Stack<Character>();
  
Scanner sc = new Scanner(System.in);
Scanner input = new Scanner(System.in);
  
//get the number of lines
System.out.print("Enter the number of lines: ");
int num = sc.nextInt();
  
//get the input string lines
for(int i=0; i<num; i++)
{
String str = input.nextLine();
for(int k=0; k<str.length(); k++)
{
char c = str.charAt(k);
push(st, c);
}
push(st, '\n');
}
  
//display the result
System.out.print("\nThe reverse strings are: ");
while(!st.isEmpty())
{
System.out.print(pop(st));
}
}
}

OUTPUT:

Enter the number of lines: 2
hello hi yes you can do it
ok bye

The reverse strings are:
eyb ko
ti od nac uoy sey ih olleh


Related Solutions

Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Create an ADT class that creates a friend contact list. The program should be able to...
Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++
Stack ADT What would you say is the most important drawback of using the stack that...
Stack ADT What would you say is the most important drawback of using the stack that should be considered before choosing it for use in a real application? Typed out please.
create a C++ Program 1. Ask and get a course name 2. Create an array of...
create a C++ Program 1. Ask and get a course name 2. Create an array of students of size 10, 3. Initialize the elements of the students array of appropriate names and grades 4. Create an object of class GradeBook (provide the course name and the created student array, in 3 above, as arguments to the constructor call. The arguments are used to initialize the data members of the class GradeBook. Desired Output: ========================================================= Enter course name: Object Oriented Programming...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT