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

Commit

Permalink
Estructura de Datos - Lab 4 : Lista Enlazada
Browse files Browse the repository at this point in the history
* Lista enlazada

* Expan Queues impls

* Pila con Linked List

* Refactor LEPila's hierarchy

* Add a Peek method to queues

* Stage Changes

* Stage changes

* App implementation

* Presentable de laboratorio
  • Loading branch information
Laifsyn authored Jul 17, 2024
1 parent bf5116f commit a012b29
Show file tree
Hide file tree
Showing 10 changed files with 863 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.utp.clsEstructuraDatos.Estructuras.colas;

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

public abstract class AbstractLinkedQueue<T> implements IQueue<T> {
protected LinkedList<T> inner = new LinkedList<>();
protected int len = 0;

@Override
/**
* Linked List has no capacity constraint
*/
public boolean isFull() {
return false;
}

@Override
public int len() {
return this.len;
}

@Override
public void clear() {
inner.clear();
this.len = 0;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < inner.len(); i++) {
sb.append(inner.get(i).get().toString());
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append("]");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.utp.clsEstructuraDatos.Estructuras.colas;

public interface IQueue<T> {
default boolean isEmpty() {
return (len() == 0);
}

boolean isFull();

void insert(T elemento);

T remove();

T peek();

int len();

void clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.utp.clsEstructuraDatos.Estructuras.linked_list;

import com.utp.clsEstructuraDatos.Estructuras.colas.AbstractLinkedQueue;

public class LEColaCircular<T> extends AbstractLinkedQueue<T> {

@Override
public void insert(T elemento) {
len += 1;
inner.insert_last(elemento);
}

@Override
public T remove() {
if (this.isEmpty()) {
return null;
}
len -= 1;
return inner.remove_first().get();
}

@Override
public T peek() {
var e = inner.get(0);
if (e.isPresent()) {
return e.get();
}
return null;
}

public LinkedList<T> extricate() {
return inner;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.utp.clsEstructuraDatos.Estructuras.linked_list;

import com.utp.clsEstructuraDatos.Estructuras.colas.AbstractLinkedQueue;

public class LEColaLineal<T> extends AbstractLinkedQueue<T> {

/**
* Append at the end of the queue
*/
@Override
public void insert(T elemento) {
len += 1;
inner.insert_last(elemento);
}

/**
* Remove the latest inserted element from the queue
*/
@Override
public T remove() {
if (this.isEmpty()) {
return null;
}
if (inner.len() == 1) {
len = 0;
}
return inner.remove_first().get();
}

@Override
public T peek() {
var e = inner.get(0);
if (e.isPresent()) {
return e.get();
}
return null;
}

public LinkedList<T> extricate() {
return inner;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.utp.clsEstructuraDatos.Estructuras.linked_list;

import com.utp.clsEstructuraDatos.Estructuras.pila.IStack;

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() {
len = 0;
inner.clear();
}

@Override
public int capacity() {
return -1;
}

@Override
/**
* Linked List has no capacity constraint
*/
public boolean isFull() {
return false;
}

public LinkedList<T> to_inverted() {
var inner = new LinkedList<T>();
for (int i = len - 1; i >= 0; i--) {
inner.insert_first(this.inner.get(i).get());
}
return inner;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < len; i++) {
sb.append(inner.get(i).get().toString());
sb.append("| ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append("]");
return sb.toString();
}
}
Loading

0 comments on commit a012b29

Please sign in to comment.