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.
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++
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...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT