No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Construyendo un Stack

21/29
Recursos

Aportes 158

Preguntas 11

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

o inicia sesión.

IMPORTANTE

Quiero que notes como el método peek lo único que hace es tomar el último elemento, sin embargo, NO lo elimina, ¿Qué pasaría si escribieras peek dos veces?

myStack.peek()
myStack.peek()

Pues las dos veces te va a devolver el mismo valor porque el elemento top nunca fue eliminado. Por tanto, no tendrías forma de acceder a los otros elementos que están en el stack, es por eso que el método peek suele ir acompañado del método pop:

myStack.peek()
myStack.pop()

De esta forma tomas el primer elemento al top de la pila y a su vez lo eliminas del top ^^

Lo pude resolver con el pop ya esta y te lo comparto 😄, hoy es 24 de diciembre asi que te deseo feliz noche buena y feliz navidad

Fue un placer haber estado en este curso, estoy en plazti Day me quedan 20min asi que fue un placer, hasta aqui llego.

  pop() {
    if (!this.length) {
      console.error("There is no nodes in the stack.");
      return;
    }
    const topNode = this.top;
    if (this.length === 1) {
      this.top = null;
      this.bottom = null;
    } else {
      this.top = this.top.next;
    }
    this.length--;
    return topNode
  }
}```

😀 Construyendo un Stack en Dart.

class Node{
  var value;
  var next;
  Node(this.value);
}

class Stack{
  var top;
  var bottom;
  int length = 0;

  dynamic peek() => top;

  dynamic push(var value){
    final newNode = Node(value);
    if(length == 0){
      top = newNode;
      bottom = newNode;
    }else{
      final holdingPointer = top;
      top = newNode;
      top.next = holdingPointer;
    }

    length++;
    return this;
  }

  dynamic pop(){
    final node = top;
    if(length == 0){
      return 'Error: Stack empty.';
    }else if(length == 1){
      top = node.next;
      bottom = null;
      length--;
    }else{
      top = node.next;
      length--;
    }

    return this;
  }
}
import 'stack.dart';

void main(List<String> arguments){
  final stack = Stack();
  stack.push(1);
  stack.push(2);
  stack.push(3);
  stack.peek();
  print(stack.length);
  stack.pop();
  print(stack.length);
}

Fijarse en las notas del .peek() y del .pop()

Mi código sin ver la clase. Espero no tener muchas correcciones.

class Node {
    constructor(value){
        this.value = value
        this.prev = null
    }
}

class Stack{
    constructor(){
        this.top = null
        this.bot = null
        this.lenght = 0
    }

    push(value){
        let newNode = new Node(value)
        if (this.lenght == 0){
            this.bot = newNode
        }
        newNode.prev = this.top
        this.top = newNode
        this.lenght++
        return this
    }
}

let stack = new Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)

Creo que podemos evitar usar la variable holdlingPointer en el método push de la siguiente manera:

push(value) {
    ...
    } else {
        newNode.next = this.top;
        this.top = newNode;
    }
    this.length++;
    return this;
}

Para actulizar el nuevo “Top” una vez eliminado con pop , lo que hice fue guardar cada valor con sus respectivos valores para next y previous e ir actulizando los valores paral el nuevo top y asi hasta vaciar el stack

class Node  {
    constructor(value) {
        this.value = value;
        this.next = null;
        this.previous = null;
    }
}


class Stack { 
    constructor() {
        this.top = null;
        this.length = 0;
    }

    
    peek() {
        return this.top.value;
    }


    push(value) {
        
        const newNode = new Node(value);

        // in case it's an empty stack 
        if(!this.top) {
            this.top = newNode;
        }
        else {
            // in case it's not an empty list
            // link the nodes up by assigning next and previous pointer to the following node
            newNode.previous = this.top;
            this.top.next = newNode;
            // update the top to this new node
            this.top = newNode;            
        }

        this.length++;

        return this;
    }


    pop() {

        if(this.length === 0) {
            console.warn('The Stack is empty');
            return undefined;
        }

        if(this.length === 1) {
            this.top.value = null;
            this.top.next = null;
            this.top.previous = null;

            this.length--;
        }
        
        if (this.length > 1){
            // get the the value of the previous node
            const holdindValue = this.top.previous;
             // once deleted the top, the previous value will become the new `top`
            this.top = holdindValue;    
            holdindValue.next = null;
    
            this.length--;
        }

        return this;

    }

}

const myStack = new Stack();

Aquí dejo mi código con validaciones para el método pop.

class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor() {
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }
    peek() {
        return this.top;
    }
    push(value) {
        const newNode = new Node(value);
        if(this.length === 0) {
            this.top = newNode;
            this.bottom = newNode;
        } else {
            const holdingPointer = this.top;
            this.top = newNode;
            this.top.next = holdingPointer;
        }

        this.length++;

        return this;
    }
    pop() {
        if(this.length === 1) {
            this.top = this.top.next;
            this.bottom = null;
            this.length--;
            console.log("Stack is empty now");
            return this;
        } else if(this.length === 0) {
            console.log("Stack is empty");
            return this;
        } else {
            this.top = this.top.next;
            this.length--;
            return this;
        }
    }
}

const myStack = new Stack();

Así me quedó mi Stack. En la literatura siempre recomiendan un método isEmpty, por lo que en lugar de revisar la longitud de la pila, mando a llamar a ese método para saber si la pila tiene o no elementos. Es algo de buenas prácticas.

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class Stack{
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }

    peek(){
        return this.top;
    }

    push(value){
        const newNode = new Node(value);
        if ( this.isEmpty() ) {
            this.top = newNode;
            this.bottom = newNode;
        }
        else{
            const holdingPointer = this.top;
            this.top = newNode;
            this.top.next = holdingPointer;
        }
        this.length++;
        return this;
    }

    pop(){
        if ( !this.isEmpty() ){
            const topItem = this.top;
            this.top = this.top.next;
            this.length--
            return topItem
        }
        return null;
    }

    isEmpty(){
        return this.length == 0;
    }

}

const myStack = new Stack();

Aqui les dejo mi metodo pop(), sencillo y facil de entender

pop(){
        //Valida que hayan elementos en el stack
        if(this.length > 0){
            const popped = this.top
            this.top = this.top.next
            this.length--
            return popped
        }
        //En caso de que el stack este vacio
        return null
    }

una aplicación clásica de las pilas o stacks con recursión son las famosas torres de Hanoi, comparto un ejemplo en JavaScript

Aporte con mi método pop:
.
.
.
.
.
.
.
.
.


pop(){
        if(this.length ==0) return console.warn('There are not more elements in the stack');
        if(this.length ==1){
            this.bottom = null;
        }
        const newTop = this.top.next;
        this.top = newTop
        this.length--;
        return this
    }

Mi aporte:

pop(){
        if(this.length===0){
            console.log("Stack Vacío");
        }else if(this.length===1){
            console.log(`Eliminado este elemento: "${this.top.value}"`);
            this.top=null;
            this.bottom=null;
            this.length=0;
        }else {
            const newTop = this.top.next;
            this.top=newTop;
            this.length--;
        }
        return this;
    }

Aquí mi solución al reto:

pop(){
        if (this.lenght>0){
            const nodeTop = this.top;
            this.top = this.top.next;
            this.lenght--;
            return nodeTop;
        } else
            return null;
    }

Aquí está mi aporte:

class Node {
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class Stack { //LIFO: Last In, First Out
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }

    pop () {
        if (this.length === 0) return null;
        let currentNode = this.bottom;

        if (this.length === 1) {
            this.bottom = null;
            this.top = null;
        } else if (1 < this.length) {
            for (let i = 0; i < this.length - 2; i++) {
                currentNode = currentNode.next;
            }
    
            currentNode.next = null;
            this.top = currentNode;
        }

        this.length--;

        return currentNode;
    }

    push (value) {
        const newNode = new Node(value);

        if (this.length === 0) {
            this.top = newNode;
            this.bottom = this.top;
            
        } else {
            this.top.next = newNode;
            this.top = newNode;
        }
        
        this.length++;
    
        return newNode;
    }

    peek () {
        return this.top;
    }

    isEmpty () {
        return this.length === 0;
    }
}

Les comparto mi solución al reto pop()

  pop() {
    const deletedValue = this.top;
    this.top = this.top.next;
    this.length--;
    return deletedValue;
  }

Mi método de pop:

pop(){
        this.top = this.top.next;
        this.length--;
        return this;
    }

Mi solución del método pop()

// Elimina el ultimo elemento
    pop(){
        if(!this.top){
            return undefined;
        }
        const deleted = this.top.value;
        
        if(this.length === 1){
            this.top = null;
            this.bottom = null;
        }else{
            const holdingPointer = this.top.next;
            this.top = holdingPointer;
        }
        
        this.length--;
        return deleted;
    }

Metodo pop

pop(){
  if(!this.top){
    return false;
  }
  if(this.length == 1){
    this.top = null;
    this.bottom = null;
    this.length--;
    return true;
  }
  let position = this.bottom;
  //Busco el penultimo elemento
  for(let i = 1; i < this.length-1; i++){ 
     position = position.next;
  }
  position.next = null;
  this.top = position;
  this.length--;
  return true; 
}

el .pop() retorna el elemento removido.
Si se remueven todos los elementos el stack queda vacio

pop() {
        if (this.length == 0) {
            console.error('el stack esta vacio');
            return undefined;
        }

        const nodeToRemove = this.top;
        delete this.top;
        this.top = nodeToRemove.next;
        
        this.length--;
        
        if (this.length == 0){
            this.bottom = null;
        }

        return nodeToRemove;
    }

Pop Method

pop() {
    if(this.length != 0){
      this.top = this.top.next;
      this.length--;
      return this;
    }
  }

Esta fue mi solucion con el metodo pop:

pop(){
    if(this.length === 1){
      this.bottom = null;
      this.top = null
    } else {
      this.top = this.top.next;
    }
    this.length--;
    return this;
  }

Por acá dejo mi solución al mñetodo pop

pop() {
        const holdingPointer = this.top.next;
        delete this.top;
        this.top = holdingPointer;
        if (this.length === 1) {
            this.bottom = null;
        }
        this.length--;
        return this;
    }

Comparto la solución que hice para el metodo pop:

pop() {
    if (this.length === 0) {
      return this;
    }
    
    const holdingPointer = this.top;
    this.top = holdingPointer.next;

    if (this.length === 1) {
      this.bottom = null;
    }

    this.length--;
    return this;
  }

Tal vez asi pueda servir tambien retorna el ultimo elemto y disminuye el nodo

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor() {
    this.top = null;
    this.bottom = null;
    this.length = 0;
  }

  peek() {
    return this.top;
  }

  push(value) {
    const newNode = new Node(value);

    if (this.length === 0) {
      this.top = newNode;
      this.bottom = newNode;
    } else {
      const holdingPointer = this.top;
      this.top = newNode;
      this.top.next = holdingPointer;
    }

    this.length++;

    return this;
  }

  pop() {
    if (this.length === 0) {
      this.top = null;
      this.bottom = null;
      this.length = 0;
      return null;
    }

    const outNode = this.top.value;
    this.top = this.top.next;
    this.length--;
    return outNode;
  }
}

const myStack = new Stack();
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.pop();
console.log(myStack);```

Aqui va mi solución 😃

Mi solución al pop 😎

class Node {
	constructor(value) {
		this.value = value;
		this.next = null;
	}
}

class Stack {
	constructor(value) {
		this.top = new Node(value);
		this.bottom = this.top;
		this.length = 1;

		return this;
	}

	peek() {
		return this.top;
	}

	push(value) {
		const newNode = new Node(value);
		const holdingPointer = this.top;
		this.top = newNode;
		this.top.next = holdingPointer;

		this.length++;
		return this;
	}

	pop() {
		this.top = this.top.next;

		this.length--;
		return this;
	}
}

const stack = new Stack(4);
stack.push(8);
stack.push(12);
stack.pop();

Mi solución

pop() {
    if (this.length > 0) {
      this.length--;
      if (this.length < 1) {
        this.top = null;
        this.bottom = null;
      } else {
        this.top = this.top.next;
      }
    }

    return this;
  }

mi aporte al reto:

pop(){
    if (this.length === 0){
        return 'empty stack'
    }

    if (this.length === 1){
        this.top = null
        this.bottom = null
        this.length--
        return this 
    }
    const newLastElement = this.top.next
    this.top = newLastElement
    this.length--
    return this 
}

adicionalmente hice validaciones en los otros métodos, el código completo quedaría de esta forma

class Node {
    constructor(value){
        this.value = value 
        this.next = null 
    }
}

class Stack {
    constructor(){
        this.top = null
        this.bottom = null
        this.length = 0
    }

    peek(){
        if (this.length === 0){
            return 'empty stack'
        }
        return this.top
    }

    push(value){
        const newNode = new Node(value)
        if (this.length === 0){
            this.top = newNode
            this.bottom = newNode
        } else {
            const holdingPointer = this.top
            this.top = newNode
            this.top.next = holdingPointer
        }

        this.length++
        return this 
    }

    pop(){
        if (this.length === 0){
            return 'empty stack'
        }

        if (this.length === 1){
            this.top = null
            this.bottom = null
            this.length--
            return this 
        }
        const newLastElement = this.top.next
        this.top = newLastElement
        this.length--
        return this 
    }
    
}

const MyStack = new Stack()

MyStack.push("1")
MyStack.peek()
MyStack.push("2")
MyStack.peek()
MyStack.push("3")
MyStack.peek()
MyStack.push("4")
MyStack.peek()
MyStack.pop()
MyStack.peek()
MyStack.pop()
MyStack.peek()
MyStack.pop()
MyStack.peek()
MyStack.pop()
MyStack.peek()

Mi solución:

class Node {
    constructor(value) {
        this.value = value;
        this.next  = null;
    }
}

class Stack {

    #top    = null;
    #bottom = null;
    #length = 0;


    constructor() {}

    peek() {
      	if (this.#length === 0) return null;
        return this.#top.value;
    }

    pop(value) {
      	if (this.#length === 0) return null;
        const nodo  = this.#top
        this.#top   = nodo.next
        this.#length--
      	return nodo.value
    }

    push(value) {
        const newNode = new Node(value);
      	newNode.next  = this.#top
      	this.#top     = newNode
        this.#length++
        return newNode.value
    }
}

Hola a todos, dejo mi solución del pop()

    pop() {

        if (this.top === this.bottom) {
            this.top = null;
            this.bottom = null;
        } else {
            const holdingPointer = this.top;
            this.top = this.top.next;
            this.top.next=null;
            this.length--;
            return holdingPointer;
        }
    }
class Node {
    constructor(value){
        this.value = value;
        this.next;
    }
}

class Stack {
    constructor(){
        this.top = null,
        this.bottom = null,
        this.length = 0
    }

    peek(){
        return this.top
    }

    push(value){
        const newNode = new Node(value);
        if(this.length === 0){
            this.top = newNode;
            this.bottom = newNode;
        }else{
        const holdingPointer = this.top;
        this.top = newNode;
        this.top.next = holdingPointer
        }
        this.length++;
        return this
    }

    pop(){

        if(this.length === 1){
            this.bottom = null
        }
        const ultimoElemento = this.top.next
        this.top = null;
        this.top = ultimoElemento;

        this.length--;
        return this


    }
}
const stack = new Stack();

Solución al reto:

class Node {
  constructor(value) {
    this.value = value;
    this.next = null; 
  }
}

class Stack {
  constructor() {
    this.top = null;
    this.bottom = null;
    this.length = 0;
  } 
  peek() {
    return this.top;
  }
  push(value) {
    const newNode = new Node(value);
    if (this.length === 0) {
      this.top = newNode;
      this.bottom = newNode;
    } else {
      const holdingPointer = this.top;
      this.top = newNode;
      this.top.next = holdingPointer;
    }

    this.length++;

    return this;
  }
  pop() {
    if (this.length === 0) {
      console.log('No hay elementos en la pila');
      return;
    } 
    if (this.length === 1) {
      this.top = null;
      this.bottom = null;
    } else {
      const holdingPointer = this.top.next;
      delete this.top;
      this.top = holdingPointer;
    }

    this.length--;

    return this;
  }
}

const myStack = new Stack();
pop(){
    if(this.length === 0){
        return null;
    }
    const node = this.top;
    if(this.length === 1){
        this.bottom = null;
    }
    this.top = this.top.next;
    this.length--;
    return node;
  }

hola aquí les dejo mi aporte espero les ayude

//son LIFOS

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }
    //metodos

    //selecionar
    peek(){
        return this.top;
    }
    //agregar elementos al final
    push(value){
        const CurrentNode = new Node(value);
        if(this.length === 0){
            this.top = CurrentNode;
            this.bottom = this.top;
        } else{
            const waitPointer = this.top;
            this.top = CurrentNode;
            this.top.next = waitPointer;
        }
        this.length++;
        return this;
    }
    //eliminar elementos al final
    pop(){
        const waitPointer = this.top;
        this.top = waitPointer.next;
        this.length--;
        return this;
    }


}

const myStack = new Stack();
console.log(myStack);
console.log(myStack.push(0));
console.log(myStack.push(1));
console.log(myStack.push(2));
console.log(myStack.peek());
console.log(myStack.push(3));
console.log(myStack.pop());

mi aporte de pop()

pop(){
        if(this.length === 1){
            this.top = null;
            this.bottom = null;
            this.length--;
            return this;
        }else{
            const holdingPointer = this.top.next;
            this.top = holdingPointer;
            this.length--;
            return this;
        }
    }

Comparto mi solución del metodo pop


  pop() {
    let ItemDeleted = this.#top;
    let previusNode = this.#bottom;

    if (this.length === 1) {
      this.#bottom = null;
      this.#top = null;
      return ItemDeleted;
    }

    for (let index = 1; index < this.length - 1; index++) {
      previusNode = previusNode.next;
    }

    previusNode.next = null;
    this.#top = previusNode;
    return ItemDeleted;
  }```

Hubo un error por parte del profesor, ya que al implementar el metodo push, this.top.next debia ser igual al null. Dejo mi solucion al metodo pop() en conjunto con una solucion al error dicho. Ademas, implemente el metodo print que imprime al stack en el formato de array tradicional.

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor() {
    this.top = null;
    this.bottom = null;
    this.length = 0;
  }
  peek() {
    return this.top;
  }
  push(value) {
    const newNode = new Node(value);
    if (this.length == 0) {
      this.bottom = newNode;
      this.top = newNode;
    } else {
      this.top.next = newNode;
      this.top = newNode;
    }
    this.length++;
    return this;
  }
  pop() {
    if (this.length == 0) {
      return "Stack is empty";
    }
    let node = this.top;
    if (this.length == 1) {
      this.top = null;
      this.bottom = null;
      this.length = 0;
    } else {
      var current = this.bottom;
      for (let i = 0; i < this.length - 2; i++) {
        current = current.next;
      }
      current.next = null;
      this.top = current;
    }
    this.length--;
    return node;
  }
  print() {
    let current = this.bottom;
    let stack = [];
    while (current != null) {
      stack.push(current.value);
      current = current.next;
    }
    console.log(stack);
  }
}

Mi solución

buenas noches,

comparto reto:

class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor() {
        this.top = null; // elemento de arriba
        this.bottom = null; // elemento de abajo
        this.lenght = 0;
    }

    // toma el ultimo elemento agregado
    peek() {
        return this.top;
    }

    // agregar elemento al final
    push(value) {
        const newNode = new Node(value);
        if (this.lenght === 0) {
            this.top = newNode;
            this.bottom = newNode;
        } else {
            const holdingPointer = this.top;
            this.top = newNode;
            this.top.next = holdingPointer;
        }

        this.lenght++;
        return this;
    }

    // eliminar ultimo elemento
    pop() {
        const holdingPointer = this.top.next;
        const holdingBottom = this.bottom;
        delete this.bottom;
        this.bottom = holdingPointer;
        this.lenght--;
        return holdingBottom;
    }
}

Gracias.

Se agrega el método pop para eliminar el último en entrar al stack, el top del stack

class Node{
    constructor(value){
        this.value = value;
        this.next =  null;
    }
}

class Stack{
    constructor(){
        this.top = null;
        this.botton = null;
        this.lenght = 0;
    }
    peek(){//seleccionar el elemento de arriba
        if(this.lenght === 0){  
            return this;
        }else{
            return this.top;
        }
    }
    push(value){//agregar ultimo
         const newNode = new Node(value);
         if(this.lenght === 0){  
            this.top = newNode;
            this.botton = newNode;
         }else{
            const holdingPointer = this.top;
            this.top = newNode;
            this.top.next = holdingPointer;
         }
         this.lenght++;
         return this;
    }
    pop(){//eliminar ultimo
        if(this.lenght !== 0){   
            const nextNode = this.top.next;
            this.top = nextNode;
            this.lenght--;
        }
        return this;
    }
}

var miStack = new Stack();
miStack.push('Nueva Tarea');
miStack.peek();
miStack.push('Nueva Tarea2');
miStack.push('Nueva Tarea3');
miStack.pop();```
class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor() {
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }

    peek() {
        return this.top;
    }

    push(value) {
        const newNode = new Node(value);
        if (this.length === 0) {
            this.top = newNode;
            this.bottom = newNode;
        } else {
            newNode.next = this.top;
            this.top = newNode;
        }
        this.length++;
        return this;
    }

    pop() {
        if (this.length === 0) {
            return undefined;
        }
        const removedNode = this.top;
        if (this.length === 1) {
            this.top = null;
            this.bottom = null;
        } else {
            this.top = removedNode.next;
        }
        this.length--;
        return removedNode;
    }
}
pop() {
  if (this.length === 0) {
    return
  } else if (this.length === 1) {
    this.top = null;
    this.bottom = null;
  } else {
    this.top = this.top.next;
  }

  this.length --;
  return this;
}	

Mi código para el método pop( )

pop(){
        if (this.length == 0) {
            console.log("Objeto vacio");
            return this;
        }
        const holdingNode = this.top.next;
        this.top = holdingNode;
        this.length --;
        return this;
    }

Al fin uno que resuelvo en poco tiempo jaja.

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class Stack{
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }

    peek(){
        return this.top;
        
    }

    push(value){
        const point = new Node(value);

        if (this.length === 0){
            this.top = point;
            this.bottom = point;
        }else{
            const lastTop = this.top;
            this.top = point;
            this.top.next = lastTop;
        }

        this.length ++;

        return this;
    }

    pop(){
        const newTop = new Node(this.top.next);
        const newNext = this.top.next;

        this.top =newTop;
        this.top.next = newNext;


       this.length --;
       return this;
    }
}

let myStack = new Stack();

myStack.push(2);
myStack.push(6);
myStack.push(8);
myStack.push(4);


myStack.pop();


<code> 
    public pop(){
        if(this.length === 0){
            console.error("cannot pop last element if Stack is empty")
        }else{
            if (this.top){
                if(this.top.previous){
                    let newTopNode : StackNode = this.top.previous
                    this.top = newTopNode
                    if(!this.top.previous){
                        this.bottom = this.top
                    }
                    this.length = this.length - 1
                }
            }else{
                console.error("there is no top or top.previous to make the new top")
            }
        }
    }

Aquí les comparto mi solución del método pop()

	pop() {
		if (this.length === 0) {
			return "pila vacía.";
		} else {
			this.top = this.top.next;
		}
		this.length--;
		return this;
	}

my pop :😃

pop()
    {
        if(this.lenght > 0)
        {

            this.top = this.top.next;
            if(this.lenght === 1)
            {
                this.bottom = null; 
            }
            this.lenght--;
            
            return this;
        }
        else
        {
            return new Error('stack is empty');
        }
        
    }

My version of pop() stack function:

  pop(){
    if (this.length === 0) {
      throw new Error("stack is already empty");
    }
    if (this.length === 1) {
      this.top = null;
      this.bottom = null;
      this.length--;
      return this;
    }
    this.top = this.top.next;
    this.length--;
    return this;
  }

Aqui dejo mi solución al metodo pop

pop()
  {
    if(this.length <= 0) return 'No hay elementos en el stack' // Primero verifico si hay elementos en el stack 
    const view = this.top
    this.top = this.top.next
    this.length--
    return view
  }
pop(){
    if(this.length){
        if(this.top.next){
            this.top = this.top.next;
        }else{
            this.top = null;
            this.bottom = null;
        }
        this.length--;
    }
    return this;
}
  pop() {
    if (this.length === 1) {
      this.top = null;
      this.bottom = null;
    } else {
      this.top = this.top.next;
    }

    this.length--;

    return this;
  }

Aquí mi código con el método de pop:

class Node{
	constructor(value){
		this.value = value;
		this.next = null;
	}
}
class Stack {
	constructor(){
		this.top = null;
		this.bottom = null;
		this.length = 0;
	}

	peek(){
		return this.top; //Se hace referencia sobre el stack

	}

	push(value){
		const newNode= new Node(value);
		if(this.length === 0)	{
			this.top = newNode;
			this.bottom = newNode

		}	else	{
			const holdingPointer = this.top;
			this.top = newNode;
			this.top.next = holdingPointer;
		}
			this.length++

			return this;
	}

	pop(){
		const holdingPointer = this.top.next;
		delete this.top;
		this.top = holdingPointer;
		this.length--
	}
}

Mi código un poco más simplificado:

class Node {
  constructor(value) {
    this.value = value
    this.next = null
  }
}

class Stack {
  constructor() {
    this.top = null
    this.bottom = null
    this.length = 0
  }
  peek() {
    return this.top
  }
  push(value) {
    const newNode = new Node(value)
    if (!this.top) { //Si no hay un "top" entonces el nuevo nodo es el primer "top" y el "bottom" al mismo tiempo
      this.bottom = newNode
    }
    newNode.next = this.top // En el caso que del primer nodo el "next" sería igual al "top" de ese momento, es decir a "null".  Para los siguientes nodos, el "next" apunta al nodo "top" de ese momento, y luego se asigna el "top" al nuevo nodo.
    this.top = newNode
    this.length++
    return this
  }
  pop() {
    if (!this.top) { // Si no hay un "top", entonces no puede remover ningún nodo
      return null
    }
    this.top = this.top.next // Con redefinir el "top" al "next" de el mismo, se elimina el nodo automáticamente.
    if (this.top === null) { // Si el "top" llega a ser "null", entonces el "bottom" también debe ser null
      this.bottom = null
    }
    this.length--
    return this // Retorno todo el stack para pruebas.  Si se quiere devolver el nodo eliminado hay que guardarlo en una variable antes de reasignar el "top".
  }
}

const myStack = new Stack()
myStack.push(1)
myStack.push(2)
myStack.push(3)
myStack.peek()
myStack.pop()
myStack.pop()
myStack.pop()

Mí método pop():

pop(){
        if(this.length === 0){
            console.log('no items to delete')
            return
        }
        const holdingPointer = this.top.next
        this.top = holdingPointer
        this.length--
        return this
    }

En el código ya está agregado el reto del curso, el método pop 😄

  class Node {
    constructor(value) {
      this.value = value;
      this.next = null;
    }
  }

  class stack {
    constructor() {
      this.top = null;
      this.bottom = null;
      this.length = 0;
    }
    peek() {
      return this.top
    }
    pop() {
      const getout = this.top;
      this.top = this.top.next;
      getout.next = null
      this.length--
      return getout;
    }
    push(value) {
      const element = new Node(value);
      if (this.length == 0) {
        this.top = element;
        this.bottom = element;
      } else {
        element.next = this.top;
        this.top = element
      }
      this.length++
      return this;
    }
  }

  const myStack = new stack();
  myStack.push(2);
  myStack.push(32);
  myStack.push(43);
  myStack.push(3);
  myStack.push(4);
  myStack.push(1);
  myStack.push(54);
  myStack.push(12);
  myStack.push(44);
  myStack.push(7);
  myStack.push(6);

  //COMANDOS PARA PROBAR

  myStack.peek();
  myStack.pop();

Codigo bomnito

//Simple stack implementation
//@method push: pushes an element to the stack
//@method pop: pops an element from the stack
//@method peek: returns the top element of the stack
//@method print: prints the stack
const Node = require("../node");

class Stack {
    constructor() {
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }

    peek() {
        return this.top;
    }
    push(value) {
        const newNode = new Node(value);

        if (this.length === 0) {
            this.top = newNode;
            this.bottom = newNode;
        } else {
            const lastTop = this.top;
            this.top = newNode;
            this.top.next = lastTop;
        }
        this.length++;
        return this;
    }

    pop() {
        if (this.length === 0) return undefined;
        const lastTop = this.top;
        this.top = this.top.next;
        this.length--;
        if (this.length === 0) {
            this.bottom = null;
        }
        return lastTop;

    }




    print() {
        let current = this.top;
        while (current) {
            console.log(current.value);
            current = current.next;
        }
    }
}


module.exports = Stack;

Método Pop!

    pop() {
        const holdingPointer = this.peek();;

        if (this.length > 1) {
            this.top = this.top.next;

            holdingPointer.next = null;
        } else {
            this.top = null;
            this.bottom = null;
        }

        if (this.length > 0) {
            this.length--;
        }

        return holdingPointer;
    }

Mi método pop.

pop(){
    const nodeToDelete = this.top;
    const nodeToDeleteValue = nodeToDelete.value;
    if(this.length == 1){
        this.top = null;
        this.bottom = null;
    }else{
        this.top = this.top.next; 
        nodeToDelete.next = null;
    }
    
    this.length--;
    return nodeToDeleteValue;
}

Les comparto como quedó el método pop utilizando una pequeña validación sobre cuantos elementos existen en el stack

 pop(){
        if(this.length === 0) {
            return this
        } else if(this.length === 1) {
            delete this.last
            delete this.first
            this.length-=1
        } else {
            const newLast = this.last.next  
            delete this.last
            this.last = newLast 
            this.length-=1
        }

        return this

Esta es mi solución a la función pop()

pop() {
	if (this.length === 1) {
		this.top = null;
		this.bottom = null;
	} else if(this.length > 1) {
		const removeTheLastPointer = this.top.next;
		this.top = removeTheLastPointer;
	} else {
		return this;
	}

	this.length--;
	return this;
}

Que tal campeon, buen dia.
comparto mi codigo.

class Node {
  constructor(value){
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor(){
    this.top = null;
    this.bottom = null;
    this.length = 0;
  }
  push(value){
    const newNode = new Node(value);
    if (value === undefined) {
      return null
    }
    if(!this.length){
      this.top = newNode;
      this.bottom = newNode;
    } else {
      newNode.next = this.top;
      this.top = newNode;
    }
    this.length++;

    return this;
  }
  pop() {
    let message = {Error: 'The Stack is Empty'};
    const stackList  = this.top;
    
    if (this.length === 0) {
      throw new Error(message.Error);
    }
    if (this.length === 1) {
      this.top = null;
      this.bottom = null;
    } else {
      this.top = this.top.next;
    }
    this.length--;

    return stackList;
  }
  peek() {
    return this.top;
  }
  getStackList() {
    let currentList = this.top;
    let stack = '';

    for (let i = 0; i < this.length; i++) {
      stack += `${currentList.value}${i === this.length - 1 ? '' : ' '}`;
      currentList = currentList.next    
    }

    return stack;
  }
}

const myStackList = new Stack();

Así me quedo el método pop

pop(){
    if(this.length === 0){
        console.error("Este stack no tiene elementos")
        return this
    }else if(this.length === 1){
        this.bottom = null
        this.top = null
    }else{
        this.top = this.top.next
    }
    this.length--
    return this
}

He aquí mi solucion para el método de pop()

class Node {
	constructor(value) {
		this.value = value;
		this.next = null;
	}
}

class Stack {
	constructor() {
		this.top = null;
		this.bottom = null;
		this.length = 0;
	}
	peek() {
		return this.top;
	}
	push(value) {
		const newNode = new Node(value);
		if (this.length === 0) {
			this.top = newNode;
			this.bottom = newNode;
		} else {
			const holdingPointer = this.top;
			this.top = newNode;
			this.top.next = holdingPointer;
		}

		this.length++;
		return this;
	}
	pop() {
		if (this.length === 0) {
			console.error("this stack it's already empty");
			return this;
		} else if (this.length === 1) {
			this.top = null;
			this.bottom = null;
			this.length--;
			return this;
		} else {
			const newTop = this.top.next
			this.top = newTop;
			this.length--;
			return this;
		}
	}
}

const myStack = new Stack();

Les comparto mi código de toda la implementación de la pila. Cualquier corrección o mejora es bienvenida.

class Node{
  constructor(value){
    this.value = value;
    this.next = null;
  }
}

class Stack{
  constructor(){
    this.top = null;
    this.bottom = null;
    this.length = 0;
  }
  peek(){
    return this.top;
  }
  push(value){
    const newNode = new Node(value);
    if (this.length == 0) {
      this.top = newNode;
      this.bottom = newNode;
    } else {
      newNode.next = this.top;
      this.top = newNode;
    }
    this.length++;
    return this;
  }
  pop(){
    if (this.length === 0) 
      throw new Error ("The Stack is empty"); 

    const topNode = this.top;
    if (this.length === 1){
      this.top = null;
      this.bottom = null;
    }else {
      this.top = this.top.next
    }
    this.length--;
    return topNode;
  }
}

Estas son las pruebas que le hice.

let myStack = new Stack();
console.log(myStack.push(1));
console.log(myStack.push(2));
console.log(myStack.push(3));
console.log(myStack.push(4));
console.log(myStack.peek());
console.log(myStack.peek());
console.log(myStack.pop());

Aquí mi ejercicio completo, me ahorré un paso en e método push y en el método pop utilice un switch para hacer las validaciones.

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}
class Stack{
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }
    peek(){        
        return this.top;        
    }
    push(value){
        const newNode = new Node(value);
        if(this.length === 0){
            this.bottom = newNode;
            this.top = newNode;
        }else{            
            this.top.next = newNode;
            this.top = newNode;
        }   
        this.length++;     
        return this;
    }
    pop(){
        let pointer = this.bottom;
    switch (this.length) {
        case 0:
            return this;
        case 1:
            this.bottom = null;
            this.top = null;
            this.length--;
            return this;
        default:
            while(pointer.next.next){
                pointer = pointer.next;
            }
            pointer.next = null;
            this.top = pointer;
            this.length--;
            return this;
    }        
    }
}
const myStack = new Stack();
myStack.push(1)
myStack.push(2)

Este es mi aporte del metodo pop()

  pop() {
    this.top = this.top.next;
    if (this.length === 1) {
      this.bottom = null;
    }
    this.length--;
    return this;
  }

Mi solución para el método pop():

    pop() {

        if (this.lenght >= 0) {
            const nodePreviousTop = this.top.next;
            this.top = nodePreviousTop;
        } else {
            return 'Index out of bounds range.';
        }
        
        this.lenght--;

        return this;
    }

Les comparto mi implementación del Stack:

class Node {
  constructor(value) {
    this.value = value
    // In this case the next node is the one which is bellow
    this.next = null
  }
}

class Stack {
  constructor() {
    this.top = null
    this.bottom = null
    this.length = 0
  }

  peek() {
    return this.top
  }

  push(value) {
    const node = new Node(value)
    this.length++

    if (this.length === 0) {
      this.top = node
      this.bottom = node

      return
    }

    const currentTop = this.top
    this.top = node
    this.top.next = currentTop
  }

  pop() {
    if (this.length === 0) throw new Error('Nothing to pop')

    this.top = this.top.next
    this.length--
  }

  toString() {
    let current = this.top
    let stack = ''

    for (let i = 0; i < this.length; i++) {
      stack += `${current.value}${i === this.length - 1 ? '' : '\n'}`
      current = current.next
    }

    return stack
  }
}

es demasiado satisfactorio cuando pienso una lógica (En este caso el metodo pop(),) y me funciona. que genial! gracias Diego.

este es un stack de nodos pero puede ser con array u otro tipo de estructura

class Node {
  constructor(value){
    this.value = value
    this.next = null
  }
}
class Stack {
  constructor(){
    this.stack = null
    this.length = 0
  }
  peek(){
    return this.top
  }
  push(value){
    const node = new Node(value)
    if (this.length == 0) this.stack = node
    else {
      node.next = this.stack
      this.stack = node
    }
    this.length++
  }
  pop(){
    if (!this.length) return 'El stack esta vacio'
    this.stack = this.stack.next
    this.length--
  }
}

let stack = new Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.pop()
stack.pop()
stack.pop()
stack.pop()
stack.pop()
// stack.pop()



stack

Mi método pop:

pop() {
    if (this.length !== 0) {
        this.top = this.top.next;
        this.length -= 1;
        if (this.length === 0) {
            this.bottom = null;
        }
    }
    return this;
}

😊

pop() {
        let pointer = this.top.next;

        delete this.top;

        this.top = pointer;

        this.length--;
        return this;
    }
class Node {
    constructor(value) {
        this.value = value 
        this.next = null
    }
}

class Stack {
    constructor(){
        this.top = null
        this.bottom = null
        this.length = 0
    }

    peek(){
        return this.top
    }

    push(value){
        let newNode = new Node(value)
        if(this.length === 0){
            this.bottom = newNode
        }
        else{
            this.top.next = newNode
        }
        this.top = newNode
        this.length++
        return this
    }

    pop(){
        if(this.top === null){
            return this
        }
        const nodeToPop = this.top
        let currentNode = this.bottom
        while(currentNode.next !== this.top){
            currentNode = currentNode.next
        }
        currentNode.next = null
        this.top = currentNode
        this.length--
        return nodeToPop
    }
}

const myStack = new Stack()
myStack.push(1)
myStack.push(2)
myStack.push(3)
myStack.pop()
myStack.peek()

Mi método pop

  pop() {
    let item = {};
    if (this.length === 0) {
      return null;
    }
    if (this.length === 1) {
      item = this.top;
      this.top = null;
      this.bottom = null;
      this.length = 0;
    } else {
      item = this.top;
      this.top = item.next;
      this.length--;
    }

    return item;
  }

Hice las clases basandome en la logica que se utilizo con la linkedList … En teoria funciona

class Node{
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor() {
        this.stack = null;
        this.length = 0;
    }

    pop(){
        const last = this.stack.next;
        this.stack = last;
        this.length--;
        return this;
    }

    push(value){
        const node = new Node(value);
        node.next = this.stack;
        this.stack = node;
        this.length++;
        return this;
    }

    peek(){
        return this.stack.value;
    }
}

module.exports = Stack;

Mi pop()

    pop(){
        if (this.length > 0){
            this.top = this.top.next;
            this.length--;
            return this;
        }
    }

Mi solución al reto:

pop() {
	if (this.length === 0) {
		console.log('No hay elementos')
	} else if (this.length === 1) {
		this.top = null
		this.bottom = null
		this.length -= 1
	} else {
		const newTop = this.top.next
		this.top = newTop
		this.length -= 1
	}
	return this
}

Les comparto mi solución al reto, le agregue algunas condiciones para que no cause error cuando se ejecutan algunos métodos al estar vacio el stack.

class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor() {
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }
    peek() {
        return (this.top != null) ? this.top.value : this.top;
    }
    push(value) {
        const newNode = new Node(value)
        if (this.length === 0) {
            this.top = newNode;
            this.bottom = this.top;
        } else {
            newNode.next = this.top
            this.top = newNode;
        }
        this.length++;
        return this;
    }
    pop() {
        if (this.length === 0) {
            return undefined;
        }
        if (this.length === 1) {
            this.bottom = null
        }
        const removedTop = this.top;
        const newTop = this.top.next;

        removedTop.next = null;
        this.top = newTop;

        this.length--;
        return removedTop.value;
    }
    print() {
        if (this.length === 0) {
            console.log("This stack is empty");
        }
        let probe = this.top;
        for (let i = 0; i < this.length; i++) {
            console.log(probe.value);
            probe = probe.next
        }
    }
}

El reto

pop() {
        if(this.length === 0) {
            this.top = null;
            this.bottom = null;
        } else {
            this.top = this.top.next
        }
        this.length--;
        return this
    }```
class Node {
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class Stack {
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    }

    peek(){
        return this.top;
    }

    push (value){
        const newNode = new Node(value);

        //validacion

        if (this.length === 0) {
            this.top = newNode;
            this.bottom = newNode;
        }else {
            const holdingPointer = this.top;
            this.top = newNode;
            this.top.next = holdingPointer;
        }

        this.length++;

        return this;
    }

    pop(){
        if (!this.length){
            console.error("No nodes in the stack");
            return;
        }

        const topNode = this.top;

        if(this.length === 1){
            this.top = null;
            this.bottom = null;
        }else {
            this.top = this.top.next;
        }
        this.length--;
        return topNode;
    }
}

const myStack = new Stack();

Aquí esta mi código del desafío, espero sus feedbacks guys.

pop() {
      if (this.length <= 1) {
        console.log("You haven't elements anymore.");
        this.bottom = null;
        this.top = null;
      } else {
        this.top = this.top.next;
        if (this.length === 1) {
          this.top = this.bottom;
          this.top = this.top.next;
        }
      }
      this.length--;
      return this;
    }

Así queda mi actividad con el método pop() 😄

pop() {
    const node = this.top;

    if (this.length === 0) {
      return "Error: Stack empty.";
    } else if (this.length === 1) {
      this.top = node.next;
      this.bottom = null;
      this.length--;
    } else {
      this.top = node.next;
      this.length--;
    }

    return this;
  }

Yo realize dos validaciones, si no existe ningun nodo y si solo existe uno solo, Saludos ;p

class Stack {
   constructor() {
      this.top = null;
      this.bottom = null;
      this.length = 0;
   }

   peak() {
      return this.top;
   }

   push( value ) {
      const newNode = new Node( value );

      if(this.length === 0) {
         this.top = newNode;
         this.bottom = newNode;
      } else {
         const holdingPointer = this.top;
         this.top = newNode;
         this.top.next =  holdingPointer;
      }

      this.length++;

      return this;
   }

   pop() {
      if( this.length === 0 ) {
         return this;
      }
      if( this.length === 1 ) {
         this.top = null;
         this.bottom = null;
         this.length = 0;
      } else {
         const holderPointer = this.top;
         this.top = holderPointer.next;
         this.length--;
      }
   return this;
   }
}

Implementación pero en Kotlin, saludos!
Stack.kt

pop() {
        if(this.length === 0) {
            return  `No hay datos en el stack`;
        } else if(this.length === 1) {
            this.top = null;
            this.bottom = null;
        } else {
            this.top = this.top.next;
        }
        this.length--;
        return this;
}

Comparto mi solución al reto 😄 Lo hice recorriendo el stack, no sé si esta sea la mejor forma, pero funcionó xd

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor() {
    this.top = null;
    this.bottom = null;
    this.length = 0;
  }

  push(value) {
    const newNode = new Node(value);

    if (!this.bottom) {
      this.bottom = newNode;
      this.top = newNode;
      this.length++;

      return this;
    }

    this.top.next = newNode;
    this.top = newNode;

    this.length++;

    return this;
  }

  peek() {
    return this.top;
  }

  walkStack() {
    let counter = 0;
    let length = this.length - 2;
    let currentNode = this.bottom;

    while (counter !== length) {
      currentNode = currentNode.next;
      counter++;
    }

    return currentNode;
  }

  pop() {
    if (this.length === 0) {
      return console.log("El Stack ya está vacio");
    }

    if (this.length === 1) {
      const nodeToPop = this.top;
      this.top = null;
      this.bottom = null;
      this.length--;
      return nodeToPop;
    }

    const nodeToPop = this.top;
    const newTop = this.walkStack();
    this.top = newTop;

    this.length--;

    return nodeToPop;
  }
}

![](

Desafío terminado!!

**Metodo pop**
pop() {
        if(this.length == 0) {
            this.top = null;
            this.bottom = null;
        } else {
            this.top = this.top.next;

            this.length--;

            return this
        }
    }
}

Este es mi parte

pop(){
        switch (this.length) {
            case 0:
                return console.log('there isn´t stack')
            case 1:
                this.top = null;
                this.bottom = null;
                break;
            default:
                this.top = this.top.next;
                break;
        }
        this.length--;
        return this;
    }
pop() {
    if (!this.top) {
      return null;
    }
    if (this.top === this.bottom) {
      this.bottom = null;
    }
    this.top = this.top.next;
    this.length--;

    return this;
  }```

Implementacion del metodo pop():

  pop(){
    const prevTop = this.top;
    if(prevTop.next){
      this.top = this.top.next;
    } else {
      this.top = null;
      this.bottom = null;
    }
    this.length--;
    return prevTop;
  }

Ahí dejo mi implementación del método pop:

    pop( ) {

        const topNode = this.top

        if( this.length > 0){
            this.top = this.top.next
            this.length -= 1
        } 
        if( this.length === 0)
            this.bottom = null
        
        return topNode
    }

Como curiosidad podéis probar lo que pasa si se cambia el orden en la primera línea del if:

    pop( ) {

        const topNode = this.top

        if( this.length > 0){
            this.top.next = this.top
            this.length -= 1
        } 
        if( this.length === 0)
            this.bottom = null
        
        return topNode
    }
pop() {
    if (this.length === 0) {
      console.error('Empty Stack')
      return;
    }
    const topNode = this.top;
    this.top = this.top.next;
    this.length--;
    return topNode; 
  }

Así implementé el método pop

	pop() {
		if (this.length === 0) {
			return this;
		}

		this.top = this.top.next;
		this.length--;

		if (this.length === 0) {
			this.bottom = null;
		}
		return this;
	}