Some data structures are named after real-world things.
A ‘stack’ is a metaphor for a real-world stack of items. Imagine a stack of pages. You can only access the top page. Notable, the first item to be placed in the stack is at the bottom, meaning it is always the furthest away from you. Also, the latest page to be left on the stack is on top for you to see.
A ‘queue’ is a metaphor for a real-world queue, where whatever was added to the queue first will get looked at first, and whatever was added last will be looked at last.
In computer science, we make these things extremely strict. Like with a real-world stack of papers, if you wanted to sneak a peak at the middle page, you could. Or with a real-world queue, someone might barge in and skip to the head of the line. In computer science, we instead pretend that these rules are never broken. The stack is only ever accessed at the top of it, both to add and remove data, and we can only ever add to the back of a queue and take from its front.
–
Making sure that things follow these rules can be helpful for some programs.
You might think you should just let everything be accessible at all times for the sake of convenience, but this can get confusing (or unsecure), and sometimes knowing for sure that something follows these rules is helpful for working out how to reat the data that comes in that way.
In computer science, we use data structures like this to help us. We also sometimes invent new data structures that don’t have a real-world metaphor, but do have some digital properties we like.
Latest Answers