//The Stack class represents last-in-first-out(LIFO)stack of objects
//push() is used to add items onto the top of a stack
//pop() is used to eliminate items from the top of a stack
//isEmpty() is used to tests if a stack is empty
import java.util.*;
public class HackDefence{
public static void main(String[] args){
Stack<String> stackVariable = new Stack<String>();
stackVariable.push("bottom");
displayStack(stackVariable);
stackVariable.push("middle");
displayStack(stackVariable);
stackVariable.push("top");
displayStack(stackVariable);
stackVariable.pop();
displayStack(stackVariable);
stackVariable.pop();
displayStack(stackVariable);
stackVariable.pop();
displayStack(stackVariable);
}
private static void displayStack(Stack<String> s){
if(s.isEmpty())
System.out.println("Stack is empty");
else
System.out.printf("%s TOP\n", s);
}
}
OUTPUT:
//push() is used to add items onto the top of a stack
//pop() is used to eliminate items from the top of a stack
//isEmpty() is used to tests if a stack is empty
import java.util.*;
public class HackDefence{
public static void main(String[] args){
Stack<String> stackVariable = new Stack<String>();
stackVariable.push("bottom");
displayStack(stackVariable);
stackVariable.push("middle");
displayStack(stackVariable);
stackVariable.push("top");
displayStack(stackVariable);
stackVariable.pop();
displayStack(stackVariable);
stackVariable.pop();
displayStack(stackVariable);
stackVariable.pop();
displayStack(stackVariable);
}
private static void displayStack(Stack<String> s){
if(s.isEmpty())
System.out.println("Stack is empty");
else
System.out.printf("%s TOP\n", s);
}
}
OUTPUT:
No comments:
Post a Comment