jtentor

Stack (en Java) de Estructura de Datos - Stack.java

Sep 20th, 2020
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. //
  2. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  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.     public Stack(Integer dimension) {
  42.         if (dimension <= 0) {
  43.             throw new RuntimeException("La cantidad de elementos en la  pila debe ser positiva");
  44.         }
  45.         this.data = (ELEMENT []) new Object[dimension];
  46.         this.count = 0;
  47.     }
  48.  
  49.     //endregion
  50.  
  51.     //region Stack Methods
  52.  
  53.     // Test if this stack is empty.
  54.     public boolean empty() {
  55.         return this.count <= 0;
  56.     }
  57.  
  58.     // Looks at the object at the top of this stack without removing it from the stack.
  59.     public ELEMENT peek() {
  60.         if (this.empty()) {
  61.             throw new RuntimeException("La pila está vacía...");
  62.         }
  63.         return this.data[this.count - 1];
  64.     }
  65.  
  66.     // Removes the object at the top of this stack and returns that object as the value of this function.
  67.     public ELEMENT pop() {
  68.         if (this.empty()) {
  69.             throw new RuntimeException("La pila está vacía...");
  70.         }
  71.         --this.count;
  72.         return this.data[this.count];
  73.     }
  74.  
  75.     // Pushes an item onto the top of this stack.
  76.     public ELEMENT push(ELEMENT element) {
  77.         if (this.size() >= this.data.length) {
  78. //            throw new RuntimeException("La pila está llena...");
  79.  
  80.             ELEMENT [] temp = (ELEMENT []) new Object[this.data.length * 2];
  81.             for (int i = 0; i < this.data.length; ++i) {
  82.                 temp[i] = this.data[i];
  83.             }
  84.             this.data = temp;
  85.         }
  86.         this.data[this.count] = element;
  87.         ++this.count;
  88.         return element;
  89.     }
  90.  
  91.     // Returns the 1-based position where an object is on this stack.
  92.     public int search(Object object) {
  93.         for (int pos = this.count - 1; pos >= 0; --pos) {
  94.             if (this.data[pos].equals(object)) {
  95.                 return this.count - pos;
  96.             }
  97.         }
  98.         return -1;
  99.     }
  100.     //endregion
  101.  
  102.     //region Inherited Methods
  103.  
  104.     // from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Vector.html
  105.     // Returns the number of components in this vector.
  106.     public int size() {
  107.         return this.count;
  108.     }
  109.  
  110.     //endregion
  111.  
  112.  
  113.  
  114.     //region Override Object basic methods
  115.  
  116.     @Override
  117.     public String toString() {
  118.  
  119.         if (this.size() <=0) {
  120.             return "";
  121.         }
  122.  
  123.         // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  124.         StringBuilder sb = new StringBuilder();
  125.         sb.append("[" + this.data[0].toString());
  126.         for (int i = 1; i < this.size(); ++i) {
  127.             sb.append(", " + this.data[i].toString());
  128.         }
  129.         sb.append("]");
  130.         return sb.toString();
  131.     }
  132.     //endregion
  133.  
  134.  
  135. }
  136.  
Add Comment
Please, Sign In to add comment