Have you ever wondered how data is stored in a computer? There are multiple ways in which data is stored and we call them data structures. They are a way of organizing data so that it can be used efficiently. Some examples of data structures are: linear data structures, trees, hashes, and graphs. Today, we will explore some linear data structures.
A data structure consisting of a collection of elements, each that can be recalled by using indexes. Arrays have been around for a very long time and it is quite easy to pull data from an array. An array can be easily manipulated and thus is resizable. With how arrays are stored in memory, every time an array is changed, more memory space is required to store a new version of the array. You can see how this can lead to rather bad memory management.
A linked list is a linear collection of nodes that have a pointer which tells the computer where to look for the next node. Each node has its own value. The advantage of this is that it is easy to manipulate the nodes. When inserting a node or removing nodes, all that needs to be done is change the pointers and add/remove values. Because each node has a pointer, the computer only has to make room for the additional nodes without changing where the already existing nodes in memory.
A stack is a collection of elements that works with two main procedures. The first one is called push and it does exactly what you would expect it to do; it pushes an element into the collection. The second is called pop and again, does what you’d expect it to do but it pops out the most recent element that was pushed in. This can also be known as LIFO (last in, first out). This all happens at the top of the stack and not the bottom. A stack is meant to be limited (although it can be quite big) so once a stack reaches its capacity and is unable to add more to the stack, it is considered to be in an ‘overflow’ state (SO THAT’S WHERE STACK OVERFLOW COMES FROM!).
A queue is similar to a stack but this time it follows a more linear path. Think of a conveyor belt. The first product placed on the belt will be the first one to leave the belt — this can also be known as FIFO (first in, first out). This causes elements to be kept in a specific order. The back position, aka enqueue, is where the elements are added and the front position, aka dequeue, is where the elements are removed. This allows both positions to be accessible.
These are some basic linear data structures and each have their own cases on when to use. Knowing that these type of data structures existing is half the battle, it’s up to you on how and when you want to use them.