Files
freeCodeCamp/curriculum/challenges/espanol/10-coding-interview-prep/data-structures/create-a-doubly-linked-list.md
2022-09-15 11:30:53 -07:00

4.6 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d825a367417b2b2512c87 Crear una Lista Doblemente Enlazada 1 301626 create-a-doubly-linked-list

--description--

Todas las listas enlazadas que hemos creado son listas simplemente enlazadas. Aquí, crearemos una lista doblemente enlazada. Como el nombre lo implica, los nodos en una lista doblemente enlazada tiene referencias al siguiente y anterior nodo en la lista.

Esto nos permite recorrer la lista en ambas direcciones pero también requiere usar más memoria ya que cada nodo debe contener una referencia adicional al nodo anterior en la lista.

--instructions--

Hemos proporcionado un objeto Node e iniciado nuestra DoublyLinkedList. Vamos a agregar dos métodos para nuestra lista doblemente enlazada llamados add y remove. El método add debe agregar el elemento dado a lista mietnra que el método remove debe eliminar todas las ocurrencias de un elemento dado en la lista.

Ten cuidado de manejar cualquier caso de arista cuando escribas estos métodos, tales como la eliminación para el primer o el último elemento. Además, eliminar cualquier elemento de una lista vacía debe devolver null.

--hints--

La estructura de datos DoublyLinkedList debe existir.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    return typeof test == 'object';
  })()
);

La DoublyLinkedList debe tener un método llamado add.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    if (test.add == undefined) {
      return false;
    }
    return typeof test.add == 'function';
  })()
);

La DoublyLinkedList debe tener un método llamado remove.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    if (test.remove == undefined) {
      return false;
    }
    return typeof test.remove == 'function';
  })()
);

Eliminar un elemento de una lista vacía debe devolver null.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    return test.remove(100) == null;
  })()
);

El método add debe agregar elementos a la lista.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    test.add(5);
    test.add(6);
    test.add(723);
    return test.print().join('') == '56723';
  })()
);

Cada node debe mantener un puntero al nodo anterior.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    test.add(50);
    test.add(68);
    test.add(73);
    return test.printReverse().join('') == '736850';
  })()
);

El primer elemento debe ser extraíble de la lista.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    test.add(25);
    test.add(35);
    test.add(60);
    test.remove(25);
    return test.print().join('') == '3560';
  })()
);

El último elemento debe ser extraíble de la lista.

assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    test.add(25);
    test.add(35);
    test.add(60);
    test.remove(60);
    return test.print().join('') == '2535';
  })()
);

--seed--

--after-user-code--

DoublyLinkedList.prototype = Object.assign(
  DoublyLinkedList.prototype,
  {

  print() {
    if (this.head == null) {
      return null;
    } else {
      var result = new Array();
      var node = this.head;
      while (node.next != null) {
        result.push(node.data);
        node = node.next;
      };
      result.push(node.data);
      return result;
    };
  },
  printReverse() {
    if (this.tail == null) {
      return null;
    } else {
      var result = new Array();
      var node = this.tail;
      while (node.prev != null) {
        result.push(node.data);
        node = node.prev;
      };
      result.push(node.data);
      return result;
    };
  }
});

--seed-contents--

var Node = function(data, prev) {
  this.data = data;
  this.prev = prev;
  this.next = null;
};
var DoublyLinkedList = function() {
  this.head = null;
  this.tail = null;
  // Only change code below this line

  // Only change code above this line
};

--solutions--

// solution required