Welcome To The World Of Hacking


Learn Hacking|Teach Hacking|Learn To Secure|Learn To Code

Saturday

JAVA PROGRAMMING TUTORIAL 38 : STACK

//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:

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...