Skip to content
Open
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
Empty file.
67 changes: 67 additions & 0 deletions submissions/Zakkarat/data-structures/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const Node = require('./node');

class List {
constructor () {
this._head = null;
this._size = 0;
}

insert (searchNode, value) {
let currNode = this._head;
if (this._head) {
if (this._head.getValue() === searchNode) {
this._head = new Node(value, null, this._head);
this._size += 1;
return;
}
currNode = this.getTo(this._head, searchNode);
currNode.setNext(value);
} else {
this._head = new Node(value, null, null);
}
this._size += 1;
}

getTo (node, value) {
if (!node.getNext() || !node.getNext().getValue() || node.getNext().getValue() === value) {
return node;
}
return this.getTo(node.getNext(), value);
}

show () {
let tempNode = this._head;
return [...Array(this._size).keys()].map(_ => {
const value = tempNode.getValue();
tempNode = tempNode.getNext();
return value;
}).join('->');
}

remove (value, node) {
if (node === 'head') {
node = this._head;
if (node.getValue() === value) {
this._head = node.getNext();
this._size -= 1;
return;
}
}
if (!node.getNext()) {
return;
}
if (node.getNext().getValue() === value) {
if (!node.getNext().getNext()) {
node.setNext(null);
this._size -= 1;
return;
}
node.setNext(node.getNext().getNext().getValue());
this._size -= 1;
return;
}
return this.remove(value, node.getNext());
}
}

module.exports = List;
25 changes: 25 additions & 0 deletions submissions/Zakkarat/data-structures/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Node {
constructor (value, prevNode, nextNode) {
this._value = value;
this._prevNode = prevNode;
this._nextNode = nextNode;
}

getValue () {
return this._value;
}

getPrev () {
return this._prevNode;
}

getNext () {
return this._nextNode;
}

setNext (value) {
this._nextNode = value ? new Node(value, null, this._nextNode) : null;
}
}

module.exports = Node;
53 changes: 53 additions & 0 deletions submissions/Zakkarat/data-structures/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const http = require('http');
const Stack = require('./stack');
const List = require('./list');
const port = 3000;
const stackInst = new Stack();
const listInst = new List();
const requestHandler = (req, resp) => {
const url = req.url.slice(1).split('/');
if (url[0] === '') {
resp.end('Choose stack or list in URL');
}
if (url[0] === 'stack') {
if (req.method === 'GET') {
if (url[1] === 'top') {
resp.end(stackInst.top());
}
if (url[1] === 'size') {
resp.end(stackInst.size());
}
if (url[1] === 'pop') {
resp.end(stackInst.pop());
}
}
if (req.method === 'POST') {
req.on('data', (data) => stackInst.push(data.toString()));
resp.end();
}
}
if (url[0] === 'list') {
if (req.method === 'POST') {
req.on('data', data => {
const { successor, value } = JSON.parse(data.toString());
listInst.insert(successor, value);
});
resp.end();
}
if (req.method === 'GET') {
resp.end(listInst.show());
}
if (req.method === 'DELETE') {
listInst.remove(url[1], 'head');
resp.end();
}
}
};
const server = http.createServer(requestHandler);

server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err);
}
console.log(`server is listening on ${port}`);
});
36 changes: 36 additions & 0 deletions submissions/Zakkarat/data-structures/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Node = require('./node');

class Stack {
constructor () {
this._top = null;
this._size = 0;
}

top () {
if (this._top) {
return this._top.getValue();
}
return 'Empty stack';
}

size () {
return this._size.toString();
}

push (value) {
this._top = new Node(value, this._top);
this._size += 1;
}

pop () {
let currTop;
if (this._size) {
[this._top, currTop] = [this._top.getPrev(), this._top];
this._size -= 1;
return currTop.getValue();
}
return 'No elements in stack';
}
}

module.exports = Stack;