Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack, Queue an Linked List assignment #12

Merged
merged 2 commits into from
Mar 26, 2023
Merged
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
56 changes: 56 additions & 0 deletions Week-8/Ex-8.1-reverse-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class node {
constructor(val) {
this.val = val;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
}

add(val) {
let newNode = new node(val);

if (this.head === null)
this.head = newNode;
else {
let temp = this.head;
while(temp.next) {
temp = temp.next;
}

temp.next = newNode;
}
}

reverse() {
if (this.head === null) return;

let prev = null;
let current = this.head;
let next = null;

while(current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}

this.head = prev;
}
}

const LL = new LinkedList();
LL.add(1);
LL.add(2);
LL.add(3);
LL.add(4);

console.log(LL); // 1-->2-->3

LL.reverse();

console.log(LL); //3-->2-->1
69 changes: 69 additions & 0 deletions Week-8/Ex-8.2-rotate-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class node {
constructor(val) {
this.val = val;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
}

add(val) {
let newNode = new node(val);

if (this.head === null)
this.head = newNode;
else {
let temp = this.head;
while(temp.next) {
temp = temp.next;
}

temp.next = newNode;
}
}

rotate(k, n) {
if (this.head === null) return;

let previousHead = this.head;
let prev = this.head;
let current = this.head;

let i = 1;
if (k <= n) {
while(current.next) {
if (i === k + 1) {
this.head = current;
prev.next = null;
}

prev = current;
current = current.next;

i++;
}

current.next = previousHead;

return this;
}

return this.head;
}
}

const LL = new LinkedList();
LL.add(2);
LL.add(4);
LL.add(7);
LL.add(8);
LL.add(9);

console.log(JSON.stringify(LL)); // 2->4->7->8->9

const result = LL.rotate(3, 5);

console.log(JSON.stringify(result)); //8->9->2->4->7
85 changes: 85 additions & 0 deletions Week-8/Ex-8.3-detect-loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class node {
constructor(val) {
this.val = val;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
}

add(val) {
let newNode = new node(val);

if (this.head === null)
this.head = newNode;
else {
let temp = this.head;
while(temp.next) {
temp = temp.next;
}

temp.next = newNode;
}
}

makeLoop(k) {
if (k === 0) {
return;
}
if (this.head) {
let count = 1;
let temp = this.head;
if (count < k) {
temp = temp.next;
count++
}

let kNode = temp;

while(!temp.next) {
temp = temp.next;
}

temp.next = kNode;
}
}

detectLoop() {
if(this.head === null)
return false;

let pointer = this.head;
let runner = this.head.next;

while (pointer != runner) {
if (runner === null)
return false;

pointer = pointer.next;
runner = runner.next;
}

return true;

}


}

const LL = new LinkedList();
LL.add(2);
LL.add(4);
LL.add(7);
LL.add(8);
LL.add(9);

LL.makeLoop(0);

console.log(LL);

const result = LL.detectLoop();

console.log(result);
26 changes: 26 additions & 0 deletions Week-8/Ex-8.4-paranthesis-checker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function paranthesisChecker(str) {
let stack = [];
const map = new Map();
map.set(')', '(');
map.set('}', '{');
map.set(']', '[');

for (let char of str) {
if (!map.has(char)) {
stack.push(char);
} else if (stack.pop() !== map.get(char)) {
return false;
}
}

return stack.length === 0;
};

const str = "({[]})";

const result = paranthesisChecker(str);

/*
Time Complexity: O(N)
Space Complxity: O(N)
*/
53 changes: 53 additions & 0 deletions Week-8/Ex-8.5-next-greater-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const arr = [6,8,0,1,3];
const n = 5;

class Stack {
constructor() {
this.items = [];
}

push(element) {
return this.items.push(element)
}

pop() {
if (this.items.length > 0) {
return this.items.pop();
}
}

peek() {
return this.items[this.items.length - 1];
}

isEmpty() {
return this.items.length === 0;
}
}
//6,8,0,1,3
//8 -1 1 3 -1

function greaterElement(arr, n) {
let stack = new Stack();
stack.push(arr[0]);
let result = [];

for(let i = 1; i < n; i++) {
while(stack.isEmpty() === false && stack.peek() < arr[i]) {
stack.pop();
result.push(arr[i]);
}
stack.push(arr[i]);
}

while(stack.isEmpty() === false) {
stack.pop();
result.push(-1);
}

return result;
}


console.log(greaterElement(arr, n));

61 changes: 61 additions & 0 deletions Week-8/Ex-8.6-queue-using-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Stack {
constructor() {
this.items = [];
}

push(element) {
return this.items.push(element)
}

pop() {
if (this.items.length > 0) {
return this.items.pop();
}
}

peek() {
return this.items[this.items.length - 1];
}

isEmpty() {
return this.items.length === 0;
}
}

const s1 = new Stack();
const s2 = new Stack();

class Queue {
enqueue(element) {
s1.push(element);
}

dequeue() {
if(s1.isEmpty()) {
return -1;
}

if(s2.isEmpty()) {
while(!s1.isEmpty()) {
s2.push(s1.pop());
}
}

return s2.pop();
}
}

const q = new Queue();
const result = [];
q.enqueue(2);
q.enqueue(3);
result.push(q.dequeue());
q.enqueue(4);
result.push(q.dequeue());

console.log(result);