Javascript list like data structure?

11,553

Solution 1

It sounds like the JS Array is exactly what you're looking for.
You should be able to use the push and pop functions for the stack-like data structure and splice for the rest of it.

Solution 2

Actually Array supports push and pop operations: JavaScript Array Object

Solution 3

You don't have to recreate the Javascript array for each removal. Javascript Arrays have push() and pop() methods to add and remove elements:

JavaScript Array Object

Share:
11,553

Related videos on Youtube

Manux
Author by

Manux

Updated on April 29, 2022

Comments

  • Manux
    Manux almost 2 years

    It seems, if I'm not wrong, that because of the way Javascript handles Objects it is unpractical or inefficient to implement linked lists.

    I would need a data structure in which I could easily do 2 operations(apart from indexing), appending at the end and removing (popping) an object at a given index.

    Is using an Array and "recreating" it for each remove operation the optimal solution? I would think not.

    Any ideas?

  • Tony Miller
    Tony Miller over 13 years
    As well as shift and unshift. Note the footer on the listed page that provides "try it now" versions of all the methods.
  • Manux
    Manux over 13 years
    I am stunned, and I got totally misinformed... I'll have to check next time I ask someone if he's good in JavaScript... And thanks, splice was what I was looking for :)