1
0
Fork 0
computer-science-in-javascript/src/usage.js

83 lines
2.6 KiB
JavaScript

import {
CircularDoublyLinkedList,
DoublyLinkedList,
LinkedList,
} from "./structures/index.js";
function useLinkedList () {
console.log('-----------------------------------------------------------------------------------------------------');
console.log('Linked List');
console.log('-----------------------------------------------------------------------------------------------------');
const list = new LinkedList();
list.add("red");
list.add("orange");
list.add("yellow");
// get the second item in the list
console.log('Index 1 in list', list.get(1));
// print out all the items
for (const color of list) {
console.log(color);
}
// remove the second item in the list
console.log('Removing list item with index 1', list.remove(1));
// get the item now with index 1
console.log('New list item with index 1', list.get(1));
}
function useDoublyLinkedList () {
console.log('-----------------------------------------------------------------------------------------------------');
console.log('Doubly-Linked List');
console.log('-----------------------------------------------------------------------------------------------------');
const list = new DoublyLinkedList();
list.add("red");
list.add("orange");
list.add("yellow");
console.log('Index 1 in list',list.get(1));
for (const color of list.reverse()) {
console.log(color);
}
// remove the second item in the list
console.log('Removing list item with index 1', list.remove(1));
// get the item now with index 1
console.log('New list item with index 1', list.get(1));
}
function useCircularDoublyLinkedList () {
console.log('-----------------------------------------------------------------------------------------------------');
console.log('Circular Doubly-Linked List');
console.log('-----------------------------------------------------------------------------------------------------');
const list = new CircularDoublyLinkedList();
list.add("red");
list.add("orange");
list.add("yellow");
// get the second item in the list
console.log('Index 1 in list', list.get(1)); // "orange"
for (const color of list.values()) {
console.log(color);
}
// remove the second item in the list
console.log('Removing list item with index 1', list.remove(1));
// get the item now with index 1
console.log('New list item with index 1', list.get(1));
}
(() => {
useLinkedList();
console.log('');
useDoublyLinkedList();
console.log('');
useCircularDoublyLinkedList();
})();