Advertisement
LEANDRONIEVA

Stack

Sep 27th, 2022
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. //
  2. //Created by Julio Tentor <[email protected]>
  3. //
  4.  
  5.  
  6. /*
  7. The Stack class represents a last-in-first-out (LIFO) stack of objects.
  8.  
  9. The usual push and pop operations are provided, as well as a method to
  10. peek at the top item on the stack, a method to test for whether the
  11. stack is empty, and a method to search the stack for an item and discover
  12. how far it is from the top.
  13.  
  14. When a stack is first created, it contains no items.
  15.  
  16. from https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/Stack.html
  17. from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Stack.html
  18. from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Stack.html
  19.  
  20. */
  21. public class Stack<ELEMENT> {
  22.  
  23.  //region Constants
  24.  
  25.  private final static Integer defaulDimension = 10;
  26.  
  27.  //endregion
  28.  
  29.  //region Attributes
  30.  
  31.  private ELEMENT [] data;
  32.  private Integer count;
  33.  
  34.  //endregion
  35.  
  36.  //region Constructors
  37.  
  38.  public Stack() {
  39.      this(Stack.defaulDimension);
  40.  }
  41.  @SuppressWarnings("unchecked")
  42. public Stack(Integer dimension) {
  43.      if (dimension <= 0) {
  44.          throw new RuntimeException("La cantidad de elementos en la  pila debe ser positiva");
  45.      }
  46.      this.data = (ELEMENT []) new Object[dimension];
  47.      this.count = 0;
  48.  }
  49.  
  50.  //endregion
  51.  
  52.  //region Stack Methods
  53.  
  54.  // Test if this stack is empty.
  55.  public boolean empty() {
  56.      return this.count <= 0;
  57.  }
  58.  
  59.  // Looks at the object at the top of this stack without removing it from the stack.
  60.  public ELEMENT peek() {
  61.      if (this.empty()) {
  62.          throw new RuntimeException("La pila está vacía...");
  63.      }
  64.      return this.data[this.count - 1];
  65.  }
  66.  
  67.  // Removes the object at the top of this stack and returns that object as the value of this function.
  68.  public ELEMENT pop() {
  69.      if (this.empty()) {
  70.          throw new RuntimeException("La pila está vacía...");
  71.      }
  72.      --this.count;
  73.      return this.data[this.count];
  74.  }
  75.  
  76.  // Pushes an item onto the top of this stack.
  77.  public ELEMENT push(ELEMENT element) {
  78.      if (this.size() >= this.data.length) {
  79. //         throw new RuntimeException("La pila está llena...");
  80.  
  81.          @SuppressWarnings("unchecked")
  82.         ELEMENT [] temp = (ELEMENT []) new Object[this.data.length * 2];
  83.          for (int i = 0; i < this.data.length; ++i) {
  84.              temp[i] = this.data[i];
  85.          }
  86.          this.data = temp;
  87.      }
  88.      this.data[this.count] = element;
  89.      ++this.count;
  90.      return element;
  91.  }
  92.  
  93.  // Returns the 1-based position where an object is on this stack.
  94.  public int search(Object object) {
  95.      for (int pos = this.count - 1; pos >= 0; --pos) {
  96.          if (this.data[pos].equals(object)) {
  97.              return this.count - pos;
  98.          }
  99.      }
  100.      return -1;
  101.  }
  102.  //endregion
  103.  
  104.  //region Inherited Methods
  105.  
  106.  // from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Vector.html
  107.  // Returns the number of components in this vector.
  108.  public int size() {
  109.      return this.count;
  110.  }
  111.  
  112.  //endregion
  113.  
  114.  
  115.  
  116.  //region Override Object basic methods
  117.  
  118.  @Override
  119.  public String toString() {
  120.  
  121.      if (this.size() <=0) {
  122.          return "";
  123.      }
  124.  
  125.      // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  126.      StringBuilder sb = new StringBuilder();
  127.      sb.append("[" + this.data[0].toString());
  128.      for (int i = 1; i < this.size(); ++i) {
  129.          sb.append(", " + this.data[i].toString());
  130.      }
  131.      sb.append("]");
  132.      return sb.toString();
  133.  }
  134.  //endregion
  135.  
  136.  @SuppressWarnings("unchecked")
  137. public int reemplazarValor(ELEMENT actual, ELEMENT nuevo) {
  138.         int cont = 0;
  139.         Stack<Character> pilaAux = new Stack<Character>();
  140.         if(this.empty()) {
  141.             System.out.println("Pila vacía");
  142.         }else {
  143.             while(!this.empty()) {
  144.                 if (this.peek()==actual) {
  145.                     pilaAux.push((Character) nuevo);
  146.                     this.pop();
  147.                     cont++;
  148.                 }else {
  149.                     pilaAux.push((Character) this.pop());
  150.                 }
  151.             }
  152.         }
  153.         while(!pilaAux.empty()) {
  154.             this.push((ELEMENT) pilaAux.pop());
  155.         }
  156.        
  157.         return cont;
  158.     }
  159.  
  160.  public String MostrarPila() {
  161.         String valores = "";
  162.         while(!this.empty()) {
  163.             valores += this.pop();
  164.         }
  165.    
  166.     return valores;
  167.     }
  168.  
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement