Skip to content
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
48 changes: 48 additions & 0 deletions java_implementation/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package datastructures;

public class LinkedList {

private Node head;
private int length = 0;

private class Node {
private final Object data;
private Node next;

Node(Object data) {
this.data = data;
}
}

public Object getHeadNode() {
return this.head.data;
}

public Object getTailNode() {
Node currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
return currentNode.data;
}

public Node insert(Object data) {
Node node = new Node(data);
if (this.head == null) {
this.head = node;
this.length++;
return node;
}
Node currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
currentNode.next = node;
this.length++;
return node;
}

public int size() {
return this.length;
}
}
116 changes: 116 additions & 0 deletions lib/doubly_linked_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Node = function Node(data) {
_classCallCheck(this, Node);

this.data = data;
this.next = null;
};

var DoublyLinkedList = function () {
function DoublyLinkedList() {
_classCallCheck(this, DoublyLinkedList);

this._length = 0;
this.head = null;
this.tail = null;
}

_createClass(DoublyLinkedList, [{
key: 'insert',
value: function insert(value) {
var node = new Node(value);

if (this._length) {
this.tail.next = node;
node.previous = this.tail;
this.tail = node;
} else {
this.head = node;
this.tail = node;
}
this._length++;
return node;
}
}, {
key: 'find',
value: function find(position) {
var currentNode = this.head;
var length = this._length;
var count = 1;
var message = { failure: 'Non-existent node in this list.' };

if (length === 0 || position < 1 || position > length) {
throw new Error(message.failure);
}
while (count < position) {
currentNode = currentNode.next;
count++;
}
return currentNode;
}
}, {
key: 'remove',
value: function remove(position) {
var currentNode = this.head;
var count = 1;
var message = { failure: 'non-existent node in this list.' };
var beforeNodeToDelete = null;
var nodeToDelete = null;
var deletedNode = null;

if (length === 0 || position < 1 || position > this._length) {
throw new Error(message.failure);
}

// 2nd use-case: the first node is removed
if (position === 1) {
this.head = currentNode.next;

// 2nd use-case: there is a second node
if (!this.head) {
this.head.previous = null;
// 2nd use-case: there is no second node
} else {
this.tail = null;
}

// 3rd use-case: the last node is removed
} else if (position === this._length) {
this.tail = this.tail.previous;
this.tail.next = null;
// 4th use-case: a middle node is removed
} else {
while (count < position) {
currentNode = currentNode.next;
count++;
}

beforeNodeToDelete = currentNode.previous;
nodeToDelete = currentNode;
afterNodeToDelete = currentNode.next;

beforeNodeToDelete.next = afterNodeToDelete;
afterNodeToDelete.previous = beforeNodeToDelete;
deletedNode = nodeToDelete;
nodeToDelete = null;
}

this._length--;

return message.success;
}
}]);

return DoublyLinkedList;
}();

exports.default = DoublyLinkedList;
126 changes: 124 additions & 2 deletions lib/linked_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,138 @@ var LinkedList = function () {
_createClass(LinkedList, [{
key: "getHeadNode",
value: function getHeadNode() {
return this.head;
return this.head.data;
}
}, {
key: "getTailNode",
value: function getTailNode() {}
value: function getTailNode() {
var currentNode = this.head;
while (currentNode.next) {
currentNode = currentNode.next;
}
return currentNode.data;
}
}, {
key: "find",
value: function find(position) {
var currentNode = this.head;
var message = { failure: "This node does not exist" };
var count = 1;

if (this._length === 0 || position < 1 || position > this._length) {
throw new Error(message.failure);
}
while (count < position) {
currentNode = currentNode.next;
count++;
}
return currentNode;
}
}, {
key: "insert",
value: function insert(data) {
var node = new Node(data);
if (!this.head) {
this.head = node;
this._length += 1;
return node;
}
var currentNode = this.head;
while (currentNode.next !== null) {
currentNode = currentNode.next;
}
currentNode.next = node;
this._length += 1;
return node;
}
}, {
key: "insertBefore",
value: function insertBefore(data, newData) {
var currentNode = this.head;
var insertedNode = void 0;
while (currentNode !== null && insertedNode == undefined) {
if (JSON.stringify(currentNode.next.data) === JSON.stringify(data)) {
insertedNode = new Node(newData);
insertedNode.next = currentNode.next;
currentNode.next = insertedNode;
}
if (currentNode === this.head && currentNode.data === data) {
insertedNode = new Node(newData);
insertedNode.next = currentNode;
this.head = insertedNode;
}
}
this._length += 1;
return insertedNode;
}
}, {
key: "insertAfter",
value: function insertAfter(data, newData) {
var node = new Node(newData);
var currentNode = this.head;

while (currentNode.next) {
if (currentNode.data === data) {
node.next = currentNode.next;
currentNode.next = node;
return node;
}
currentNode.next = node;
}
}
}, {
key: "removeFirst",
value: function removeFirst() {
var currentNode = this.head;
this.head = currentNode.next;
currentNode = null;
this._length--;
}
}, {
key: "remove",
value: function remove(position) {
var currentNode = this.head;
var count = 0;
var message = { failure: 'Failure: non-existent node in this list.' };
var beforeNodeToDelete = null;
var nodeToDelete = null;
var deletedNode = null;

if (position < 0 || position > this._length) {
throw new Error(message.failure);
}
while (count < position) {
beforeNodeToDelete = currentNode;
nodeToDelete = currentNode.next;
count++;
}
beforeNodeToDelete.next = nodeToDelete.next;
deletedNode = nodeToDelete;
nodeToDelete = null;
this._length--;

return deletedNode;
}
}, {
key: "size",
value: function size() {
return this._length;
}
}, {
key: "isEmpty",
value: function isEmpty() {
if (!this.head) {
return true;
}
return false;
}
}, {
key: "clear",
value: function clear() {
this.head = null;
this._length = 0;
this.data = null;
}
}]);

return LinkedList;
Expand Down
67 changes: 63 additions & 4 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
// const stack = () => {
"use strict";

// }
Object.defineProperty(exports, "__esModule", {
value: true
});

// export default Stack
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Stack = function () {
function Stack() {
_classCallCheck(this, Stack);

this.storage = "";
this._length = 0;
}

_createClass(Stack, [{
key: "push",
value: function push(value) {
this.storage = this.storage.concat("***", value);
this._length++;
}
}, {
key: "pop",
value: function pop() {
var str = this.storage.slice(this.storage.lastIndexOf('***') + 3);
this.storage = this.storage.substring(0, this.storage.lastIndexOf('***'));
this._length--;
return str;
}
}, {
key: "size",
value: function size() {
return this._length;
}

// peek() {
// return this.storage.charAt(3)
// }

}, {
key: "isEmpty",
value: function isEmpty() {
if (this._length == 0) {
return true;
}
return false;
}
}, {
key: "length",
value: function length() {
return this._length;
}
}]);

return Stack;
}();

// const stack = new Stack()
// stack.push("RedBeans")
// console.log(stack.length())

exports.default = Stack;
Loading