Data structures: Array implementation of stacks

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • See complete series on data structures here:
    • Data structures
    In this lesson, we have discussed array based implementation of stack data structure.
    Source Code:
    C code: gist.github.co...
    C++ code (Object oriented implementation) : gist.github.co...
    Time complexity of push for dynamic array implementation:
    If we start with an array of size 1 and keep doubling the size with each overflow, for n pushes.. cost of copy will be
    (1 + 2 + 4 + 8 + ... + n/2 + n)
    = n *( 1+ 1/2 + 1/4 + 1/8 + ... 1/n) - taking out n
    = n*2 - the expression in bracket above will evaluate to 2.
    So, cost of copy in n pushes = O(n)
    Cost of n normal pushes = O(n) - each push takes constant time
    Total cost of n pushes = O(n)
    Average cost of 1 push = O(1).
    For practice problems and more, visit: www.mycodeschoo...
    Like us on Facebook: / mycodeschool
    Follow us on twitter: / mycodeschool

Комментарии • 297

  • @iftekharjitu4161
    @iftekharjitu4161 3 года назад +133

    The tutorial is from 2013, and I am from 2021, seems like it will never get old. God bless you

  • @emmojo3428
    @emmojo3428 10 лет назад +447

    As a hearing impaired beginning programmer, I want to thank you sincerely for the captions. You have just earned a subscription from me, which is unique given that I don't have any other subscriptions. Keep up the great work.

    • @mycodeschool
      @mycodeschool  10 лет назад +76

      Jamaika I am very glad that you are getting helped. :)

    • @gamereplayhq
      @gamereplayhq 8 лет назад +29

      +Jamaika (y) THIS COMMENT MADE MY DAY :D

    • @SonuSonu-tk5pk
      @SonuSonu-tk5pk 7 лет назад +1

      why ? doesn't c taught in ur country?

    • @priyavratkumar4296
      @priyavratkumar4296 7 лет назад +11

      Sonu Sonu sabse yahin puchega?

    • @ashishagrawal1406
      @ashishagrawal1406 6 лет назад +21

      1. Check your English Before Commenting.
      2. One can watch RUclips videos irrespective of the fact that the thing is being taught in their country.
      3. Stop spamming RUclips with foolish comments.

  • @Danieljohnson-sh7iq
    @Danieljohnson-sh7iq 6 лет назад +52

    OMG whoever did and organize this tutorial God will bless him. I have went through 13 executive tutorials on stack i still didn't get it, but once i watch this video, it's as if the tutor transfered the knowledge to my brain straight LOL. Once again thank you, am subscribing.

  • @MatheusLB2009
    @MatheusLB2009 7 лет назад +37

    Your video makes the internet a better place

    • @israa6280
      @israa6280 5 лет назад +1

      Matheus Lacerda so agreed

  • @Calm_Energy
    @Calm_Energy 6 лет назад +1

    I really appreciate how you give me the words to describe the connection between the pseudo-code and Big O notation. For ex. "two scenarios in a push operation," then you show the details between constant time for a normal push operation then O(n) for an overflow correction using a larger array. Next you say "the time complexity of push with this strategy" O(1) in best case, O(n) in worst. Plus the pseudo-code really helps!

  • @sarojhkalyan1527
    @sarojhkalyan1527 2 года назад +1

    Thank you for making this video. Explanations from others didn't get me to understand this concept as thoroughly as yours did.

  • @MubasharAli-op5vq
    @MubasharAli-op5vq 11 месяцев назад +1

    The tutorial is from 2013, and I am from 2023, seems like it will never get old. God bless you

  • @RenuSharma-zb2bz
    @RenuSharma-zb2bz 3 года назад +1

    One good thing is that while explaining the data structures not any particular language is used so it is easy for any language study to watch this video

  • @PokeDubzTCG
    @PokeDubzTCG 8 лет назад +4

    Your videos are great! Thanks for the clear explanations, as my current CSC-250 professor doesn't give us much information on these forms of data structures.

  • @PurnenduMondal_
    @PurnenduMondal_ 6 лет назад +2

    Sir You bring my Confidence Back.....Thank you a Lot...for all of your Videos

  • @virenbhanushali9282
    @virenbhanushali9282 7 лет назад

    The clearity of your words just blew my mind.keep you work on

  • @fay_chong
    @fay_chong 8 лет назад +1

    Hi, I consider myself rather green when it comes to programming and I have legit no idea what my professor has been teaching until I found this video. Thank you so much for your clear explanation. :) This video is super helpful!

  • @daveboyle76
    @daveboyle76 3 года назад

    You're explanation is clear and concise. Far superior to the text provided with our course. Thank you.

    • @MicroNG
      @MicroNG 3 года назад

      please check the details

  • @vladalbescu4153
    @vladalbescu4153 9 лет назад +188

    Array Stack of Winterfell.

  • @amitech1992
    @amitech1992 11 лет назад +1

    Great tutorial and nice accent, easily understandable. U reduced my work to Google a lot and then go through too many pages to understand these concepts for my College viva.

  • @smitasharma2979
    @smitasharma2979 8 лет назад

    I have gone through all your data-structures videos and I really like the way you explain. I appreciate the content as well as the clarity of the voice and flow.

  • @kevinrocky4443
    @kevinrocky4443 4 года назад +80

    Anyone else feel like you learned more from this than in college?

  • @aricktanwar7436
    @aricktanwar7436 5 лет назад

    One of the talented person who knows data structures as mother tongue
    he will be the only reason i pass my examination

  • @samarthshetty1810
    @samarthshetty1810 6 лет назад +1

    I really don't understand why few disliked, this made me understand concepts of stack well.

  • @CSEconceptswithParinita
    @CSEconceptswithParinita 4 года назад +2

    Thank you so much sir! You're an inspiration for new tutors like me🙏

  • @yilingl.8756
    @yilingl.8756 10 лет назад +6

    This helps so so so much!! Thank you for the clear explanation.

  • @usmanabdurrahman2461
    @usmanabdurrahman2461 8 лет назад +18

    Tommorow's my final.Bless you 😂😂

  • @PC-ky3qo
    @PC-ky3qo 6 лет назад

    This is the best explanation i could ever get on array implementation of stack. thank you very much

  • @rownitakhanam5013
    @rownitakhanam5013 5 лет назад +1

    This video is very helpful.I was going hardtime for perceiving the concepts.And I got poor marks.Now I have started to understand by Mercy of Allah.Thanks a lot for this video.

    • @075_ritikkumar7
      @075_ritikkumar7 4 года назад +2

      Har cheej me allah ko ghusa do. Thank to this great tutor , not allah.

    • @rownitakhanam5013
      @rownitakhanam5013 4 года назад +1

      @@075_ritikkumar7 It is always Allah (The One and Only God) who always helps me for everything. He is the Rahman( The beneficent)has created us from mingled sperm. I also thanked this tutor who has helped me. But if I loose my brain power ,then this tutor can help me no more despite his talent. Because I was depressed for many years. Even I was going to attempt suicide .At that time, it was hard for me to study and understand hard concepts. Fortunately, Allah guided me through HIs Scripture 'The Quran' and helped me to overcome my depression. Finally, I have found a new meaning of my life.

    • @rownitakhanam5013
      @rownitakhanam5013 4 года назад +1

      @@075_ritikkumar7 Allah is the most wise.He created everything with exact meaaure through His knowledge He has knowledge about both seen and unseen.

    • @rownitakhanam5013
      @rownitakhanam5013 4 года назад

      @@075_ritikkumar7 Try to respect every religion. And before commenting also read scripture of respective religion

  • @zannylover2898
    @zannylover2898 5 лет назад

    No word for this kind of knowledge,simply fr m the best in the world ,where are u sir plz comeback we want more videos from you,plzzz sir help u truly the best in the world.

  • @tusharjajodia9077
    @tusharjajodia9077 3 года назад

    Still the Best Tutorial in the platform

  • @melissahong1
    @melissahong1 7 лет назад +45

    4:10 - "We do not care what garbage lies there" Hahaha

    • @user-mc1eb1sg5x
      @user-mc1eb1sg5x 6 лет назад +5

      mean bully
      I think you are the one with the garbage face and you're trying to feel good by posting sh*t in the internet.
      Get a life man.

    • @FatmaYousuf
      @FatmaYousuf 6 лет назад

      Grove Street calm your balls

    • @shubhangisoni1668
      @shubhangisoni1668 6 лет назад +9

      By garbage ,he meant garbage value , that is a typical word in programming vocabulary ,used to describe unwanted value /data .

    • @ujjwalb7406
      @ujjwalb7406 5 лет назад +1

      Garbage value is a thing!!

    • @dancinglikecrazy5510
      @dancinglikecrazy5510 4 года назад

      @@user-mc1eb1sg5x wtf💀💀💀🤣🤣🤣 you okay my guy?

  • @bihator
    @bihator 8 лет назад +5

    You are absolutely amazing man !!

  • @mycodeschool
    @mycodeschool  11 лет назад +19

    We wish that too :)

    • @VinayKumar-vm1hg
      @VinayKumar-vm1hg 4 года назад +2

      Sir how top=max_size-1

    • @jeetsharma9892
      @jeetsharma9892 4 года назад

      @@VinayKumar-vm1hg If you understood please explain it to me as well.

    • @AmanSharma-ht5zq
      @AmanSharma-ht5zq 4 года назад +2

      @@VinayKumar-vm1hg In most programming languages, the arrays are implemented as 0 based index. Let's say there are 2 elements in an array, so its max_size = 2, and there are only two indices - 0, 1. So the variable top should be equal to 1, hence top = max_size-1;

  • @royal9063
    @royal9063 3 года назад

    My wish is to free and quality education for all. I don't know how many of you are doing this work. But I wish to God to give a healthy and prosperous life to all of you. Bcoz you r doing a great work. Plz make more videos. You know about India's privet college. they are just useless. You have a great quality to teach everything in simple way. Plz help me help us to become a Good engineer from privets college. We want more videos. Plz make more videos.

    • @MicroNG
      @MicroNG 3 года назад

      please check the details

  • @nehahanwate7322
    @nehahanwate7322 5 лет назад +3

    thanks for this good explanation and clearing the concept

  • @chetanyat4632
    @chetanyat4632 9 лет назад +3

    u r d best in dis business :) thankyou soo much for such beautiful and easy to understand explanations :D
    #appreciate#your#work :)

  • @semirumutkurt6635
    @semirumutkurt6635 7 лет назад

    give this man a medal

  • @RajendraSingh-kv7df
    @RajendraSingh-kv7df 6 лет назад

    Sir this is very very clearly and amazingly explained video
    Thank u very much

  • @chenghuapang7504
    @chenghuapang7504 7 лет назад

    so easy! you did a better job than my professor...

  • @douglasdavisv
    @douglasdavisv 8 лет назад +1

    i can feel the dedication from your each word.thank you for the information.i killed the subscribe button .#feeling informative with my code school

  • @babruwankshirsagar6390
    @babruwankshirsagar6390 9 лет назад

    keep doing it. it is really work as helping hand for many of us..thank you.

  • @abdelmajidkansoussi6904
    @abdelmajidkansoussi6904 2 года назад

    thank you man , this was very helpful. God bless you.

  • @himanshusattavan1335
    @himanshusattavan1335 7 лет назад

    My code school,
    just simply rocks........

  • @dodgywarden8788
    @dodgywarden8788 5 лет назад

    Thankyou so much. Now i can Write Stack code on my own

  • @sreekeshv12
    @sreekeshv12 7 лет назад

    thanks bro I understood the topic very well you earned yourself a subscriber

  • @sarweswaranss5526
    @sarweswaranss5526 4 года назад

    Perfect teaching..Finally i learnt tq u so much..

    • @MicroNG
      @MicroNG 3 года назад

      please check the details

  • @welkin102
    @welkin102 11 лет назад +3

    i really wish that you guys had started this video-makings a year before !!

  • @ys2313
    @ys2313 5 лет назад +1

    Thanks Sir. it helped me a lot.

  • @sowjanyayellumahanti8460
    @sowjanyayellumahanti8460 8 лет назад

    this explanation helped me a lot sir. thank you very much

  • @tuankietdang7285
    @tuankietdang7285 7 лет назад

    Just what I need, thank you for your informative video

  • @syedfilms4447
    @syedfilms4447 4 года назад

    amazing woaww i learn first time from your this tutorial and understand stack with array too much easily thansk sir

    • @MicroNG
      @MicroNG 3 года назад +1

      please check the details

  • @DMSAProductions
    @DMSAProductions 10 лет назад

    Thank you very much! I have been trying all over again to creat something similar without any success!!! THANK YOU!!!! You helped me a lot!

  • @Sun-yv9fr
    @Sun-yv9fr 5 лет назад

    I've been watching EVERY video about to understand the pop( ) concept, and NOW I'm finally understanding what happens to the number after the pop( ).

  • @noaido9803
    @noaido9803 5 лет назад

    All the lessons are great!
    Thank you!

    • @MicroNG
      @MicroNG 3 года назад

      please check the details

  • @vyshuvysh2502
    @vyshuvysh2502 6 лет назад

    explanation is done very well... thanks

  • @TheGreenRedYellow
    @TheGreenRedYellow 8 лет назад +7

    Your top function has a bug. It will crash if we have an empty stack. At 11:25.

    • @hamadaesmael2176
      @hamadaesmael2176 5 лет назад

      add if condition to check if it is empty

    • @jerushanmoodley2641
      @jerushanmoodley2641 4 года назад

      #include
      #include
      #define max_size 101
      int a[max_size];
      int top=-1;
      void isempty()
      {
      if(top == -1)
      {
      printf("error: stack is empty ");
      return;
      }
      }
      void push(int x)
      {
      if(top == max_size -1) // handles stack overflow
      {
      printf("error: stack overflow
      ");
      return;
      }
      top++;
      a[top] = x;
      }
      void pop()
      {
      if(top == -1)
      {
      printf("error: no element to pop
      ");
      return;
      }
      top--;
      }
      int Top()
      {
      isempty();
      return a[top];
      }
      void print()
      {
      isempty();
      int i;
      printf("stack: ");
      for(i=0;i

  • @mohitbv2331
    @mohitbv2331 8 лет назад +1

    Thank you. God bless you. Keep it up.

  • @skywolf6984
    @skywolf6984 10 лет назад +2

    Your videos amazing man , thanks for sharing them they help a lot. Keep up good work =)

  • @rudreshjha7166
    @rudreshjha7166 10 месяцев назад

    Nice explanation man

  • @007MrAjay
    @007MrAjay 7 лет назад +2

    For n pushes, cost of copy should be 1 + 2 + 4+....+ (n/4) + (n/2), because when the stack is overflowed on trying to push the ((n/2) + 1)th element, it creates another dynamic array of size n, which can contain all n pushes. So, why did we include 'n' in the cost?

    • @yashgirdhar100
      @yashgirdhar100 6 лет назад

      so for n+1 elements, you''ll have to include n

  • @everfresh9693
    @everfresh9693 8 лет назад +1

    Nice video dude!! It helped alot, keep up the great work

  • @adityabokkisum1000
    @adityabokkisum1000 5 лет назад

    really u saved my day man a big thank for u

  • @carefree_ladka
    @carefree_ladka 5 лет назад

    Really loved your lectures bro 😊 .. helpful

  • @ramakrishnan4356
    @ramakrishnan4356 6 лет назад

    u saved me from my exam thx a lot bro

  • @user-eh5ze4nw4z
    @user-eh5ze4nw4z 10 лет назад +2

    very good, thanks

  • @mounica.b5344
    @mounica.b5344 11 лет назад +1

    awesome tutorial ,pls keep more videos on trees and graphs in c++(oop) language

  • @johncena-fx2ur
    @johncena-fx2ur 4 года назад

    The top function will return the element at the top of the stack. So in push function top==MAX-1 might be true even if stack is not full

    • @MicroNG
      @MicroNG 3 года назад

      please check the details

  • @josiahsamson6022
    @josiahsamson6022 8 лет назад +4

    Very understandable,,,,,,,,,,,

  • @nandangupta727
    @nandangupta727 9 лет назад

    awesome tutorial of DS .. thnku so much.. :)

  • @maaz6912
    @maaz6912 5 лет назад

    In the top function, where the line " return A[top]; " is there, the compiler throws out an error that "array subscript is not an integer". And yes, my function starts with int, not void.

  • @srinaath9845
    @srinaath9845 7 лет назад

    thank u so much..i am very grateful to you

  • @NaveenKumarErroju
    @NaveenKumarErroju 7 лет назад

    fabulous explanation...

  • @morrisombiro
    @morrisombiro 6 лет назад

    IsEmpty()
    {
    return top==-1;
    } // is much cleaner...Awesome videos nonetheless.

  • @jagadeeshr91
    @jagadeeshr91 5 лет назад

    Good explanation

  • @thapelomokgwaphe3005
    @thapelomokgwaphe3005 10 лет назад

    thank you very much,wish u wer my lecturer

  • @usama57926
    @usama57926 6 лет назад +1

    thank u for mathematical explanation

  • @KistlakRajapakshaSevenNet
    @KistlakRajapakshaSevenNet 8 лет назад +3

    Thank U Very Much Bro !! :D

  • @HabiburRahman-xs9gb
    @HabiburRahman-xs9gb 7 лет назад

    simple and clear...just awssm

  • @rztutul8824
    @rztutul8824 7 лет назад

    Thanks ...excellent video ..

  • @jayp1638
    @jayp1638 10 лет назад +2

    Amazing video Buddy :)

  • @cagrkaymak3063
    @cagrkaymak3063 9 лет назад

    really nice job, u helped me a lot

  • @TheJust4give
    @TheJust4give 9 лет назад

    I am becoming a big time fan of your lessons. Do you have any lesson on AVL ? I was not able to find one from your list.

  • @XieQiu
    @XieQiu 8 лет назад

    very clear. thank you!

  • @AspirantNoteBooks
    @AspirantNoteBooks 8 лет назад +13

    Earned a subscription from me TOO :)

  • @AbhishekYadav-hs4ix
    @AbhishekYadav-hs4ix 2 года назад

    Thank You Sir!!

  • @elie3423
    @elie3423 6 лет назад

    in case of IsEmpty() method, just return top == -1 without if and else statements

  • @patrikhyll4301
    @patrikhyll4301 8 лет назад

    thank you a lot!!! a great series!

  • @shivangisrivastava1158
    @shivangisrivastava1158 4 года назад

    love your voice man 💯

  • @chilinouillesdepommesdeter819
    @chilinouillesdepommesdeter819 6 лет назад

    Great job!

  • @tejamvangala3296
    @tejamvangala3296 8 лет назад

    all are very good videos.i think better than my teacher (b tech 2nd year cse MIT manipal).what do u do for liviving man

    • @AkshayKumar-dz5ts
      @AkshayKumar-dz5ts 7 лет назад +4

      lmao
      is that seriously a question man?
      he obviously is a programmer who also likes to teach
      thats what he does for a living

  • @kal5211
    @kal5211 3 года назад

    Excellent.

  • @abhiGunasekar
    @abhiGunasekar 4 года назад +2

    Hi MyCodeSchool Instructor. I am having some trouble in my code. I don't know why but I'm getting a stack overflow error when I try compiling this code. Would you be able to compile it on your end and let me know what might be the issue. My source code is exactly the same as yours.
    // Stack -- Array based implementation
    #include
    #define MAX_SIZE 5
    int A[MAX_SIZE];
    int top = -1;
    void Push(int x) {
    if (top = MAX_SIZE - 1) {
    printf("Error: stack overflow
    ");
    return;
    }
    A[++top] = x;
    }
    void Pop() {
    if (top == -1) {
    printf("Error: No element to pop
    ");
    return;
    }
    top--;
    }
    int Top() {
    return A[top];
    }
    void Print() {
    int i;
    printf("Stack: ");
    for (i = 0; i

    • @abhiii1375
      @abhiii1375 4 года назад

      I think you might have figured it out by now but in your push function inside the if condition, you have used assignment operator(=) instead of relational operator(==).

  • @irving99uk
    @irving99uk 8 лет назад

    Really good thank you.

  • @Tushgup
    @Tushgup 10 лет назад +4

    Isn't stack a LIFO(Last in first out)? So shouldn't the order of the print function be 12 5 2?

  • @mustafaali9128
    @mustafaali9128 7 лет назад

    I love your clases!

  • @str89z
    @str89z 4 года назад +1

    2:10 for empty array, index is -1...bc of imaginary array

  • @sanyog312
    @sanyog312 10 лет назад

    thanks it was very helpfull

  • @prasadbandaru6498
    @prasadbandaru6498 5 лет назад

    Good one!

  • @HuthaifaMady1992
    @HuthaifaMady1992 9 лет назад

    Wonderful ... Thanks,,

  • @estebanlopez1701
    @estebanlopez1701 5 лет назад

    damn this video is impressive, thanks a lot man

  • @wayoflife3098
    @wayoflife3098 5 лет назад

    Thank you sir 😊

  • @rapramos5687
    @rapramos5687 7 лет назад +1

    I think you forgot to include in the description the other array based implementation of stack in C. you only included the github link to the source code you wrote in the video.

  • @ivanreii
    @ivanreii 9 лет назад

    just a question, how exactly do you create a new array when an array is full? we can't create a new node like what we did in linked list since these arrays are located in stack part of the memory, where linked list is in the heap, correct?
    I mean, we can't write
    if(top == max_size)-1{
    int B[max_size*2]; or new B[max_size*2];
    }
    even if it's possible, when B is maxed out, there will be no int C[max_size*3].
    any enlightment?

  • @25sanjeevlamba
    @25sanjeevlamba 6 лет назад

    sir, may i know for such a excellent presentation which software do u use?