Programming Projects involving Stacks
Project 1) Make the
isPlandrome() method below work using only 1 Stack and no other data structures
import java.util.Stack;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Palindrome2Student {
public static Stack<Character>stack= new Stack();
public static void main(String args[]){
String word= JOptionPane.showInputDialog("Type in a potential palindrome");
int lenght;
System.out.println(isPalindrome(word));
}
public static boolean isPalindrome(String stringToParse){
int length = stringToParse.length();
for (int i =0;i < length;i++){
System.out.println("Pushing in: "+ stringToParse.charAt(i));
stack.push(stringToParse.charAt(i));
}
System.out.println("________________________");
for (int i =0;i < length;i++){
System.out.println("Popping out : "+stack.pop());
}
return false;
}
}
Project 2) Write a class that ensures that brackets, curley braces and parentheses are properly closed. This class should work for nested delimiters.
Using stack to check matching delimiters |
Examples: |
- c[d] // correct
- a{b[c]d}e // correct
- a{b(c]d}e // not correct; ] doesn't match (
- a[b{c}d]e} // not correct; nothing matches final }
- a{b(c) // not correct; Nothing matches opening {
|