JavaScript data structures and algorithms-queue exercises

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.