generated from pesto-students/PestoPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #19 from pesto-students/week7
Week 7 Assignment Linked List
- Loading branch information
Showing
7 changed files
with
626 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,69 @@ | ||
class Node { | ||
constructor(element) { | ||
this.element = element; | ||
this.nextNode = null; | ||
} | ||
} | ||
|
||
class LinkedList { | ||
constructor() { | ||
this.head = null; | ||
this.size = 0; | ||
} | ||
|
||
addNode(element) { | ||
var node = new Node(element); | ||
|
||
var current; | ||
|
||
if (this.head == null) { | ||
this.head = node; | ||
} else { | ||
current = this.head; | ||
while (current.nextNode) { | ||
current = current.nextNode; | ||
} | ||
|
||
current.nextNode = node; | ||
} | ||
this.size++; | ||
} | ||
|
||
printLinkedList(linkedlist) { | ||
let temp = linkedlist; | ||
var printedList = []; | ||
while (temp !== null) { | ||
printedList.push(temp.element); | ||
temp = temp.nextNode; | ||
} | ||
console.log(printedList); | ||
} | ||
|
||
reverseLinkedList() { | ||
let prevNode = null; | ||
let current = this.head; | ||
let nextNode = null; | ||
|
||
while (current !== null) { | ||
nextNode = current.nextNode; | ||
current.nextNode = prevNode; | ||
prevNode = current; | ||
current = nextNode; | ||
} | ||
|
||
this.printLinkedList(prevNode); | ||
} | ||
} | ||
|
||
var l = new LinkedList(); | ||
|
||
l.addNode(10); | ||
l.addNode(20); | ||
l.addNode(30); | ||
l.addNode(40); | ||
l.addNode(50); | ||
|
||
console.log("Original Linked list"); | ||
l.printLinkedList(l.head); | ||
console.log("Reverse Linked list"); | ||
l.reverseLinkedList(); |
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,76 @@ | ||
class Node { | ||
constructor(element) { | ||
this.element = element; | ||
this.nextNode = null; | ||
} | ||
} | ||
|
||
class LinkedList { | ||
constructor() { | ||
this.head = null; | ||
this.size = 0; | ||
} | ||
|
||
addNode(element) { | ||
var node = new Node(element); | ||
var current; | ||
|
||
if (this.head == null) { | ||
this.head = node; | ||
} else { | ||
current = this.head; | ||
while (current.nextNode) { | ||
current = current.nextNode; | ||
} | ||
|
||
current.nextNode = node; | ||
} | ||
this.size++; | ||
} | ||
|
||
printLinkedList() { | ||
var temp = this.head; | ||
var printedList = []; | ||
while (temp !== null) { | ||
printedList.push(temp.element); | ||
temp = temp.nextNode; | ||
} | ||
console.log(printedList); | ||
} | ||
|
||
rotateLinkedList(k) { | ||
let current = this.head; | ||
let count = 1; | ||
while (count < k && current != null) { | ||
current = current.nextNode; | ||
count++; | ||
} | ||
|
||
var secondElemnts = current; | ||
while (current.nextNode != null) { | ||
current = current.nextNode; | ||
} | ||
|
||
current.nextNode = this.head; | ||
this.head = secondElemnts.nextNode; | ||
secondElemnts.nextNode = null; | ||
|
||
this.printLinkedList(); | ||
} | ||
} | ||
|
||
let l = new LinkedList(); | ||
l.addNode(10); | ||
l.addNode(20); | ||
l.addNode(30); | ||
l.addNode(40); | ||
l.addNode(50); | ||
l.addNode(60); | ||
l.addNode(70); | ||
|
||
console.log("Original Linked list"); | ||
l.printLinkedList(); | ||
|
||
let k = 2; | ||
console.log("Roteted Linked list"); | ||
l.rotateLinkedList(k); |
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 Node { | ||
constructor(element) { | ||
this.element = element; | ||
this.nextNode = null; | ||
} | ||
} | ||
|
||
class LinkedList { | ||
constructor() { | ||
this.head = null; | ||
this.size = 0; | ||
} | ||
|
||
addNode(element) { | ||
var node = new Node(element); | ||
var current; | ||
|
||
if (this.head == null) { | ||
this.head = node; | ||
} else { | ||
current = this.head; | ||
while (current.nextNode) { | ||
current = current.nextNode; | ||
} | ||
|
||
current.nextNode = node; | ||
} | ||
this.size++; | ||
} | ||
|
||
printLinkedList() { | ||
var temp = this.head; | ||
var printedList = []; | ||
while (temp !== null) { | ||
printedList.push(temp.element); | ||
temp = temp.nextNode; | ||
} | ||
} | ||
|
||
detectLoop(k) { | ||
var temp = this.head; | ||
while (temp !== null) { | ||
if (temp.nextNode !== null) { | ||
if (temp.element === k - 1 && temp.nextNode.element === k + 1) { | ||
return `Loop detected with ${temp.element}, ${k}, ${temp.nextNode.element}`; | ||
} | ||
} | ||
temp = temp.nextNode; | ||
} | ||
return `Loop not detected`; | ||
} | ||
} | ||
|
||
let l = new LinkedList(); | ||
l.addNode(1); | ||
l.addNode(2); | ||
l.addNode(3); | ||
l.addNode(5); | ||
|
||
let k = 4; | ||
console.log(l.detectLoop(k)); |
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,42 @@ | ||
class Stack { | ||
constructor() { | ||
this.status = null; | ||
this.stack = []; | ||
this.openArr = ["{", "[", "("]; | ||
this.closeArr = ["}", "]", ")"]; | ||
} | ||
|
||
push(element) { | ||
this.stack.push(element); | ||
} | ||
|
||
pop() { | ||
this.stack.pop(); | ||
} | ||
|
||
checkElement(arr) { | ||
for (let i = 0; i < arr.length; i++) { | ||
if (this.openArr.includes(arr[i])) { | ||
this.push(arr[i]); | ||
} else { | ||
let check = this.closeArr.indexOf(arr[i]); | ||
if (this.stack[this.stack.length - 1] == arr[check]) { | ||
this.pop(); | ||
this.status = true; | ||
} else { | ||
this.status = false; | ||
} | ||
} | ||
} | ||
|
||
return this.status; | ||
} | ||
} | ||
|
||
let arr = "{[()]}"; | ||
let arr1 = "{[(]]}"; | ||
let arr2 = "{[(]}"; | ||
let stack = new Stack(); | ||
console.log(stack.checkElement(arr)); | ||
console.log(stack.checkElement(arr1)); | ||
console.log(stack.checkElement(arr2)); |
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,24 @@ | ||
function nextGreater(arr) { | ||
var next; | ||
|
||
let result = new Array(); | ||
for (let i = 0; i < arr.length; i++) { | ||
next = -1; | ||
for (let j = i; j < arr.length; j++) { | ||
if (arr[j] > arr[i]) { | ||
next = arr[j]; | ||
break; | ||
} | ||
} | ||
result.push(next); | ||
} | ||
|
||
console.log(result); | ||
} | ||
|
||
let arr = new Array(11, 13, 21, 3); | ||
let arr1 = new Array(1, 3, 2, 4); | ||
let arr2 = new Array(6, 8, 0, 1, 3); | ||
nextGreater(arr); | ||
nextGreater(arr1); | ||
nextGreater(arr2); |
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,49 @@ | ||
class Queue { | ||
constructor() { | ||
this.stack1 = []; | ||
this.stack2 = []; | ||
} | ||
|
||
enqueue(element) { | ||
this.stack1.push(element); | ||
} | ||
|
||
dequeue() { | ||
while (this.stack1.length > 0) { | ||
const item = this.stack1.pop(); | ||
this.stack2.push(item); | ||
} | ||
|
||
let lastItemPop = this.stack2.pop(); | ||
|
||
while (this.stack2.length > 0) { | ||
let item = this.stack2.pop(); | ||
this.stack1.push(item); | ||
} | ||
|
||
console.log(`Item Deque ${lastItemPop}`); | ||
} | ||
} | ||
|
||
let q = new Queue(); | ||
|
||
q.enqueue(1); | ||
q.enqueue(2); | ||
q.enqueue(3); | ||
q.enqueue(4); | ||
q.enqueue(5); | ||
q.enqueue(6); | ||
|
||
if (q.stack1 !== null) { | ||
console.log("Your Queue Is"); | ||
console.log(q.stack1); | ||
} | ||
|
||
q.dequeue(); | ||
q.dequeue(); | ||
q.dequeue(); | ||
|
||
if (q.stack1 !== null) { | ||
console.log("Your Queue After Deque Is"); | ||
console.log(q.stack1); | ||
} |
Oops, something went wrong.