@gobalkrishnan/dsa_collection
Version:
Data Structure and Algorthim used to build application
106 lines (89 loc) • 3.69 kB
JavaScript
import { input, closeInput } from '../console_io.js';
import { LinkedList } from '../data_structure/linked_list/singly_linked_list.js';
class SinglyLinkedListMenu {
constructor() {}
static process_print() {
console.log("===== Main Menu =====");
console.log("0. Exit");
console.log("1. Insert Begin");
console.log("2. Insert End");
console.log("3. Insert After");
console.log("4. Insert Before");
console.log("5. Delete Begin");
console.log("6. Delete End");
console.log("7. Delete After");
console.log("8. Delete Before");
console.log("9. Display in JSON");
console.log("10. Display");
console.log("11. Delete current value")
}
static async process() {
let start = null;
let num = null;
while (num !== '0') {
this.process_print();
// Wait for user input
num = await input("Enter the Option: ");
switch (num) {
case '0': // Exit
console.log("Exiting the program.");
closeInput(); // Close the readline interface here
break;
case '1': // Insert Begin
{
const data = await input("Enter the Data: ");
start = LinkedList.insert_begin(start, data);
break;
}
case '2': // Insert End
{
const data = await input("Enter the Data: ");
start = LinkedList.insert_end(start, data);
break;
}
case '3': {
const data = await input("Enter the Data: ");
const search_data = await input("Enter the Search Value:");
start = LinkedList.insert_after(start,search_data,data);
break;
}
case '4': {
const data = await input("Enter the Data: ");
const search_data = await input("Enter the Search Value:");
start = LinkedList.insert_before(start,search_data,data);
break;
}
case '5': {
start = LinkedList.delete_begin(start);
break;
}
case '6': {
start = LinkedList.delete_end(start);
break;
}
case '7': {
const search_data = await input("Enter the Search Value:");
start = LinkedList.delete_after(start,search_data);
break;
}
case '8': {
const search_data = await input("Enter the Search Value:");
start = LinkedList.delete_before(start,search_data);
break;
}
case '9':start = LinkedList.display_as_json(start);break;
case '10': start = LinkedList.display(start);break;
case '11' : {
const search_data = await input("Enter the Search Value:");
start = LinkedList.delete(start,search_data);
break;
}
default:
console.log("Invalid option. Please try again.");
break;
}
}
}
}
// Run the process method
SinglyLinkedListMenu.process();