From 028a948b2e773ec55f8798a4ca7da78b143e792c Mon Sep 17 00:00:00 2001 From: Chikus Date: Fri, 3 Jan 2020 20:20:03 +0200 Subject: [PATCH] Chikus data structures --- submissions/Chikus/README | 29 ++++++++++ submissions/Chikus/data-store.js | 96 +++++++++++++++++++++++++++++++ submissions/Chikus/http-client.js | 80 ++++++++++++++++++++++++++ submissions/Chikus/http-server.js | 42 ++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 submissions/Chikus/README create mode 100644 submissions/Chikus/data-store.js create mode 100644 submissions/Chikus/http-client.js create mode 100644 submissions/Chikus/http-server.js diff --git a/submissions/Chikus/README b/submissions/Chikus/README new file mode 100644 index 0000000..c0e90fe --- /dev/null +++ b/submissions/Chikus/README @@ -0,0 +1,29 @@ +1.- Run in a terminal http-server.js example:"node http-server.js" +2.- Run in another terminal http-client.js example:"node http-client.js" +3.- In the client terminal you can run the following stack commands one by one: + +push 1 +push 2 +push 3 +pop +pop +pop + +4.- Again in the client terminal you can run the following linkedlist commands one by one: + +insert C +insert B before C +insert A before B + +show + +insert Y +show + +remove B +show + +5.- You can mix commands and use verbose to verify that the command was executed correctly, +this ca be executed one time and it cant be changed again: + +insert C verbose diff --git a/submissions/Chikus/data-store.js b/submissions/Chikus/data-store.js new file mode 100644 index 0000000..27579b9 --- /dev/null +++ b/submissions/Chikus/data-store.js @@ -0,0 +1,96 @@ +/* eslint max-classes-per-file: ["error", 3] */ +exports.Stack = class Stack { + constructor() { + this.items = []; + } + + pop() { + if (this.items.length === 0) { + return `'Underflow'`; + } + return this.items.pop(); + } + + push(element) { + this.items.push(element); + } +}; + +class Node { + constructor(element) { + this.element = element; + this.next = null; + } +} + +exports.LinkedList = class LinkedList { + constructor() { + this.head = null; + this.size = 0; + } + + insert(element) { + const node = new Node(element); + let current; + if (this.head == null) { + this.head = node; + } else { + current = this.head; + while (current.next) { + current = current.next; + } + current.next = node; + } + this.size += 1; + } + + insertAt(element, locationElement) { + const node = new Node(element); + let curr; + let prev; + curr = this.head; + let it = 1; + while (locationElement !== curr.element) { + it += 1; + prev = curr; + curr = curr.next; + if (it > this.size) { + return -1; + } + } + node.next = curr; + if (prev) prev.next = node; + else this.head = node; + this.size += 1; + return 0; + } + + removeElement(element) { + let current = this.head; + let prev = null; + while (current != null) { + if (current.element === element) { + if (prev == null) { + this.head = current.next; + } else { + prev.next = current.next; + } + this.size -= 1; + return current.element; + } + prev = current; + current = current.next; + } + return -1; + } + + printList() { + let curr = this.head; + let str = ''; + while (curr) { + str += `${curr.element} `; + curr = curr.next; + } + return str; + } +}; diff --git a/submissions/Chikus/http-client.js b/submissions/Chikus/http-client.js new file mode 100644 index 0000000..d398671 --- /dev/null +++ b/submissions/Chikus/http-client.js @@ -0,0 +1,80 @@ +/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */ +const http = require('http'); +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin +}); + +let verbo = false; + +rl.on('line', function inputConsole(line) { + const sline = line.split(' '); + let data = ''; + let ifc = ''; + let len; + let method = ''; + if (sline[1]) len = sline[1].length; + if (line.includes('verbose')) verbo = true; + if (sline.length > 4) { + throw new Error('Error in the operation check syntax'); + } + if (line.includes('push') || line.includes('pop')) { + ifc = 'stack'; + if (sline[0] === 'push') { + method = 'POST'; + data = sline[1]; + } + if (sline[0] === 'pop') { + method = 'DELETE'; + len = 0; + } + } else if ( + line.includes('insert') || + line.includes('remove') || + line.includes('show') + ) { + ifc = 'linkedlist'; + if (sline[0] === 'insert') { + method = 'POST'; + data = line; + len = line.length; + } + if (sline[0] === 'show') { + method = 'GET'; + len = 0; + } + if (sline[0] === 'remove') { + method = 'DELETE'; + data = sline[1]; + } + } else { + throw new Error('Error in the operation check syntax'); + } + + const options = { + hostname: 'localhost', + port: 3000, + path: `/api/${ifc}`, + method: `${method}`, + headers: { + 'Content-Type': 'text/plain', + 'Content-Length': `${len}` + } + }; + + const req = http.request(options, res => { + res.on('data', chunk => { + process.stdout.write(`${chunk}\n`); + }); + res.on('end', () => { + if (verbo) process.stdout.write('Success'); + }); + }); + + req.on('error', e => { + process.stdout.write(`problem with request: ${e.message}`); + }); + req.write(`${data}`); + req.end(); +}); diff --git a/submissions/Chikus/http-server.js b/submissions/Chikus/http-server.js new file mode 100644 index 0000000..dfcbad6 --- /dev/null +++ b/submissions/Chikus/http-server.js @@ -0,0 +1,42 @@ +const http = require('http'); +const dataStore = require('./data-store.js'); + +const stack = new dataStore.Stack(); +const linkl = new dataStore.LinkedList(); + +http + .createServer(function reqres(req, res) { + if (req.url === '/api/linkedlist') { + let elem = ''; + if (req.method === 'POST') { + req.on('data', chunk => { + elem = chunk.toString().split(' '); + if (elem.length > 2) { + linkl.insertAt(elem[1], elem[3]); + } else linkl.insert(elem[1]); + }); + res.end(); + } + if (req.method === 'GET') { + res.end(`${linkl.printList()}`); + } + if (req.method === 'DELETE') { + req.on('data', chunk => { + linkl.removeElement(chunk.toString()); + }); + res.end(); + } + } + if (req.url === '/api/stack') { + if (req.method === 'POST') { + req.on('data', chunk => { + stack.push(parseInt(chunk.toString(), 10)); + }); + res.end(); + } + if (req.method === 'DELETE') { + res.end(`${stack.pop()}`); + } + } + }) + .listen(3000);