Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chikus data structures #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions submissions/Chikus/README
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions submissions/Chikus/data-store.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
80 changes: 80 additions & 0 deletions submissions/Chikus/http-client.js
Original file line number Diff line number Diff line change
@@ -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();
});
42 changes: 42 additions & 0 deletions submissions/Chikus/http-server.js
Original file line number Diff line number Diff line change
@@ -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);