Hello and welcome to Java Point. And today, the topic of discussion is ST AC in data structure and we will study the C program
to implement stack. And what is we are going to use as using array
So using array, we are going to implement a stack. OK, fine
And we'll use which language C language. So I'm going to show you the program how we are creating a stack using array with C
language. Let's start. OK? So I'll, I'll show you the steps. So this
this page, this page is divided into two parts. So I'm dividing the page into two parts fine
So first of all, I have created a menu menu means what that is according to menu
you will ask the user to do either you want to push an element
either you want to pop an element, either you want to print element or you want to exit fine
OK. So let's start with the first line hash defined mac three. This is what this is a macro
this is a macro. And with macro, we have made a macro called Max and the value of max is three
So the max value is three fine. OK. So this is defined value
That value of max is three. We have created a function push, we have created a function pop and we have created a function print
So these are what these are what declaration of prototype? These are prototype
prototype. Fine. OK. I'll explain you each and every line here. What I'm doing here
I'm creating an array of size max. The value of max is what three
So that is I'm creating an array and I'm creating a vertical array. Why a vertical array
Because stack looks a vertical. So either it's not an issue that I can create it horizontal also
So, but it looks nice if I'm creating a hori vertical. OK
So ST Max, that value of max is three. So I'm creating an area of size three that is 01 and two and the name
of this area is ST fine. I'm also creating a variable top
The value of top is minus one. So I'm creating a variable top and obviously this top is value minus one Y because there is
no element in the stack. So the current value of top will be minus one fine
OK. Now, what am I doing is that these are all global variables
these are all global variables. Now, I'm creating a main function and in main function
I'm I have created a local variable ch I've cleared a screen uh If you are using a turbo C++ you can use clear screen
if you are not using do not use clear screen fine. While one
what is while one while and what is one? One is means condition is always true
So while one means condition always true, condition always true fine. So it will move inside
and it will print four line one, push two pop three print and four exist
Is it similar like a menu of a restaurant whenever you go to a restaurant and you ask for something for a menu
You always watch. You always see a menu is having some numbers 123 and four and 51 is for popcorn
two is for sandwiches and three is for pizza and four is for pasta and everything like that
And you just order, give you order, give me order for me the point number one or two or whatever you feel like
Ok, so you are having four options. That is one for push two for pop
three for brain and four for exit. So these are first part of my code and now I move to the second part
So now you have a menu and waited is standing in front of you and he's asking so what is your choice and the choice
So one print is asking for a choice. You have a menu where you have four options
12 and three and four in which what you want. I said I want push first of all I want to push
So what I said what I press, I pressed one, the moment I pressed one scan it scanned what one C has value one switch
will have value one. So the moment switch will have value one
What is switch switch pass jumps to the same case to the value it is having
it is having value one. It was, it will jump to the case one and case one
we are calling a function push the moment I'm calling a function push it will jump to the definition of push
So let's start with definition of push here. I'm calling a function push
So what will happen? I will jump to the definition of push
Let's push, let's jump to the definition of push. OK. Fine. So I came to the push function
OK. Fine. And here I've created a local variable item. So one box is created and the box name is item
This variable is created clear. The screen is top is able to max minus one
Let's make a, let's make a stack first. So stack was 012
This was ST stack and the current value of top was what
Minus one? Which I have already told you just see this the top value was minus one fine
OK. Now what is checking is that is top is equal to max minus one
What was the value of max three? Three minus one is what two is minus one equal to two
no minus one is not equal to two. This is false. The moment it gets false
So obviously, it's not overflow condition. It will come to the L it will increase top to top plus one
That is what I told you in the moment you want to push you first, increase the top. So top is going to top plus
one. The current value of top is minus one and plus one will make it zero. So top will point to zeroth position top
And then what I'll do, I'll ask the user to enter the item to insert
I have asked the user entered five. So what will happen? This item variable will have value five
So this item variable will now have a value five. And what am I doing is that ST that is ST top top is what
zero, ST zero will have item. And what is there in item five? So ST zero will have item what five
Hope you understood? Fine. OK. So now stack has one element and top is pointing to the zero position fine
It will come out of the L bar the moment it will come out of the push function
What will happen? Now understand the moment I come out of the push function
this item variable which was local will get vanished. Why? Because item is a local variable and the life of local variable is till the
control remains the block. And for this, you need to study the storage class
the item variable will get removed. Now you must be thinking, why not ST why not top
If I show you this, this ST this top all are global variables and the life of global variable is still the program comes to
an end current. Right now, I'm not out of the program. The moment I'm not out of the program
this variable will remain safe fine. Ok. The top will have value zero and D zero will have value five
Ok. Now, when you come out of this push function, where you will go
you will go from where you have received the call, where you have received the call, you have received the call from this place and
it will move to the next line. What is next line break? What, what is, what break will do break will take you out from
switch, you will come out of switch. This, this bracket is of for switch swi a this bracket is for a while and this bracket
is for main fine. This bracket is for main function. You have created three brackets
12 and three. So these are three closing bracket. The moment you get a statement break
it will come out of switch, then it will try to come out from while but while will not let it to come out because
while will ask to go and check the condition first, the moment I'll go and check the condition condition is again
one and one is what? True? The condition is again, true. So what will I again got trapped in this loop
And this loop is again giving me an option one for push two for pop three for print and four for that
It in a similar fashion. The moment you had the one of the menu
the waiter again comes to you and ask her what do you want next? So now you again said enter the choice
Waiter is asking to enter the choice. You said one again, you pressed one ch is having a value
one switch is having one again, it will move to the case one and case one is calling push
it will go to the push function fine. OK? It will again come
it will again come, it will create an item variable again. Why again
Because item is a local variable. So it will create an item variable again. OK
It will again check if top is equal to maximum. Now current value of top is zero and max minus one is two
no zero is not equal to two. Condition again becomes what it will come here
Top is going to top plus one. So top zero plus one is what top top will come to the next empty
I'll ask enter the item to insert, suppose I want to insert eight
So item will have eight. So item will have eight and at last what I mean
ST top top value is one ST one will have item. So ST one will have item and what is there in item eight
Fine. So it will be here fine. Ok. Now what will happen
The moment I come out of the LS, I'll come out the moment I come out of the push function
this item variable will get deleted because its life is finished. Now where it will go
it will go to the function where it has received the call. It has received a call from Bush
Fine. It again, see the break it come out of the switch again
it will come try to come out of the vile. But we will ask him to go and check the condition
Condition is again, one again, four menu will be pop and it will ask you again
what is your choice? Again? I have pressed one again, switch one again
case one again, push again, I'll come to the push again. I'll create an item fine
Again, I'll create an item. I'll move very fast. Now is top, is equal to max minus one
Top is what one? And what is max? Two? No, top one is not equal to two
I'll again, is the top top is going to top plus one? Top will, will come here fine
OK. I'll again, ask, enter the element to insert, I have insert nine
So item will have nine and what I'll do ST top top I two
ST two will have item. So what is there in item? Nine
So nine will come here. The moment I come out of L, I'll come out
the moment I come out of push this item will get deleted. This will be uh because it's a local variable
But where I'll go, I'll go to the function where I receive a call, I received a call from this
Again. The next line is break, I'll come out of the switch again is while while will not let me to go out
I'll go to the while. Condition again, condition through again. I'll see the 34 print statement
four menu statement. Again, somebody is asking me enter the choice again
I pressed one switch is again having one case one again, push function will be called
I'll come to the push. Now again, I'll create an item. Now again
I'll create an item again. Is top is going to max minus one
Yes. Now current value of top is two and max minus one is two and in front of you
So what I'll print overflow. See the condition. Now you are printing an overflow
Now you will not be able to insert an element fine. OK? So now you
so condition is now this condition becomes true. Now this condition becomes true and you will print the overflow that the moment you try to
print. After put after third element, you will not be able to find
So what will happen? You will not go to the ELTS part because it has been executed
You come out of the push where you will go push a statement break
you come out of the break, you come out of the switch while will not let you to come out
Again, you go and check the condition condition is true. Again, you will see four options
Now what you pressed three. Now what you pressed three, the moment you press the three ch is having what three switch
is having? Three, the switch will take you the case three. And in case three
we are calling a pre function that is print function fine. Now we are calling which function print function fine
So I'm calling a print function. So what now you see the current situation is 58 current situation situation of after pushing an element of
in ST was 589. So I'm calling a print function. So here I'm calling a print
It will jump to the print print definition here, printed definition and the current situation of stack was 589
So I'm making a condition five situate stack 58 and 901 and two
And top is pointing to second position fine. OK? It's checking its top is equal to minus one
No, the value of top is two. It's not equal to minus one condition become false
I come out I now I come to the part for I is equal to top
What is top two? So for I is equal to two. Yes
is two greater than zero. Yes. Condition two. What I'll do? I'll print stst two
What is there in ST 29? So I'll print nine over here
Where in mid of double codes? So I've created specifically, especially I've created this double codes and in mid of double
code, what you are doing D or what is percent DS T two. What is there in ST 29 and after nine where
what is their new line? So it will come to the new line again, it will jump and it will decrease the value of I
from 2 to 1. So the current value of I will become one OK
Fine is one greater than zero Z. Yes. So it will put print ststst one
ST one is eight. So it will print like this. So you will feel uh you will have a feel of stack
it will print nine and then eight. OK. After printing what we will do
it will come to the new line, it will come to the new line fine. OK
It will come to the new line. It will again decrement. The value of I I will become zero is zero
greater than zero. No, but it's equal to zero condition again become true. It will print ST zero
What is ST 05? So it will print five like this. What will happen
It will again, decrement, I I'll become minus one is minus one greater than zero
No, it becomes false. The moment it becomes false, you come out of the L spot or if you come out of the loop
you come out of the L spot, you come out of the function. And here is the output
The value of value in the stack are 58 and nine and nine is the top element
So here is the best way how you can print element into the track element of the track
So what happens now the moment you come out of the print function where you will go
you will again go to the place where you have received the call, you have received the call from this place
And the next line is break. Break means you come out of the switch while will not let you to come out
So what we will do while say check the condition but after checking the condition
I found condition is true. So again, I'm getting a four option one for push two for three
for print four for exit. And now what I did I have pressed two
So now C is having two switch is having two and switch
C will take me to case two and in case two, I'm calling which function pop function
So I'm now going to show you the working of pop function
So I move to the pop function. And the current situation of stack is 589
So I'm going to make this 589 over here fine. So I'm going to make this five eight and nine
that is 01 and two and top is pointing to the second position
Fine. OK. Again, what I'll do, I'll make a variable item. OK
This item is a local variable. I have created this local variable. I've cleared the screen again
I'll say if you're using Turbo C then use clear screen. If you are using close code blocks
then do not use this clear screen function. If top is equal to minus one
no top is not minus one, top is equal to two. So this is false
I'll come to the E part. What I'm doing item is having ST two
What is there in this? This sstst two is having value nine
So this item will have nine fine. What I'll do, I'll print deleted element is percent D
So this statement will get printed deleted. Element is going to percent D and D
What will be printed? Item item is having nine. So I'll see that item deleted is nine
It will be printed. That is this element is deleted automatically. It's not like
but we'll see that it's deleted and top will decrease top minus minus
So top will come to this place, top minus minus fine. OK
So what happens? You come out of the LS fine. You come out of the pop
The moment you come out of the function, what happens? This item variable will vanish
This life is finished. Now where you will go from, where you have received the call
you have received the call from which place you have received the call from. Again
the next line is what break, break will come out will make you out of switch
This file will not let you out again, you will take four options
Again, you press the two switch will take to the second case. Second case is pop
Now, I'll not come to this place again. OK? I've tried to make you explain every time
So I I'll do everything over there only. OK? So is top is able to minus one
No, again, item will be created again, this item will be created fine
OK? Its top is going to minus one. The top is not minus one because top is one
So I'll come to the part item will have ST top values one ST one is having eight
So it will come here. So printed line will be deleted. Element is eight because item is having eight
top minus minus this eight will get deleted and top minus minus will let you here
Again. You come out of this, you go there in main function
you again press the two, you again, come to the top function, you again created a variable
I um again, you check, top is equal to minus one. No, top is zero and zero is not equal to minus one condition false
You what you do item has ST zero, Y zero, top value zero
ST zero is having five. So item is having value five again, deleted element is what
Five. So it will be deleted. Element is five. Again, top minus minus
top current value zero and zero minus minus will make you make top is equal to what minus one
Again, you go to the main function, you come out of the switch, you again
go to the vile again, you press the two again, you come to the top again
you created this item variable. Again, you check is top is going to minus one
Yes, now top is minus one condition become true and in front of your stack is empty
Now you will not be able to pop anything. So what will get printed and the floor fine
So I've tried my level best to explain you this code. So uh I request you to kindly kindly kindly run the same code
You will be able to see the running of this code very easily
Fine. So at last what I'll say, I'll say thank you so much for watching Javadi Point
Please like subscribe and share the channel. Thank you. Thank you so much
Thank you very much. Yeah