Data structures are abstract, sometimes hard to be understood. However, vivid metaphor helps us to master them better.
Data strcutres included in this article:
First, let's see stack's core features:
push( item )
: push an item at endpop()
: remove an item from endpeek()
: get the end elementI found a very visual example before, that's an open chips box.
And to corresponse with our development habbit(count index from left to right), I push it down.
push( chip )
pop()
peek()
Queue is similiar to stack, but easiler to learn.
Queue's key features are:
enqueue( item )
: push an item at end dequeue()
: remove an item from frontpeekFront()
: get the front itempeekEnd()
: get the end itemSuppose there're some people queuing to use ATM.
enqueue( person )
dequeue()
peekFront()
peekEnd()
Linked list consists of nodes, and each node can have a link to another link, like a chain.
Mock its main concept via codes:
const nodeC = { link: null }
const nodeB = { link: nodeC }
const nodeA = { link: nodeB }
const linkedList = { head: nodeA }