Skip to content

Commit

Permalink
Implemented a linked list
Browse files Browse the repository at this point in the history
Implemented a linked list
  • Loading branch information
mitrich2004 committed Nov 15, 2023
1 parent bf72b9d commit 965d15d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
72 changes: 72 additions & 0 deletions dijkstra-prim-visualization/src/algorithms/linkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const createLinkedList = function (capacity) {
const createNode = (key, value, next = null) => {
const node = {
key: key,
value: value,
next: next,
};

return node;
};

let head = null;
let currentSize = 0;

this.isEmpty = () => currentSize == 0;

this.insert = (key, value) => {
currentSize++;
const newNode = createNode(key, value);

if (head == null) {
head = newNode;
} else {
let tmp = head;

while (tmp.next != null) {
tmp = tmp.next;
}

tmp.next = newNode;
}
};

this.extractMin = () => {
let minValue = Infinity;
let minNode = null;
let tmp = createNode(-1, -1, head);

//find the min node
while (tmp.next) {
if (tmp.next.value < minValue) {
minValue = tmp.next.value;
minNode = tmp;
}

tmp = tmp.next;
}
tmp = minNode.next;

//delete the min node
if (minNode.next == head) {
head = head.next;
} else {
minNode.next = minNode.next.next;
}
--currentSize;

return tmp;
};

this.decreaseKey = (key, newValue) => {
let tmp = createNode(-1, -1, head);

while (tmp.next.key != key) {
tmp = tmp.next;
}

tmp.next.value = newValue;
};
};

export default createLinkedList;
5 changes: 3 additions & 2 deletions dijkstra-prim-visualization/src/algorithms/mstPrim.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createGraph, buildAdjacencyList } from "./graph";
import { createGraphFromComponent, buildAdjacencyList } from "./graph";
import createMinHeap from "./minHeap";
import createLinkedList from "./linkedList";

const prim = (graph) => {
const adjacencyList = buildAdjacencyList(graph);

const nodesCount = adjacencyList.length;

const fringe = new createMinHeap(nodesCount);
const fringe = new createLinkedList(nodesCount);
const isInHeap = new Array(nodesCount);
const results = new Array(nodesCount);
const keys = new Array(nodesCount);
Expand Down
7 changes: 4 additions & 3 deletions dijkstra-prim-visualization/src/algorithms/spDijkstra.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createGraph, buildAdjacencyList } from "./graph.js";
import { createGraphFromComponent, buildAdjacencyList } from "./graph.js";
import createMinHeap from "./minHeap.js";
import createLinkedList from "./linkedList.js";

const dijkstra = (graph) => {
const adjacencyList = buildAdjacencyList(graph);
Expand All @@ -8,7 +9,7 @@ const dijkstra = (graph) => {
const startNode = 0;
const finishNode = nodesCount - 1;

const fringe = new createMinHeap(nodesCount);
const fringe = new createLinkedList(nodesCount);
const keys = new Array(nodesCount);
const parents = new Array(nodesCount);

Expand All @@ -24,7 +25,7 @@ const dijkstra = (graph) => {
const node = fringe.extractMin();
const neighbours = adjacencyList[node.key];

if (node == finishNode) {
if (node.key == finishNode) {
break;
}

Expand Down

0 comments on commit 965d15d

Please sign in to comment.