1

Learning about data structures in details. Checked few js tutorials online and they seemed to use array for everything.

Like this:

class Stack { 

    // Array is used to implement stack 
    constructor() 
    { 
        this.items = []; 
    } 

    // Functions to be implemented 
    // push(item) 
    // pop() 
    // peek() 
    // isEmpty() 
    // printStack() 
} 
4
  • Does this answer your question? How do you implement a Stack and a Queue in JavaScript? Commented Jan 3, 2020 at 12:12
  • Yes. Javascript arrays behave like linked lists in low-level languages Commented Jan 3, 2020 at 12:13
  • Array::pop() and Array::shift() let you handle an array as stack or queue Commented Jan 3, 2020 at 12:14
  • A stack - use a regular array and use the .push() and .pop() methods. A queue - use a regular array and use the .push() and .shift() methods. Almost everyone simply use the arrays directly instead of wrapping them in an extra object layer Commented Jan 3, 2020 at 12:14

1 Answer 1

1

Actually, there is no existing object containers for the likes of stack and queue but there are a handful of techniques on how you efficiently implement them.

refer to these links: https://chevtek.io/9-javascript-tips-you-may-not-know/ https://yuiazu.net/2019/02/19/stack-and-queue-in-javascript/

hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.