From the course: Programming Foundations: Algorithms

Unlock the full course today

Join today to access over 24,000 courses taught by industry experts.

Stacks and queues

Stacks and queues

- [Instructor] Let's take a look at two more common data structures, stacks and queues, and let's start with the stack. A stack is a collection of elements that supports two principle operations, push and pop. The last item pushed onto the stack is the first one popped off. That makes the stack what's called a LIFO data structure. Last in, first out. So if we had a stack with one item on it and then we pushed another item onto the stack, followed by another, we'd have a stack with three items in it. Pushing an item onto a stack is a constant time operation, since it doesn't matter how many items are already on the stack. Then we could take that stack and pop an item off the top in order to operate on it. And again, that's a constant time operation. Queues work a little bit differently. Like a stack, the queue structure supports adding and removing items, but it operates in a first in, first out method. If we had an…

Contents