Abstract: This collection of documents serves as an introductory resource for understanding Data Structures and Algorithms (DSA). It provides a comprehensive overview of essential concepts, common data structures, and algorithmic techniques.
Purpose: The purpose of these documents is to equip re...
,Push, Pop and Other Operations in Stack
Implemented Using an Array
CodeWithHarry
We have seen stack in our Data Structure course. Then we
talked about how we can implement stack with the help of
arrays. Today with that confidence we will move forward and
talk about stack operations. All code and everything I have
provided you in description so you should not worry about it.
If in an empty stack I want to fill something I will quickly shift
to a Red pen and from red pen I make operations from which
I will do stack push program. So let me draw a separator line
so that it will be easy for you too. Now whatever I will write
below this line will be with the help of this stack. If your SP is
full then what you will do either do printf or do return
negative value. By anyway, you return a message that stack is
overflow. You might have heard stackoverflow and now you
know what it means. Everybody will write in comment that I
know the meaning of stack overflow. And I am not writing
pseudocode here I am writing only concept.
I want that you should try to implement pop operation
yourself. Many of you will say I can not do it but I will say just
try for 5 minutes. And after that I am showing you how you
can implement pop. First of all you do n't need any value to
pop. Instead you are going to return a value keep this in mind
and in push you don't need to return any value. We created it
step-by-step. We created push function then we created Pop.
,We want to keep everything in term of pointers because we
want to to pass stack to functions. If I do stack * sp then I will
get all the information of stack which is pointed by sp. I 'll not
pass one-one value to it. We will use semicolon when we will
code. To perform pop what you have to do is to perform pop.
If the stack is full you can do this by returning a big integer I
just did it by printing stack overflow. Else move top a step
forward and put the value there. After that using the new
value of top I inserted the val on that index of array.
If you want data structures algorithm 's videos fast then you
can comment something like this , we want DS videos fast. If
many people do this then I 'll start uploading 2 videos a day
of DS. I hope you are getting it and let me know in comments
if you are liking it. And if you liking the videos please like the
video it will not take much time.
Coding Push(), Pop(), isEmpty() and isFull()
Operations in Stack Using an Array| C Code
For Stack
CodeWithHarry
In the last video of data structure series, we saw that how we
can push and pop if we are implementing a stack with the
help of array. Today I show you all this operation by coding
and if you feel that stack concept is not clear to you then if
you watch today 's video till end , then you will understand
the stack properly that is my guarantee. We had only a
variable sp which only store address. I have to request actual
, space in the memory where this size top and arr will come.
Now I have an actual instance of struct stack type , a
structure instance. Now we have to make sp. arr with the
help of dynamic allocation. I want element till the size of sp.
and I 'll write multiply by sizeof ( int ) because this array will
store only integers as it was storing here. If PTR 's PTR top is -
1 then your stack is empty. If the the the top of PTR will be on
the last index of stack array. Then in that case my stack is full.
So in this way I can implement isfull and isempty. I hope
everyone is clear now. And if you are facing problem with C
language then watch C language video.
As we have just created it , it is like an empty box. But 10
sweets can come in this box I am referring integer as sweet.
SP size is 10 so it can contain 10 sweets. So I can push 10
sweets in this one by one. But many people forget that if you
have read all 10 sweet then 11th one can not be inserted.
PTR array is an array of our structure pointer. And PTR top is
the topmost position which we have already updated here. I
will make it equal to val that it is Call by Value not reference
so it will update automatically and once it is updated so
element will be pushed in this. Let 's check whether push is
working or not. We will print isfull and isempty before and
after push. The thing is that. SP 's array top overall means I
want the topmost value in val. If you look at the stack which
is on your screen right now I want to pop 15 from the stack. If
it is isempty then stack underflow will print else we will
return an integer.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller abdullahkhan7. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.99. You're not tied to anything after your purchase.