From the course: Programming Foundations: Algorithms

Unlock the full course today

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

Stacks and queues example

Stacks and queues example

- [Instructor] Okay, let's take a few moments and walk through some examples using a stack and a queue. Some languages like C sharp, for example, have dedicated built-in data types to represent these data structures. But in the case of Python, we can just use a regular list to represent both a stack and a queue. So I'll start by opening up the stack starting point in my start code, and I'll just begin by declaring an empty list that's going to serve as our stack. Next, I'll add some items to the stack by using the append function that the list type supports. So let's see, I'll write stack dot append. And some languages might use the push function, but in this case, append does the same thing. So I'll write append one and we'll just put a few of these on there. So we'll do two, three, and four. Okay, so at this point, I've pushed four items onto the stack, so let's go ahead and print the stack to see what it looks like.…

Contents