Stacks and Queues in Verse
Overview
Stacks and queues are common data structures in computer science that specify the order in which you can add and remove elements. You can use them to control the order in which you interact with data:
- Stack: a container where you insert and remove elements in a first-in, last-out (FILO) order, such as picking a book off the top of a stack of books to read.
- Queue: a container where you insert and remove elements in a first-in, first-out (FIFO) order, such as processing a line of customers waiting for a meal.
Although the underlying code for stacks and queues are similar, they each have key differences and strengths you can leverage to make your experience fast, efficient, and intuitive.
Stacks
You can think of a stack as a stack of pancakes. Suppose you’re at a breakfast restaurant, and your waiter gives you an endless stack of pancakes. Because you want to be polite, you’re only going to take one pancake at a time, and only from the top of the stack. You can ask your waiter to push a new pancake to the top of your stack, and when you’re hungry you can pop a pancake off the top of the stack and onto your plate to eat it. If you want to eat a specific pancake in the middle of the stack, you’ll have to keep eating pancakes from the top till you get to the one you want. The first pancake added is going to be the last one you remove. This makes a stack a FILO, or first-in, last-out data structure.
You're reading a preview
The full reference is free for BrainDeadGuild Discord members — sign in to read it all, or open the original at the source.
Sign in with your BrainDead.TV / BrainDeadGuild Discord account for full access.