Vivid Metaphor of Data Structures

Data structures are abstract, sometimes hard to be understood. However, vivid metaphor helps us to master them better.

Data strcutres included in this article:

  • stack
  • queue
  • linked list

Stack

First, let's see stack's core features:

  • push( item ): push an item at end
  • pop(): remove an item from end
  • peek(): get the end element

I 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.

  • we push a chip into box: push( chip )
  • we take a chip from box: pop()
  • current top chip in box: peek()

Queue

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 front
  • peekFront(): get the front item
  • peekEnd(): get the end item

Suppose there're some people queuing to use ATM.

  • at end comes a new guy: enqueue( person )
  • front guy has done and leave: dequeue()
  • current front guy: peekFront()
  • current last guy: peekEnd()

Linked List

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 }

Thanks for your reading. Welcome to subscribe my blog by Github.

Post Time: 2019/1/17
Category: Technology/Data Structure
Author all rights reserved reprint please indicate the source no commercial reprint
Copyright © 2017-2021Terry SuALL RIGHTS RESERVED