Java Essentials - The stack class in java
20K views
Oct 24, 2024
Java Essentials - The stack class in java
View Video Transcript
0:00
In this video we are discussing the stack class in Java
0:04
We know that stack is one data structure which supports the lasting fast-out or fast-in-out data structure
0:12
This stack class is a sub-class of vector. So let us go for more discussion on it
0:19
So the stack is a sub-class of vector and that implements a standard last-in fast-out stack
0:27
a stack has all the methods of the vector class but it has got its own methods too so all the
0:35
methods can be accessed from the stack class which was there in the vector so but stack class is
0:41
having some separate methods defined within it the first one is the push to insert into the stack
0:48
at the top position to insert some element at the stack at the top position we're having the
0:53
method called pop delete the top most element from the stack then we're having this
0:57
pick then return topmost element not deleting it just returning the topmost element
1:03
we having this empty to check whether the stack is empty or not we having this search and search elements in the stack content so these are the very important methods which you use in our stack when you will when you be writing our java
1:18
code so now let us go for one practical demonstration to give you a better idea how this stack
1:23
class can be written can be written in our java code and how the program will produce our
1:29
desired outputs so here is the demonstration for you we have defined one stack object
1:36
stack class object that is a my stack which will hold the integer wrapper class
1:41
objects as its members so is the new stack constructor so now how to put our
1:47
elements that is the integer objects within this my stack stack object here so
1:53
stack is the name of the class my stack is the object under the stack class so the
1:58
method is push with the help of push we can we can put our integer objects
2:03
within this my stack object so you can find that my stack dot push 10 then 20 then 30 then 40 then 50 so 50 will be at the top
2:13
and 10 will be at the bottom here so now we are going to print the size of this my stack so as we have pushed five such elements so that why five such integer class objects so that why the size stack size is five here now we
2:29
going to introduce one method there is a peak this particular peak method
2:34
will actually check the topmost element of the stack but it will not delete it
2:39
from the top it will only just check the current content of the topmost element of
2:44
the stack here so when you are going for this peak and later when you are printing
2:48
that is a my stack dot pick method we are printing the current content whatever has been
2:53
picked it is printing 50 here so now using this while body so not my stack dot is empty
3:01
so is empty will be returning true and when the is empty will be returning true
3:07
then I should come out from this while so that's why I have used the knot operator
3:12
there so true knot of true will become false so it will come out from this while
3:16
So that why while not my stack dot is empty so that is another method you are getting defined under the stack class here so that is our is empty and here you can find that is empty is a method which returns the bullion and it returns the bullion it is defined under the actual class is a vector and is empty is the method and this vector is under the package java dot util so as this particular
3:44
while will be executing until the stack is getting empty so the while will get execute and each and every iteration will be picking up one of the elements from the stack so my stack dot pop is the respective method which returns this integer class of the
4:00
we can because here we are having this my stack we defined that it will be holding
4:05
the integer wrapper class objects and the method is a the public e and then pop
4:11
removes the object at the top of the stack and returns that object as the value
4:15
of its of this function so in this way it will return so let me go for the
4:20
output you can find that 50 has been returned at first then 40 then 30 then
4:26
20 and then the bottom most element then will be returned at the last
4:30
and then my stack dot is empty will return true not of true will become false so it will come
4:36
out from this while loop in this way in this particular demonstration we have
4:40
discussed that how to deal with the stack class objects in our Java code thanks
4:45
for watching this video
#Computers & Electronics
#Java (Programming Language)
#Programming