Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In this guide, we're going to talk about how we can build a Python implementation for the abstract
stack data structure, so by definition, a data structure is something that is abstract.
It's more of a concept than something you can actually touch and feel.
And so that can make it a little bit tricky to understand.
And so what I'm going to do in this guide is walk through what a stack would look like in a real life
application, and we're going to use the Python programming language in order to do it.
And don't worry, if you don't have any experience with Python or maybe even even if you don't want
to learn Python, that's perfectly fine.
Part of the reason why I picked it out was one, it's one of the most popular programming languages
out there, but also because Python is very close to just spoke in English.
And so even if you don't have any experience with it, you should be able to follow the logic.
And hopefully it'll help you understand at a more practical level what stacks are and how you're able
to use them.
And so we're going to create a stack class in this guide so a stack and will create this class and say
Stack and then Perens and then give a semicolon.
And that's how you can create a class in Python.
But I'm not going to spend a lot of time talking about the syntax of the Python programming language.
Instead, I'm going to talk about how you can build a stack using code.
Now, a stack has several key processes that you need to be able to do.
So the very first thing is you need to be able to add to a stack in the way that we add to a stack is
with the word push.
So we're going to create the ability to push items onto the stack.
Remember, the easiest way to think of a stack is to think of a stack of books or a stack of anything.
You start with one item, then you add another item on top of that.
Then you add another item.
On top of that, you could say you push an item on top of that and then by the end you have this full
stack of items.
Now, if you want to take an item off, then you pop it off of the stack and you keep on popping until
you don't have any items left in that stack.
And so that is exactly what we're going to build, is the ability to add and the ability to remove.
And then there's one other method that is quite helpful, which is called peek and a peek method allows
you to simply look at the last item on that stack.
So we're going to build out those three in order to create a stack.
We're going to start off with what what's called a initialize or function.
And this is going to give us our initial list.
Or if you're coming from another programming, language lists in Python are what are called arrays and
other programming languages.
So we're going to say we're going to start with def.
And then two underscores Annet followed by two underscores and then pass self into it.
Once again, we're not going to talk a lot about Python.
This is simply giving us a way of starting.
So any time we create a stack, we're going to start with a empty list of items.
And so we can do that by saying self items equals this empty list.
And that's all we need to do in this init method.
Everything else from this point on are going to be to create those methods, the push, the pop and
then the peak, and then I'll create one more that can be pretty helpful, which gives us the ability
to retrieve all of the items in a stack.
So let's start off with push.
So I'm going to say Def Push.
And in Python, whenever you have a method inside of a class, you first pass self to it so that it
can access those items.
Because what we're trying to do is we want to have the ability to add on to this items list.
So we need to have access to that stack.
So we're going to say push self and then we're going to take in an item.
Now, don't let the word item or items confuse you.
We could call this ASDF, we could call it X. We could call it whatever we want.
These names are completely up to us.
I think item makes the most sense.
Follow that with a semicolon and then we're going to say sell items dot append.
This is something specific to Python and many, many programming languages like Ruby and some other
languages actually have a push method because the word push is so pop.
You learn because when people are learning algorithms and data structures, they're used to using words
like push, but Python has an append method and then we're simply going to upend an item to it.
So this is pretty basic.
This is only one line of code inside a push where we're taking an item and we're going to push it.
We're going to add this on to this list of other items.
So now let's create the pop method.
So I'm going to say def pop and pop is only going to take in self as an argument, because when we call
the pop method, all we're doing is removing an item from the stack and that's it.
So with Pop we're going to say after a semicolon, return self items and then pop.
Now don't let this confuse you.
If you're not familiar with Python, Python actually has a pop method on top of their list module.
So right here, when because we're using these brackets, that's a list.
Pop is a method that you can call on a list and it removes the very last item.
It's literally doing exactly what our pop method here is doing.
Technically, we could also do something even more manual.
We could do something like self items and then here we could say negative one in brackets.
And this would do something pretty much the same ID grab that very last item off.
I don't really like that because the pop method allows for us to use something native.
It's directly built in to the python programming language.
And so it gives you the ability to not have to deal with edge cases because if you use that negative
one bracket syntax, then you'd have to also check to see are there items in the list?
Because if it's a empty list and you call Pop and you did that negative one, you'd get an error.
So we don't want to deal with that.
We're simply going to use the native method built in.
So we're creating our own version of pop here, but it's in the stack class.
This pop method here is in the list module directly for Python.
And we're saying when we call Pop, we want to access the very last item in our stack in our list.
And then lastly, let's call before we build the peak method.
Let's call create a method here called get all we're going to pass in self to it.
And then this is going to return self Dott items.
So this is simply going to return that full list.
It's going to return all of the items in here.
And this is going to give us to a point where we can test this out.
So let's give ourselves a couple of lines and then I'm going to create a set of users here.
You could use anything you want, but I'm going to create a variable called users.
I'm going to initialize a new stack.
So what that means is we're saying that this stack class right here, we are creating an actual object.
We're creating something that we can store in a variable that is using the stack class as a blueprint.
And then this is something where we're going to be able to push pop and then get all.
So with users, we're saying we're creating a new instance of the stack class and then let's push some
items onto it.
And the way we can do that is saying users don't push and then we're going to pass in our item.
So we're going to say push and let's push Jordan and let's call this a few more times.
We'll do push for Tiphanie and then push for Christine just like this.
Let's see if this is working.
So you say print users dot get all and then we'll see if this gives us all of these items, all of these
users in our stack.
So I'll switch over here and let's just run this python stacks and then this is just a stacked up Y
module.
Now, if you do not have Python installed on your system and you either you don't want to go through
the process of installing it or anything like that, but you'd still like to follow along.
A really helpful tool is called Reppel It and it's completely free of a free version.
And you can actually create a you have to create an account, but then you can create a on line Reppel,
which gives you the ability to write Python code along with some other programming languages directly
in the browser.
So if you are wanting to follow along but you don't have Python installed, you don't.
Have a local development environment, then you could type all of that code directly into it and then
you'll be able to do that and I'll include a link to that in the show notes.
OK, so here I've saved this file and now let's run this code.
And there we go.
We have our list.
We have our stack of Jordan, Tiphanie and Christine.
So all of this is working perfectly.
Now, let's see what this looks like if we start to call some of those methods.
So right under users get all this gives us the full list of users.
Now, let's call users dot pop.
And then I'm going to simply call this method and actually let's store this.
You don't need that.
I'm sorry.
I'm used to writing JavaScript code lately.
So let's say that we want to pop a user off.
We're going to say removed user and we're going to set it equal to users.
Dot, pop.
The reason why we can do that is notice here I said return selfie items, not pop.
This is going to actually return.
The user that's getting removed, which this can be very helpful in the pop method, is when you're
working with data structures, it expects that you're actually going to return that value.
When you call pop, you're not just deleting it from the stack, you're returning it so you can do something
with it.
So remove user and then lets print out that user.
And then now let's also print get all again.
So what this is going to do, it's going to say when we call, get all here, it's going to be at this
state where we just added those three users.
Now, we popped a user off the stack.
We're printing that out.
And then we're going to see what our get all method returns after we've popped.
Because if all of this works, then it should print out the three names we added, but then it should
print out the single name we removed.
And then lastly, it should remove or it should show the new updated stack with that last user removed.
So let's see exactly how this works.
Let's run it again.
And there we go.
That work perfectly.
So we have the full set of names.
We removed a name and then it returns and shows us the new updated version of the stack, which has
a person removed.
So this is working exactly like how we'd imagine.
Remember, back to the book example.
When we take a book off of that stack of books, then what's left is all of the previous items that
we added.
So this is giving us exactly what we're looking for.
We only have one other thing that we're going to do, which is build out that peak method.
So let's build that out.
I'm going to say Def Peak and then Peak is going to call Self Peak is very similar to Pop, except we're
not removing an item in this case.
We are simply going to see the very last item and that last user that is in the stack.
So we have to do one check here.
So I'm going to say if not and then I'm going to check the length of self items and I'm going to say
so if the length.
And so this is just saying, like for this name, Jordan, Tiffany and Christine, this right here,
the length is going to be three.
If we run it here, it's going to be two.
If we call it when there are no items in the stack, it's going to be zero.
So what we're checking here, I'm going to say as long as the length of items is not equal to zero.
So in other words, as long as it's not empty, I want you to show and return self items and then negative
one just like this.
So kind of like I was telling you about how you it would be possible if we extended the pop method,
we could rewrite that using the last item and we could manually remove it.
No reason to do that because we have pop here and that built-In method does all that work for us here.
What we're saying is I want to return the value.
Now, this is not going to remove it.
Peak only gives us access to view that data, but it's not going to remove it.
And so we can test that out here.
So instead of saying users dot, pop and removing the user, I'm going to say last user here and call
users dot peak and then print out the last user.
And now let's see what the difference is, because this is really helpful to tell the difference between
peak and push.
So let's run this one more time and there we go.
That's all working perfectly.
When we ran it the first time, it showed our three items in the stack.
Then we peaked at we got access to view that very last user.
And then when we ran it all again, we saw that all of those items are still in the stack.
So hopefully that shows you the difference between peak and pop.
So that is really it when it comes to implementing the stack data structure.
This gives us the ability to create a stack to push items onto the stack, to remove items, the very
last items that were added to the stack and then also to peak or to view the very last item that was
added.
This is the stack data structure encode written in Python.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.