Thumbs up for help.create a stack and write a program that reverses the letters in a string. Then display. use the LinkedList class in java to implement a stack.
import java.util.*;
class Stack1
{
LinkedList<Character> l;
// constructor
Stack1()
{
// create a linked list
this.l = new LinkedList<Character>();
}
// return the size of the Stack1
public int size()
{
return l.size();
}
// push element into Stack1
public void push(char ch)
{
// addFirst() function adds the element in the front of the list
this.l.addFirst(ch);
}
// pop element from Stack1
public char pop()
{
// if Stack1 is empty
if( this.isEmpty() == true )
{
System.out.println("Stack1 is empty");
return ' ';
}
// get first element in the list
char ans = this.l.get(0);
// removeFirst() function removes the element from the front of the list
this.l.removeFirst();
return ans;
}
public boolean isEmpty()
{
return this.size() == 0;
}
}
class Solution
{
// return the reversed string of str
public static String reverse(String str)
{
// create a Stack1 to store character
Stack1 st = new Stack1();
int i;
// traverse the string
for( i = 0 ; i < str.length() ; i++ )
{
// add the character at index i to Stack1
// charAt(i) return the character at index i in String
st.push( str.charAt(i) );
}
// store the reversed String
String ans = "";
// loop untill Stack1 is empty
while( !st.isEmpty() )
// pop the top most element from Stack1 using pop()
ans += st.pop();
return ans;
}
public static void main(String[] args)
{
// create a Scanner class object
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
// read complete line
String str = sc.nextLine();
// split the string using space as delimeter
String[] arr = str.split(" ");
int i;
// create an array to store reversed words
String[] rev = new String[ arr.length ];
for( i = 0 ; i < arr.length ; i++ )
// reverse the current word
rev[i] = reverse( arr[i] );
System.out.print("\nReversed sentence : ");
for( i = 0 ; i < rev.length ; i++ )
System.out.print(rev[i] + " ");
}
}
Sample Output

Thumbs up for help.create a stack and write a program that reverses the letters in a...
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...
Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...
Java Write a pseudocode and Java program to implement the Stack using arrays. Write Push(), Pop(),and Display() methods to demonstrate that it works.
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); ...
Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter and reverses only the values in the bottom half of the Stack. For example, if a Stack containing the values [1, 2, 3, 4, 5] were passed in (with 1 at the bottom and 5 at the top), the Stack would be changed to [2, 1, 3, 4, 5] (with 2 at the bottom and 5 at the top) after this method was called...
Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...
need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class. Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop() Test your class thoroughly before using it within the soduku program Part II. Create...
WRITE A JAVA PROGRAM . (WILL GUARANTEE A THUMBS UP) BELOW ARE INSTRUCTIONS: IF USER ENTERS: "1, 2, 3" THIS IS THE Expected Output: 1,1,2,2,3,3
MUST BE WRITTEN IN JAVA CODE Write a program that uses a stack to determine whether a string is a palindrome (i.e., the string is spelled identically backward and forward). The program should ignore spaces and punctuation.
Here's the problem that I have to solve: Write a Java program that uses a Stack data structure to evaluate postfix expressions. Your program takes a postfix expression as an input,for example:3 42.3+ 5.25* ,from the user and calculates/display the result of the expression. What I'm having trouble is getting it to reading and calculating it as postfix Here's my code: import java.util.Scanner; public class Assignment2 { public static void main(String[] args) { Scanner scan = new...