|
| 1 | +// Queue data structure class example |
| 2 | +class Queue { |
| 3 | + constructor(dataStore = []) { |
| 4 | + this.dataStore = dataStore; |
| 5 | + } |
| 6 | + |
| 7 | + enqueue(element) { |
| 8 | + this.dataStore.push(element); |
| 9 | + } |
| 10 | + |
| 11 | + dequeue() { |
| 12 | + this.dataStore.shift(); |
| 13 | + } |
| 14 | + |
| 15 | + front() { |
| 16 | + return this.dataStore[0]; |
| 17 | + } |
| 18 | + |
| 19 | + back() { |
| 20 | + return this.dataStore[this.dataStore.length - 1]; |
| 21 | + } |
| 22 | + |
| 23 | + empty() { |
| 24 | + if(this.dataStore.length === 0) { |
| 25 | + return true; |
| 26 | + } else { |
| 27 | + return false; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + count() { |
| 32 | + return this.dataStore.length; |
| 33 | + } |
| 34 | + |
| 35 | + toString() { |
| 36 | + return JSON.stringify(this.dataStore); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Dequeue class example for exercise 1 |
| 41 | +class Dequeue { |
| 42 | + constructor(dataStore = []) { |
| 43 | + this.dataStore = dataStore; |
| 44 | + } |
| 45 | + |
| 46 | + enqueueFront(element) { |
| 47 | + this.dataStore.unshift(element); |
| 48 | + } |
| 49 | + |
| 50 | + enqueue(element) { |
| 51 | + this.dataStore.push(element); |
| 52 | + } |
| 53 | + |
| 54 | + dequeue() { |
| 55 | + this.dataStore.shift(); |
| 56 | + } |
| 57 | + |
| 58 | + dequeueBack() { |
| 59 | + this.dataStore.pop(); |
| 60 | + } |
| 61 | + |
| 62 | + front() { |
| 63 | + return this.dataStore[0]; |
| 64 | + } |
| 65 | + |
| 66 | + back() { |
| 67 | + return this.dataStore[this.dataStore.length - 1]; |
| 68 | + } |
| 69 | + |
| 70 | + empty() { |
| 71 | + if(this.dataStore.length === 0) { |
| 72 | + return true; |
| 73 | + } else { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + count() { |
| 79 | + return this.dataStore.length; |
| 80 | + } |
| 81 | + |
| 82 | + toString() { |
| 83 | + return JSON.stringify(this.dataStore); |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +// Implementation |
| 89 | +// ########################################### |
| 90 | +/* |
| 91 | + 1. Modify the Queue class to create a Deque class. A deque is a queue-like structure |
| 92 | + that allows elements to be added and removed from both the front and the back of the list. |
| 93 | + Test your class in a program. |
| 94 | +*/ |
| 95 | +console.log('CHAPTER 5'); |
| 96 | +console.log('### Excercise 1'); |
| 97 | +const deq = new Dequeue(); |
| 98 | +deq.enqueue('Jane'); |
| 99 | +deq.enqueue('Jhon'); |
| 100 | +deq.enqueue('Sam'); |
| 101 | +deq.enqueueFront('Emilia'); |
| 102 | +deq.enqueueFront('Mother of Dragons'); |
| 103 | +deq.enqueueFront('Kalisy'); |
| 104 | +console.log('Original Queue Stack'); |
| 105 | +console.log(deq.toString()); |
| 106 | +deq.dequeue(); |
| 107 | +deq.dequeue(); |
| 108 | +deq.dequeueBack(); |
| 109 | +console.log('2 Dequeue and 1 Dequeue from back'); |
| 110 | +console.log(deq.toString()); |
| 111 | + |
| 112 | +/* |
| 113 | + 2.- Use the Deque class you created to determine if a given word is a palindrome. |
| 114 | +*/ |
| 115 | + |
| 116 | +function isPalindrome(word) { |
| 117 | + const letter = new RegExp('[a-z]'); |
| 118 | + let palindrome = word.toLowerCase().split(''); |
| 119 | + |
| 120 | + palindrome = palindrome.filter(item => letter.test(item)); |
| 121 | + const queue = new Dequeue(palindrome); |
| 122 | + |
| 123 | + while (!queue.empty()) { |
| 124 | + if(queue.count() > 1) { |
| 125 | + a = queue.front(); |
| 126 | + b = queue.back(); |
| 127 | + } else { |
| 128 | + console.log(`The string '${word.toUpperCase()}' is palindrome!`); |
| 129 | + break; |
| 130 | + } |
| 131 | + if(a === b && queue.count() > 1) { |
| 132 | + queue.dequeue(); |
| 133 | + queue.dequeueBack(); |
| 134 | + } else { |
| 135 | + console.log(`The string '${word.toUpperCase()}' is not palindrome!`); |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +console.log('\n\n### Excercise 2'); |
| 142 | +const word = 'racecar'; |
| 143 | +const word2 = 'A man, a plan, a canal: Panama'; |
| 144 | +const word3 = 'what is this'; |
| 145 | + |
| 146 | +isPalindrome(word); |
| 147 | +isPalindrome(word2); |
| 148 | +isPalindrome(word3); |
| 149 | + |
| 150 | +/* |
| 151 | + 3. Modify the priority queue example from Example 5-5 so that the higher-priority |
| 152 | + elements have higher numbers rather than lower numbers. Test your implementation |
| 153 | + with the example in the chapter. |
| 154 | +*/ |
| 155 | +// Class Patient for Exercise 3 |
| 156 | +class Patient { |
| 157 | + constructor(name = 'generic', code = '0') { |
| 158 | + this.name = name; |
| 159 | + this.code = code; |
| 160 | + } |
| 161 | +} |
| 162 | +class GenericQueue extends Queue { |
| 163 | + constructor() { |
| 164 | + super(); |
| 165 | + } |
| 166 | + |
| 167 | + dequeue() { |
| 168 | + let priority = this.front().code; |
| 169 | + let position = 0; |
| 170 | + for (let i = 0; i < this.count(); i++) { |
| 171 | + if (this.dataStore[i].code >= priority) { |
| 172 | + priority = this.dataStore[i].code; |
| 173 | + position = i; |
| 174 | + } |
| 175 | + } |
| 176 | + return this.dataStore.splice(position,1); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +console.log('\n\n### Excercise 3'); |
| 181 | +const ed = new GenericQueue(); |
| 182 | +const p = new Patient("Smith",5); |
| 183 | +ed.enqueue(p); |
| 184 | +const p2 = new Patient("Jones", 4); |
| 185 | +ed.enqueue(p2); |
| 186 | +const p3 = new Patient("Fehrenbach", 6); |
| 187 | +ed.enqueue(p3); |
| 188 | +const p4 = new Patient("Brown", 1); |
| 189 | +ed.enqueue(p4); |
| 190 | +const p5 = new Patient("Ingram", 1); |
| 191 | +ed.enqueue(p5); |
| 192 | +console.log(ed.toString()); |
| 193 | + |
| 194 | +while (ed.count() > 0) { |
| 195 | + let seen = ed.dequeue(); |
| 196 | + console.log("\nPatient being treated: " + seen[0].name); |
| 197 | + console.log("Patients waiting to be seen: ") |
| 198 | + console.log(ed.toString(), '\n'); |
| 199 | +} |
0 commit comments