diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/linked_list/LinkedList.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/linked_list/LinkedList.java index c4a8ea3..047164b 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/linked_list/LinkedList.java +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/linked_list/LinkedList.java @@ -70,6 +70,21 @@ public Optional remove_last() { return element; } + public Optional get(int idx) { + if (idx < 0 || idx >= len) { + return Optional.empty(); + } + Optional> child = this.child; + for (int i = 0; i < idx; i++) { + child = child.get().nextNode(); + } + return Optional.of(child.get().value); + } + + public Optional peek_first() { + return get(0); + } + public int size() { return len; } diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/IStack.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/IStack.java new file mode 100644 index 0000000..a389b67 --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/IStack.java @@ -0,0 +1,23 @@ +package com.utp.clsEstructuraDatos.Estructuras.pila; + +public interface IStack { + default boolean isEmpty() { + return (len() == 0); + } + + default boolean isFull() { + return len() == capacity(); + } + + void push(T elemento); + + T pop(); + + T peek_top(); + + int len(); + + void clear(); + + int capacity(); +} diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/LEPila.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/LEPila.java new file mode 100644 index 0000000..a769c1c --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/LEPila.java @@ -0,0 +1,60 @@ +package com.utp.clsEstructuraDatos.Estructuras.pila; + +import com.utp.clsEstructuraDatos.Estructuras.linked_list.LinkedList; + +public class LEPila implements IStack { + int len = 0; + LinkedList inner = new LinkedList(); + + @Override + public void push(T elemento) { + len += 1; + inner.insert_first(elemento); + } + + @Override + public T pop() { + if (isEmpty()) { + return null; + } + var e = inner.remove_first(); + len -= 1; + if (e.isPresent()) { + return e.get(); + } + return null; + } + + @Override + public T peek_top() { + var e = inner.get(0); + if (e.isPresent()) { + return e.get(); + } + return null; + } + + @Override + public int len() { + return this.len; + } + + @Override + public void clear() { + inner.clear(); + } + + @Override + public int capacity() { + return -1; + } + + @Override + /** + * Linked List has no capacity constraint + */ + public boolean isFull() { + return false; + } + +} diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/Pila.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/Pila.java similarity index 99% rename from src/main/java/com/utp/clsEstructuraDatos/Estructuras/Pila.java rename to src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/Pila.java index 86d1163..87fc100 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/Pila.java +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/pila/Pila.java @@ -1,4 +1,4 @@ -package com.utp.clsEstructuraDatos.Estructuras; +package com.utp.clsEstructuraDatos.Estructuras.pila; import java.util.ArrayList; diff --git a/src/main/java/com/utp/clsEstructuraDatos/laboratorio_1/Main.java b/src/main/java/com/utp/clsEstructuraDatos/laboratorio_1/Main.java index 81d3959..e4090dd 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/laboratorio_1/Main.java +++ b/src/main/java/com/utp/clsEstructuraDatos/laboratorio_1/Main.java @@ -4,13 +4,14 @@ import javax.swing.JFrame; import javax.swing.JOptionPane; +import com.utp.clsEstructuraDatos.Estructuras.pila.Pila; + import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.TextArea; import java.util.ArrayList; -import com.utp.clsEstructuraDatos.Estructuras.Pila; public class Main { public static void main(String[] args) { App app = new App(); @@ -32,7 +33,7 @@ void start() { frame.setLayout(new GridBagLayout()); add_button_commands(frame); GridBagConstraints c = new GridBagConstraints(); - + // Representación en vivo de la Pila c.gridy = 2; c.gridwidth = 8;