ex3
Supplementary Exercise for Programming (Coding)
import java.util.*;
public class StacksTest {
public static void main(String[] args) {
Stack s = new Stack();
s.push(new Integer(3));
s.push(new Integer(5));
s.push(new String("hi"));
while(!s.isEmpty()) {
System.out.print(s.pop() + " ");
}
s.clear(); //Empty the contents of
the stack
System.out.println("\nHere's how I
reverse a string: ");
Scanner k = new
Scanner(System.in);
System.out.print("Enter a
string> ");
String input = k.nextLine();
for(int i = 0; i <
input.length(); i++)
s.push(input.charAt(i) + "");
System.out.println("The reversed
string is: ");
while(!s.isEmpty()) {
System.out.print(s.pop());
}
System.out.println();
}
}
------------------------------------------------------------------------------------------------------------------------------
public class Stack {
private java.util.ArrayList pool = new java.util.ArrayList();
public Stack() {
}
public Stack(int n) {
pool.ensureCapacity(n);
}
public void clear() {
pool.clear();
}
public boolean isEmpty() {
return pool.isEmpty();
}
public Object topEl() {
if (isEmpty())
throw new java.util.EmptyStackException();
return pool.get(pool.size()-1);
}
public Object pop() {
if (isEmpty())
throw new java.util.EmptyStackException();
return pool.remove(pool.size()-1);
}
public void push(Object el) {
pool.add(el);
}
public String toString() {
return pool.toString();
}
}
import java.util.*; public class StacksTest { static class Stack { private ArrayList pool = new ArrayList(); public Stack() { } public Stack(int n) { pool.ensureCapacity(n); } public void clear() { pool.clear(); } public boolean isEmpty() { return pool.isEmpty(); } public Object topEl() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.get(pool.size() - 1); } public Object pop() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.remove(pool.size() - 1); } public void push(Object el) { pool.add(el); } public String toString() { return pool.toString(); } public static Boolean isPalindrome(String input) { Stack s = new Stack(); for (int i = 0; i < input.length(); i++) s.push(input.charAt(i)); boolean palindrome = true; int i = 0; while(i < input.length()) { if(input.charAt(i) != (char)s.pop()) { palindrome = false; break; } i++; } return palindrome; } } public static void main(String[] args) { Stack s = new Stack(); s.push(new Integer(3)); s.push(new Integer(5)); s.push(new String("hi")); while (!s.isEmpty()) { System.out.print(s.pop() + " "); } s.clear(); // Empty the contents of the stack System.out.println("\nHere's how I reverse a string: "); Scanner k = new Scanner(System.in); System.out.print("Enter a string> "); String input = k.nextLine(); for (int i = 0; i < input.length(); i++) s.push(input.charAt(i)); boolean palindrome = isPalindrome(input); System.out.println(); } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.