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 鈥淭op鈥 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;
	}