Advertisement
ralphdc09

its101.md.wk.07.StackImplementation

Nov 2nd, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. package its101.mdwk07;
  2.  
  3. //Create a custom Stack class for creating stack
  4. public class Stack {
  5.     private final int maxSize;
  6.     private final int[] stackArray;
  7.     private int top;
  8.  
  9.     public Stack(int h) {
  10.         maxSize = h;
  11.         stackArray = new int[maxSize];
  12.         top = -1;
  13.     }
  14.  
  15.     //Create push() method, this will be use to push new element to the top of the stack
  16.     public void push(int j) {
  17.         if (top >= (maxSize - 1)){
  18.             System.out.println("Error: Stack is overflow");
  19.         }
  20.         else {
  21.         stackArray[++top] = j;
  22.         System.out.println(j + " is pushed into the stack");
  23.         }
  24.     }
  25.  
  26.     //Create pop() method, this will be use to pop element from the top of the stack
  27.     public int pop() {
  28.         if (top < 0){
  29.             System.out.print("Error: Stack is underflow");
  30.         }
  31.         else {
  32.             int k = stackArray[top--];
  33.             System.out.println(k + " is popped from the stack");
  34.         }
  35.         return 0;
  36.     }
  37.  
  38.     //Create peek() method, this will look at the top element of the stack without removing it
  39.     public int peek() {
  40.         return stackArray[top];
  41.     }
  42.  
  43.     //Create print() method, this will print the stack's elements
  44.     void printStack(){
  45.         for(int i = top;i>-1;i--){
  46.             System.out.print(" "+ stackArray[i]);
  47.         }
  48.         System.out.println();
  49.     }
  50.  
  51.     //Create isEmpty() method, this will check if the stack is empty or not
  52.     public boolean isEmpty() {
  53.         return (top == -1);
  54.     }
  55.  
  56.     //Create isFull() method, this will check if the stack is full or the set maxSize s is reached
  57.     public boolean isFull() {
  58.         return (top == maxSize -1);
  59.     }
  60.  
  61.     public static void main(String[] args) {
  62.         // Create stack object
  63.         Stack MyStack = new Stack(5); // maxSize is set to 5
  64.  
  65.         // Check if stack is empty using isEmpty()
  66.         boolean emptyResult = MyStack.isEmpty();
  67.         System.out.println("Is the stack empty? " + emptyResult);    //  It will print true because the stack is empty
  68.  
  69.         // Push elements into the stack
  70.         MyStack.push(1);
  71.         MyStack.push(2);
  72.         MyStack.push(3);
  73.         MyStack.push(4);
  74.         MyStack.push(5);
  75.  
  76.         // Print the stack element using the print()
  77.         System.out.print("The element of the stack: ");
  78.         MyStack.printStack();
  79.  
  80.         // Check again if stack is empty using isEmpty()
  81.         emptyResult = MyStack.isEmpty();
  82.         System.out.println("Is the stack empty? " + emptyResult);   //  It will print false because the stack is now contain elements
  83.  
  84.         // Check if stack is full using isFull()
  85.         boolean fullResult = MyStack.isFull();
  86.         System.out.println("Is the stack full? " + fullResult);    // It will print true because the maxSize is reached
  87.  
  88.         // Check what element at the top of the stack using peek()
  89.         int topElement = MyStack.peek();
  90.         System.out.println("Element at top of stack: " + topElement);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement