Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions Exercise_1.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
60 changes: 35 additions & 25 deletions Exercise_2.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
95 changes: 52 additions & 43 deletions Exercise_3.js
Original file line number Diff line number Diff line change
@@ -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);