gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
sto preparando un esame di informatica su JS e TS, dammi alcuni esercizi sulle linked list
- User
sto preparando un esame di informatica su JS e TS, dammi alcuni esercizi sulle linked list
- Assistant
sto preparando un esame di informatica su JS e TS, dammi alcuni esercizi sulle linked list
Sicuramente! Le linked list (liste concatenate) sono una struttura dati fondamentale e comprenderle bene in JavaScript (JS) e TypeScript (TS) può aiutarti molto nell'approfondimento delle tue conoscenze in informatica. Di seguito trovi alcuni esercizi che puoi provare a implementare:
Node che rappresenta un nodo della lista.SinglyLinkedList con metodi per aggiungere, rimuovere e trovare nodi.// JavaScript
class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
}
insertFirst(data) {
this.head = new Node(data, this.head);
}
// Altri metodi come remove, find, etc...
}
// TypeScript
class Node<T> {
constructor(public data: T, public next: Node<T> | null = null) {}
}
class SinglyLinkedList<T> {
private head: Node<T> | null = null;
insertFirst(data: T): void {
this.head = new Node(data, this.head);
}
// Altri metodi come remove, find, etc...
}
Aggiungi un Metodo size()
size() alla tua classe SinglyLinkedList per calcolare il numero di nodi nella lista.Cerca un Nodo
find(data) che restituisce il nodo con il valore specificato.DoublyNode con puntatori sia al nodo successivo che a quello precedente.insertFirst, remove, find, etc.) per la DoublyLinkedList.// JavaScript
class DoublyNode {
constructor(data, next = null, prev = null) {
this.data = data;
this.next = next;
this.prev = prev;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
insertFirst(data) {
const newNode = new DoublyNode(data, this.head);
if (this.head) {
this.head.prev = newNode;
} else {
this.tail = newNode;
}
this.head = newNode;
}
}
// TypeScript
class DoublyNode<T> {
constructor(
public data: T,
public next: DoublyNode<T> | null = null,
public prev: DoublyNode<T> | null = null
) {}
}
class DoublyLinkedList<T> {
private head: DoublyNode<T> | null = null;
private tail: DoublyNode<T> | null = null;
insertFirst(data: T): void {
const newNode = new DoublyNode(data, this.head);
if (this.head) {
this.head.prev = newNode;
} else {
this.tail = newNode;
}
this.head = newNode;
}
}
Rendi la Lista Circolare
Rovescia una Linked List
reverse() per invertire l'ordine dei nodi nella SinglyLinkedList.// JavaScript
class SinglyLinkedList {
// ...metodi esistenti...
reverse() {
let prev = null;
let current = this.head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
}
}
// TypeScript
class SinglyLinkedList<T> {
// ...metodi esistenti...
reverse(): void {
let prev: Node<T> | null = null;
let current = this.head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
}
}
Rimuovi Duplicati
Unisci due Liste Concatenate
merge per unire due SinglyLinkedList in una sola.Trova l'elemento centrale di una lista concatenata
SinglyLinkedList utilizzando l'approccio del "punteggio lento e veloce".Spero che questi esercizi ti possano aiutare a prepararti per il tuo esame! Buono studio!