Advertisement
udaykumar1997

l

Apr 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public class StackDemo {
  2. private static final int capacity = 3;
  3. int arr[] = new int[capacity];
  4. int top = -1;
  5.  
  6. public void push(int pushedElement) {
  7. if (top < capacity - 1) {
  8. top ;
  9. arr[top] = pushedElement;
  10. System.out.println("Element " pushedElement
  11. " is pushed to Stack !");
  12. printElements();
  13. } else {
  14. System.out.println("Stack Overflow !");
  15. }
  16. }
  17.  
  18. public void pop() {
  19. if (top >= 0) {
  20. top--;
  21. System.out.println("Pop operation done !");
  22. } else {
  23. System.out.println("Stack Underflow !");
  24. }
  25. }
  26.  
  27. public void printElements() {
  28. if (top >= 0) {
  29. System.out.println("Elements in stack :");
  30. for (int i = 0; i <= top; i ) {
  31. System.out.println(arr[i]);
  32. }
  33. }
  34. }
  35.  
  36. public static void main(String[] args) {
  37. StackDemo stackDemo = new StackDemo();
  38.  
  39. stackDemo.pop();
  40. stackDemo.push(23);
  41. stackDemo.push(2);
  42. stackDemo.push(73);
  43. stackDemo.push(21);
  44. stackDemo.pop();
  45. stackDemo.pop();
  46. stackDemo.pop();
  47. stackDemo.pop();
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement