Advertisement
korobushk

Stack

Mar 18th, 2021
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. public class App {
  2.      public static void main(String[] args) {
  3.  
  4.         Stack theStack = new Stack(5);
  5. ////
  6. //        theStack.push('h');  //0
  7. //        theStack.push('e');  //1
  8. //        theStack.push('l');  //2
  9. //        theStack.push('l');  //3
  10. //        theStack.push('o');  //4
  11. //
  12. //        while (!theStack.isEmpty()) {
  13. //            char value = theStack.pop();
  14. //            System.out.print(value);
  15. //        }
  16.         String input = "hola como estas hello how are you";
  17.  
  18.         System.out.println(reverseString(input));
  19.     }
  20.  
  21.  
  22.     public static String reverseString(String str){
  23.         int stackSize = str.length(); // get the max stack size
  24.         Stack theStack = new Stack(stackSize); // we make the stack
  25.         for(int j = 0; j < str.length(); j++){
  26.             char ch = str.charAt(j); // getting a char from the input string
  27.             theStack.push(ch);
  28.         }
  29.  
  30.         String result = "";
  31.         while(!theStack.isEmpty()){
  32.             char ch = theStack.pop();
  33.             result = result+ ch; // appending to the output
  34.         }
  35.  
  36.         return result;
  37.     }
  38.  
  39. }
  40.    
  41.    
  42.    
  43.    
  44.     public static class Stack {
  45.         private final int maxSize;
  46.         private final char[] stackArray;
  47.         private int top;
  48.  
  49.         public Stack(int size) {
  50.             this.maxSize = size;
  51.  
  52.             this.stackArray = new char[maxSize];
  53.             this.top = -1;
  54.         }
  55.  
  56.         public void push(char j) {                       // top=0                     top=1                    top=2           top=3
  57.             if (isFull())
  58.                 System.out.println("stack is full");
  59.             else
  60.                 top++;
  61.             stackArray[top] = j;
  62.             // stackArray[0]=20;      stackArray[1]=40        stackArray[2]=60       stackArray[3]=80
  63.         }
  64.  
  65.  
  66.         public char pop() {
  67.  
  68.             if (isEmpty()) {
  69.                 System.out.println("stack is empty");
  70.                 return '0';
  71.             } else {
  72.                 int old_top = top;                                  //top=3
  73.  
  74.                 top--;
  75.                 return stackArray[old_top];                         // return 3     80 60  40 20
  76.  
  77.             }                                                           //top=2
  78.         }
  79.  
  80.         public char peak() {
  81.             return stackArray[top];
  82.         }
  83.  
  84.         public boolean isEmpty() {
  85.             return (top == -1);
  86.  
  87.         }
  88.  
  89.         public boolean isFull() {
  90.             return (maxSize - 1 == top);
  91.         }
  92.     }
  93.  
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement