Implementation of the queue
Queue class function Deque () {this.datastore = []; This.enqueuefront = Enqueuefront; This.enqueueback = Enqueueback; This.dequeuefront = Dequeuefront; This.dequeueback = Dequeueback; This.front = Front; This.back = back; this.tostring = toString; This.empty = Empty;} Queue-Team first function Enqueuefront (element) {This.dataStore.unshift (element);} Out team-team first function Dequeuefront () {return this.dataStore.shift ();} Queue-tail function Enqueueback (element) {This.dataStore.push (element);} Out of team-tail function Dequeueback (element) {return This.dataStore.pop (element);} Reads the element function front () {return this.datastore[0] of the first team;} Reads the element function back () {return this.datastore[this.datastore.length-1] of the end of the queue;} Displays all elements in the queue function toString () {Let retstr = '; for (Let i = 0; i < this.dataStore.length; ++i) {retstr + = ' ${this.datastore[i]}\n '; } return retstr;} Determines whether the queue is empty function empty () {if (this.dataStore.length = = = 0) {returnTrue } else {return false; }}
Practice one. Modify the queue class to form a deque class. This is a data structure similar to a queue that allows elements to be added and removed from both ends of the queue, and therefore also called a two-way queue. Write a test program to test the class.
Bidirectional Queue class function Deque () {this.datastore = []; This.enqueuefront = Enqueuefront; This.enqueueback = Enqueueback; This.dequeuefront = Dequeuefront; This.dequeueback = Dequeueback; This.front = Front; This.back = back; this.tostring = toString; This.empty = Empty;} Queue-Team first function Enqueuefront (element) {This.dataStore.unshift (element);} Out team-team first function Dequeuefront () {return this.dataStore.shift ();} Queue-tail function Enqueueback (element) {This.dataStore.push (element);} Out of team-tail function Dequeueback (element) {return This.dataStore.pop (element);} Reads the element function front () {return this.datastore[0] of the first team;} Reads the element function back () {return this.datastore[this.datastore.length-1] of the end of the queue;} Displays all elements in the queue function toString () {Let retstr = '; for (Let i = 0; i < this.dataStore.length; ++i) {retstr + = ' ${this.datastore[i]}\n '; } return retstr;} Determines whether the queue is empty function empty () {if (this.dataStore.length = = = 0) {return true; } else {return false; }}//Test Let D = new Deque ();d. Enqueuefront (' a ');d. Enqueuefront (' B ');d. Enqueuefront (' C ');d. Enqueuefront (' d '); D.enqueuefront (' e '); Console.log (D.datastore); ["E", "D", "C", "B", "A"]d.enqueueback (' a ');d. Enqueueback (' B ');d. Enqueueback (' C ');d. Enqueueback (' d '); D.enqueueback (' e '); Console.log (D.datastore); ["E", "D", "C", "B", "A", "a", "B", "C", "D", "E"]d.dequeuefront ();d. Dequeuefront (); Console.log (D.datastore); ["C", "B", "A", "a", "B", "C", "D", "E"]d.dequeueback ();d. Dequeueback ();d. Dequeueback (); Console.log (D.datastore); ["C", "B", "A", "a", "B"]
Two. Use the previously completed Deque class to determine whether a given word is a palindrome.
function isPalindrom (word) { let d = new Deque(); let max = word.length; for (let i = 0; i < max; ++i) { d.enqueueBack(word[i]); } while (d.dataStore.length > 1) { if (d.dequeueFront() !== d.dequeueBack()) { return false; } } return true;}// 示例console.log(isPalindrom(`racecar`)); // trueconsole.log(isPalindrom(`ada`)); // trueconsole.log(isPalindrom(`mazey`)); // false
JavaScript data structures and algorithms-queue exercises