class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
append(data) {
let newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
}
printList() {
let current = this.head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
}
}
let list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.printList(); // Output: 10 20 30
A stack is a LIFO (Last In, First Out) data structure. The most recent element added is the first one to be removed.