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
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() { string name = "sherry"; reverse(name); cout << name << endl; //should...
Write a program that reverses a string and prints it on the screen. Sample input/output: Enter a string to be reversed: Testing the reverse string program reversed string: margorp gnirts esrever eht gnitseT