Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- constructor(val) {
- this.next = null;
- this.prev = null;
- this.val = val;
- }
- }
- class doctorsOffice {
- constructor() {
- this.head = null;
- this.tail = null;
- this.count = 0;
- this.occurMap = new Map()
- }
- Append(name) {
- if (this.head === null) {
- this.head = new Node(name);
- this.tail = this.head;
- this.count++
- this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
- } else {
- const newNode = new Node(name);
- newNode.prev = this.tail;
- this.tail.next = newNode;
- this.tail = this.tail.next;
- this.count++
- this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
- }
- return 'OK';
- }
- Insert(position, name) {
- if (+position > this.count) return 'Error';
- if (position == 0 && this.head) {
- const newNode = new Node(name);
- this.head.prev = newNode;
- newNode.next = this.head;
- this.head = this.head.prev;
- this.count++
- this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
- return 'OK';
- } else if (position == 0) {
- const newNode = new Node(name);
- this.head = newNode;
- this.tail = newNode;
- this.count++
- this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
- return 'OK'
- } else if (position == this.count) {
- const newNode = new Node(name);
- newNode.prev = this.tail;
- this.tail.next = newNode;
- this.tail = this.tail.next;
- this.count++
- this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
- return 'OK';
- } else {
- let temp = this.head;
- for (let i = 0; i < position - 1; i++) {
- temp = temp.next;
- }
- const newNode = new Node(name);
- newNode.prev = temp;
- newNode.next = temp.next;
- temp.next.prev = newNode;
- temp.next = newNode;
- this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
- this.count++
- return 'OK'
- }
- }
- Find(name) {
- return this.occurMap.get(name) || 0
- }
- Examine(count) {
- if (this.count < count) return 'Error';
- let str = ''
- while (count) {
- if (this.head != this.tail) {
- this.occurMap.set(this.head.val, this.occurMap.get(this.head.val) - 1)
- str += this.head.val + ' ';
- this.head = this.head.next;
- count--
- } else {
- this.occurMap.set(this.head.val, this.occurMap.get(this.head.val) - 1)
- str += this.head.val + ' ';
- this.head = null;
- this.tail = null;
- count--
- }
- this.count--
- }
- return str.trim();
- }
- }
- const test = new doctorsOffice();
- while (true) {
- const data = gets().split(' ')
- const command = data[0];
- const par1 = data[1];
- const par2 = data[2];
- if (command === 'End') break;
- print(test[command](par1, par2))
- }
Advertisement
Add Comment
Please, Sign In to add comment