Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Pila con Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Jun 29, 2024
1 parent d4cdb67 commit e904b71
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public Optional<T> remove_last() {
return element;
}

public Optional<T> get(int idx) {
if (idx < 0 || idx >= len) {
return Optional.empty();
}
Optional<Node<T>> child = this.child;
for (int i = 0; i < idx; i++) {
child = child.get().nextNode();
}
return Optional.of(child.get().value);
}

public Optional<T> peek_first() {
return get(0);
}

public int size() {
return len;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.utp.clsEstructuraDatos.Estructuras.pila;

public interface IStack<T> {
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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.utp.clsEstructuraDatos.Estructuras.pila;

import com.utp.clsEstructuraDatos.Estructuras.linked_list.LinkedList;

public class LEPila<T> implements IStack<T> {
int len = 0;
LinkedList<T> inner = new LinkedList<T>();

@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;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.Estructuras;
package com.utp.clsEstructuraDatos.Estructuras.pila;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit e904b71

Please sign in to comment.