Data structures are different ways to organize information, that are useful for different things. Usually they have two parts: a description of how the data is laid out, and a description of the actions you’re allowed to take on that collection of data. The way software is built, it’s usually to make a sort of logical “box” that you can _only_ interact with through those fixed actions. That’s really important, because it lets whoever maintains that box fix things, change things, and do all kinds of maintenance without everyone who uses the box needing to know about it – so long as that “contract” of what the actions are and what happens is followed, all its users get the same behavior. You’ll sometimes hear the word “API,” which is short for “application programming interface,” which is the name for that contract.
One simple example of a data structure is a list, also called an “array.” You can see how many items are in a list, look up any element of the list by position, change any element of the list by position, extend the list with new items, or shorten the list. This is useful for all sorts of things, so it’s one of the most basic data structures.
Another example of a data structure is a queue. A queue is useful for things like keeping track of a TODO list, but has many other uses as well. Its organization of data is like a list, but it allows far fewer actions. You can look at the list like any other list, but you’re only allowed to change it in two ways: add something to the queue (“get in the back of the line”), or remove (and get the value of) the item at the head of the queue. The rule is “first in, first out,” so part of the definition of that second action is that if you add A then add B, when you “dequeue” items you’ll get out A, then B, always in that order.
The restricted set of actions let everyone rely on the queue behaving like a queue of people in a shop. You know that so long as you’re dequeueing, everyone will ultimately get served, and things like that. If people were allowed to “break the API boundary” and modify the underlying list using actions _not_ allowed by a queue, then none of the users could predict anything!
(This is one part of why, when someone tries to be “clever” and fix a problem by breaking those rules, more experienced engineers will usually smack them upside the head. There are exceptions but you need a lot of experience to understand the when, the why, and the how to do that safely.)
There are lots of other data structures – records, linked lists, double-ended queues, priority queues, stacks, heaps, atomic words, and many more. Each is one of the basic tools used to build software.
Latest Answers