From a988a1889e1caeb9d49a6e79fa3b1e6abbcd12f7 Mon Sep 17 00:00:00 2001 From: Zakkaray Date: Sun, 17 Nov 2019 18:21:09 +0200 Subject: [PATCH] Data-structures done --- .../Zakkarat/data-structures/Readme.md | 0 submissions/Zakkarat/data-structures/list.js | 67 +++++++++++++++++++ submissions/Zakkarat/data-structures/node.js | 25 +++++++ .../Zakkarat/data-structures/server.js | 53 +++++++++++++++ submissions/Zakkarat/data-structures/stack.js | 36 ++++++++++ 5 files changed, 181 insertions(+) create mode 100644 submissions/Zakkarat/data-structures/Readme.md create mode 100644 submissions/Zakkarat/data-structures/list.js create mode 100644 submissions/Zakkarat/data-structures/node.js create mode 100644 submissions/Zakkarat/data-structures/server.js create mode 100644 submissions/Zakkarat/data-structures/stack.js diff --git a/submissions/Zakkarat/data-structures/Readme.md b/submissions/Zakkarat/data-structures/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/submissions/Zakkarat/data-structures/list.js b/submissions/Zakkarat/data-structures/list.js new file mode 100644 index 0000000..11b2995 --- /dev/null +++ b/submissions/Zakkarat/data-structures/list.js @@ -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; diff --git a/submissions/Zakkarat/data-structures/node.js b/submissions/Zakkarat/data-structures/node.js new file mode 100644 index 0000000..0a74bcc --- /dev/null +++ b/submissions/Zakkarat/data-structures/node.js @@ -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; diff --git a/submissions/Zakkarat/data-structures/server.js b/submissions/Zakkarat/data-structures/server.js new file mode 100644 index 0000000..4cfd76d --- /dev/null +++ b/submissions/Zakkarat/data-structures/server.js @@ -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}`); +}); diff --git a/submissions/Zakkarat/data-structures/stack.js b/submissions/Zakkarat/data-structures/stack.js new file mode 100644 index 0000000..dd088c9 --- /dev/null +++ b/submissions/Zakkarat/data-structures/stack.js @@ -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;