**Create your own pie chart with ChartMaker


A+    A−    B  
Home
Algebra
Math Games
Geometry
Private Tutors
Interactive
Trigonometry
Jobs
Teacher Resources

AddThis Social Bookmark Button

Stack Data Type

Import java.util.stack;

To get a feel for some of the methods of the Stack. Run the code from this webpage.

Strack Programming Exercises

  • Exercise 1) Write your own pared down version of the Stack class. This class should have the following methods . Refer to the quick reference to see what these methods should do and return. You may want to use an Object[] array to hold Stack elements. When the array fills up, allocate an array of twice the size and copy the values to the larger array.
    • push(E Object)
    • pop()
    • peek()
    • isEmpty();

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;
	}

}
	  


solution



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:

  1. c[d] // correct
  2. a{b[c]d}e // correct
  3. a{b(c]d}e // not correct; ] doesn't match (
  4. a[b{c}d]e} // not correct; nothing matches final }
  5. a{b(c) // not correct; Nothing matches opening {


solution



Top
AddThis Social Bookmark Button