generated from pesto-students/PestoPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from pesto-students/week-8
Stack, Queue an Linked List assignment
- Loading branch information
Showing
6 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
|
||
|