Files
2022-04-01 02:01:59 +09:00

4.8 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d825a367417b2b2512c87 Creare una lista doppiamente concatenata 1 301626 create-a-doubly-linked-list

--description--

Tutte le liste concatenate che abbiamo creato finora sono liste semplicemente concatenate. Qui, creeremo una lista doppiamente concatenata. Come suggerisce il nome, i nodi in una lista doppiamente concatenata hanno dei riferimenti ai nodi successivo e precedente nella lista.

Questo ci permette di attraversare la lista in entrambe le direzioni, ma richiede anche più memoria perché ogni nodo deve contenere un riferimento aggiuntivo al nodo precedente nella lista.

--instructions--

Abbiamo fornito un oggetto Node e abbiamo avviato il nostro DoublyLinkedList. Aggiungiamo due metodi alla nostra lista doppiamente concatenata chiamati add e remove. Il metodo add dovrebbe aggiungere l'elemento dato alla lista mentre il metodo remove dovrebbe rimuovere tutte le occorrenze di un dato elemento nella lista.

Fai attenzione a gestire eventuali casi limite durante la scrittura di questi metodi, come le cancellazioni per il primo o l'ultimo elemento. Inoltre, la rimozione di qualsiasi elemento in una lista vuota dovrebbe restituire null.

--hints--

La struttura dei dati DoublyLinkedList dovrebbe esistere.

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

Il DoublyLinkedList dovrebbe avere un metodo denominato add.

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

Il DoublyLinkedList dovrebbe avere un metodo denominato remove.

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

Inoltre, la rimozione di qualsiasi elemento da una lista vuota dovrebbe restituire null.

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

Il metodo add dovrebbe aggiungere elementi alla 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';
  })()
);

Ogni nodo dovrebbe tenere traccia del nodo precedente.

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';
  })()
);

Il primo elemento dovrebbe essere rimovibile dall'elenco.

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';
  })()
);

L'ultimo elemento dovrebbe essere rimovibile dall'elenco.

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