Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let input = [
- 'Append Steven',
- 'Examine 1',
- 'Find Ina',
- 'Append Niki',
- 'Insert 0 Peter',
- 'Append Nadya',
- 'Insert 3 Grigor',
- 'Examine 5',
- 'Append Asya',
- 'Insert 4 Steven',
- 'Append Steven',
- 'Find Asya',
- 'Find Steven',
- 'Examine 3',
- 'Find Peter',
- 'Examine 4',
- 'Find Steven',
- 'Insert 1 Ina',
- 'End'
- ];
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- // Code
- let myResult = []
- class LinkedListNode {
- constructor() {
- this.value = null;
- this.next = null;
- this.prev = null;
- }
- }
- class WaitingList {
- head =null
- tail =null
- count = 0
- constructor(){
- this.nameMap = new Map();
- }
- addFirst(value) {
- const node = new LinkedListNode();
- node.value = value
- if(!this.head){
- this.head=node;
- this.tail = node;
- } else {
- node.next = this.head;
- this.head.prev = node;
- this.head = node;
- }
- this.count++;
- const count = this.nameMap.get(value) || 0;
- this.nameMap.set(value, count+1);
- myResult.push('OK');
- }
- append(value) {
- const node = new LinkedListNode();
- node.value = value
- if(!this.head) {
- this.head = node;
- } else {
- this.tail.next = node;
- node.prev = this.tail;
- }
- this.tail = node;
- this.count++;
- const counter = this.nameMap.get(value) || 0;
- this.nameMap.set(value, counter+1);
- myResult.push('OK');
- }
- removeFirst(){
- if(this.count === 1){
- const val = this.head.value;
- this.head = null;
- this.tail = null;
- this.count--;
- return val;
- } else {
- const val = this.head.value;
- this.head = this.head.next;
- this.head.prev = null;
- this.count--;
- return val;
- }
- }
- removeLast() {
- if(this.count === 1){
- const val = this.tail.value;
- this.head = null;
- this.tail = null;
- this.count--;
- return val;
- } else {
- const val = this.tail.value;
- this.tail.prev.next = null;
- this.count--;
- return val;
- }
- }
- insertBefore(node,val){
- const newNode = new LinkedListNode();
- newNode.value = val
- if(node === this.head){
- newNode.next = this.head;
- this.head.prev = newNode;
- this.head = newNode;
- this.count++;
- } else {
- node.prev.next = newNode;
- newNode.next = node;
- node.prev = newNode;
- this.count++;
- }
- const counter = this.nameMap.get(val) || 0;
- this.nameMap.set(val, counter+1);
- myResult.push('OK')
- }
- // new methods
- insert(pos, name) {
- if(pos < 0 || pos > this.count) {
- myResult.push('Error');
- } else {
- // const counter = this.nameMap.get(name) || 0;
- // this.nameMap.set(name, counter+1);
- if(Number(pos) === 0){
- this.addFirst(name)
- } else if (Number(pos) === this.count) {
- this.append(name)
- } else {
- let ref = this.head
- for (let i = 1; i<= pos; i++){
- ref = ref.next
- }
- this.insertBefore(ref, name)
- }
- }
- }
- find(name){
- let a = this.nameMap.get(name) || 0;
- myResult.push(a)
- }
- examine(val){
- if(val > this.count){
- myResult.push('Error')
- } else {
- let outPut = [];
- for (let i = 1; i <= val;i++){
- let patient = this.removeFirst()
- let counter = this.nameMap.get(patient);
- if (counter === 1){
- this.nameMap.delete(patient)
- } else {
- this.nameMap.set(patient, counter-1)
- }
- outPut.push(patient)
- }
- myResult.push(outPut.join(' '));
- }
- }
- }
- const list = new WaitingList();
- outer:
- while (true){
- command = gets().split(' ')
- let methodName = command[0].toLowerCase()
- switch(methodName) {
- case 'end':
- break outer;
- case 'append':
- list.append(command[1]);
- break;
- case 'insert':
- list.insert(command[1], command[2]);
- break;
- case 'find':
- list.find(command[1]);
- break;
- case 'examine':
- list.examine(command[1]);
- }
- }
- for (const el of myResult){
- console.log(el);
- }
Advertisement
Add Comment
Please, Sign In to add comment