Stacks

4 minutes
Share the link to this page
Copied
  Completed
he Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

Transcript

Hello, in this Java programming video, we are going to look at the data structure known as a stack. So a stack allows you to store essentially any sort of data type any sort of value in there. The interesting thing about it is LIFO Li So cheers. You guessed it, well, good laugh in front of you like a stack of books. If you stack up books, logically, you would, you know, get the top one first, not the bottom one, because the stock will fall down unless you're Houdini. So two quick stat.

It's really simple. words for the SDK equal equals new. And now to add something to the stack, you just do FTK push that push Whatever you want on the system number one, I want to do push on question number two, if you can push again, on to the number three SDK. Now I'll put a in the mall that gets more when you know 4.9 for example, and SDK dot push. And I'm gonna go one step further for strings and say hello. Like so.

So we can print out the entire stack by doing this the system out print ln, SDK, we run this. There we go. You got your printed out. My thinking, Okay, when would we want to print all of it out? Probably not often. You might want to print out one of them.

Would you just use one of them access one of them now Easy as long as the system again, you don't have to do system behind the printing out. To access it, you just do FDK get an index. So this works very similar to arrays. Zero is the first one. Two is the third one. So it's literally zero and maybe almost all four.

That will be the fifth one, which is right here. There we go. So we've got all of that. So what did this mean for me for it lastin. First out, so the last one in was this one right here. This got pushed onto the stack, this one, this one and this one.

This one. You can think of the stack sort of looking like this. Some coins. The stack sort of looks like this. In the first one, you want them, turn through them 4.9 and then Hello Want to remove them from the stack, the first one will get removed. So to remove it, you just do FTK dot pop, so pops it on the stack.

And if you heard that on my slide, one of my bones sort of popped in my hand as well. Now that didn't but it was pretty cool as well. And then if it is system, printer printed all out so you can see what it looks like now. There you go, the last one has been popped, I want to step further and I duplicate this as you see, two of them have been now popped from it. So the order in which they are added they are moved in the reverse order. You may have seen a lot of other methods by checking if it's empty getting the size of it which is done in some whether they saw us there was a sewing method as well can't find it right at the top and the variety of other stuff, feel free to check them out or recommend a can look at them to see what you could do.

So that's it for the stack data structure. If you have any questions, feel free to reach out and I look forward to seeing you in the next awesome Java programming video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.