elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  Ayuda con JS
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda con JS  (Leído 1,180 veces)
betast

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Ayuda con JS
« en: 29 Abril 2019, 01:46 am »

Alguien me puede explicar este código, es de javascript

Código
  1. class Node{
  2.    constructor(val){
  3.        this.val = val;
  4.        this.next = null;
  5.    }
  6. }
  7.  
  8. class SinglyLinkedList{
  9.    constructor(){
  10.        this.head = null;
  11.        this.tail = null;
  12.        this.length = 0;
  13.    }
  14.    push(val){
  15.        var newNode = new Node(val);
  16.        if(!this.head){
  17.            this.head = newNode;
  18.            this.tail = this.head;
  19.        } else {
  20.            this.tail.next = newNode;
  21.            this.tail = newNode;
  22.        }
  23.        this.length++;
  24.        return this;
  25.    }
  26.    pop(){
  27.        if(!this.head) return undefined;
  28.        var current = this.head;
  29.        var newTail = current;
  30.        while(current.next){
  31.            newTail = current;
  32.            current = current.next;
  33.        }
  34.        this.tail = newTail;
  35.        this.tail.next = null;
  36.        this.length--;
  37.        if(this.length === 0){
  38.            this.head = null;
  39.            this.tail = null;
  40.        }
  41.        return current;
  42.    }
  43.    shift(){
  44.        if(!this.head) return undefined;
  45.        var currentHead = this.head;
  46.        this.head = currentHead.next;
  47.        this.length--;
  48.        if(this.length === 0){
  49.            this.tail = null;
  50.        }
  51.        return currentHead;
  52.    }
  53. }
  54.  
  55.  
  56. var list = new SinglyLinkedList()
  57. list.push("HELLO")
  58. list.push("GOODBYE")
  59. list.push("!")
  60.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines