From 549ae7d6387e1166c8fe9c8aea81fd3c1d0f445d Mon Sep 17 00:00:00 2001 From: Sundeep4sandy Date: Mon, 6 Apr 2026 18:30:07 -0500 Subject: [PATCH] PreCourse1Complete --- Exercise_1.js | 54 ++++++++++++++++------------- Exercise_2.js | 60 ++++++++++++++++++-------------- Exercise_3.js | 95 ++++++++++++++++++++++++++++----------------------- 3 files changed, 118 insertions(+), 91 deletions(-) diff --git a/Exercise_1.js b/Exercise_1.js index 207189ea0..557648021 100644 --- a/Exercise_1.js +++ b/Exercise_1.js @@ -1,33 +1,41 @@ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file -​ - constructor() { - //Initialize your constructor - this.MAX = 1000; - this.top = -1; - this.a = new Array(this.MAX); + constructor() { + //Initialize your constructor + this.MAX = 1000; + this.top = -1; + this.a = new Array(this.MAX); + } + isEmpty() { + return this.top === -1; + } + push(x) { + if (this.top >= this.MAX - 1) { + console.log("Stack Overflow"); + return false; } -​ - function isEmpty() { - //Write your code here + this.top += 1; + this.a[this.top] = x; + return true; + } + pop() { + if (this.isEmpty()) { + console.log("Stack Underflow"); + return 0; } -​ - function push(x) { - //Check for stack Overflow - //Write your code here - } -​ - function pop() { - //If empty return 0 and print " Stack Underflow" - //Write your code here - } -​ - function peek() { - //Write your code here + const val = this.a[this.top]; + this.top -= 1; + return val; + } + peek() { + if (this.isEmpty()) { + console.log("Stack Empty"); + return 0; } + return this.a[this.top]; + } } -​ let s = new Stack(); s.push(10); s.push(20); diff --git a/Exercise_2.js b/Exercise_2.js index 2e3216f94..471809d28 100644 --- a/Exercise_2.js +++ b/Exercise_2.js @@ -1,33 +1,43 @@ class StackAsLinkedList { -​ - static stackNode = class { -​ - constructor(d) { - //Constructor here - this.data = d; - this.next = null; - } + static stackNode = class { + constructor(d) { + //Constructor here + this.data = d; + this.next = null; } -​ - function isEmpty() { - //Write your code here for the condition if stack is empty. + }; + constructor() { + this.root = null; // top of the stack + } + isEmpty() { + return this.root === null; + } + + push(data) { + const newNode = new StackAsLinkedList.stackNode(data); + newNode.next = this.root; + this.root = newNode; + } + + pop() { + if (this.isEmpty()) { + console.log("Stack Underflow"); + return 0; } -​ - function push(data) { - //Write code to push data to the stack. - } -​ - function pop() { - //If Stack Empty Return 0 and print "Stack Underflow" - //Write code to pop the topmost element of stack. - //Also return the popped element - } -​ - function peek() { - //Write code to just return the topmost element without removing it. + const popData = this.root.data; + this.root = this.root.next; + return popData; + } + + peek() { + if (this.isEmpty()) { + console.log("Stack Empty"); + return 0; } + return this.root.data; + } } -//Driver code + const sll = new StackAsLinkedList(); sll.push(10); sll.push(20); diff --git a/Exercise_3.js b/Exercise_3.js index d1511f80e..336608563 100644 --- a/Exercise_3.js +++ b/Exercise_3.js @@ -1,49 +1,58 @@ -// Java program to implement // a Singly Linked List class LinkedList { - constructor() { - this.head = null; + constructor() { + this.startPos = null; + } + // Linked list Node. + static Node = class { + constructor(d) { + this.data = d; + this.next = null; } - // Linked list Node. - static Node = class { - constructor(d) { - this.data = d; - this.next = null; - } + }; + + // Method to insert a new node + insert(list, data) { + let newNode = new LinkedList.Node(data); + + if (list.startPos === null) { + list.startPos = newNode; + } else { + let lastPos = list.startPos; + while (lastPos.next !== null) { + lastPos = lastPos.next; + } + lastPos.next = newNode; } -​ - // Method to insert a new node - function insert(list, data) { - // Create a new node with given data -​ - // If the Linked List is empty, - // then make the new node as head -​ - // Else traverse till the last node - // and insert the new_node there -​ - // Insert the new_node at last node - // Return the list by head - } -​ - // Method to print the LinkedList. - function printList(list) { - // Traverse through the LinkedList -​ - // Print the data at current node -​ - // Go to next node + + return list; + } + + // Method to print the LinkedList. + printList(list) { + let currNode = list.startPos; + + let output = ""; + while (currNode !== null) { + output += currNode.data + " "; + currNode = currNode.next; } + console.log(output.trim()); + + // Traverse through the LinkedList + // Print the data at current node + // Go to next node + } } - // Driver code - /* Start with the empty list. */ - let list = new LinkedList(); -​ - // ******INSERTION****** - // Insert the values - list.insert(list, 1); - list.insert(list, 2); - list.insert(list, 3); - list.insert(list, 4); - // Print the LinkedList - list.printList(list); +// Driver code +/* Start with the empty list. */ +let list = new LinkedList(); + +// ******INSERTION****** +// Insert the values +list.insert(list, 1); +list.insert(list, 2); +list.insert(list, 3); +list.insert(list, 4); +// Print the LinkedList +list.printList(list);