Arrays, Lists, Queues, and Stacks (at the Water Park)
Editor’s Note: This article was originally published to Tech Elevator Instructor Ben Langhinrichs’ Linkedin. You can visit the original post here.
Time for a little more fun with early programming concepts. Most modern programming languages such as Java and C# have collections, groups of values of the same type collected together in different ways. It can be a little hard to visualize how the different types of collections work. So, let’s go to the water park to explore.
The Array is like a narrow parking lot outside the water park with a single row of parking spaces. You can only have as many cars as you have parking spots. Any specific car, such as the third, can drive away. but the parking space is still there and only one new car can drive into that open spot. Anybody else has to wait or visit a different water park.
The List is like a row of rubber rafts bobbing on the water in a narrow channel inside the water park. The channel starts out empty, and every new rubber raft with riders goes to the end. Except, sometimes somebody is pushy and wants to be the third raft, so they go to that spot and shove the other rafts a bit to make room for the new raft. The riders of the other rafts may grumble, but they move back or forward to make room. Likewise, somebody may want to grab their raft from somewhere in the middle of the channel and go home because Suzie feels so nauseated from the motion of the water, so they just pull the raft out, in which case all the other rafts squidge forward and back to make a single row again.
The Queue is like the List but it is watched over by Gerald the Grumpy Guard who has an oversized sense of his own power. Gerald will only let the rafts enter at one end of the channel and exit out the other end, so the first one in is the first one out and that’s it, quit your whining!
The Stack is like the Queue except Vanessa the Vicious Vandal blocked off one end of the channel with large wet mattresses coated in cement, and Gerald the Grumpy Guard still won’t let anybody come or go from the middle, so the last one in is the only one who can get out again, even if they gripe that they just got there and it is no fair!
I hope that helps you visualize these four Collections a little better. But even if it doesn’t, now you’ll have an excuse to go to the water park to “study” and ponder the peculiarities of programming concepts while you float on your rubber raft.
About Ben Langhinrichs
Ben has been developing software in a variety of languages and environments for many years. He has focused in recent years on high performance middleware services using C++ and Java, and data transformation code used to drive large scale web applications. Ben enjoys teaching and inspiring others which he gets to do every day as an Instructor at Tech Elevator Cleveland.