Data structures: Array implementation of stacks

Veröffentlicht am: 07 Oktober 2013
auf dem Kanal: mycodeschool
855,161
7.2k

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: https://gist.github.com/mycodeschool/...

C++ code (Object oriented implementation) : https://gist.github.com/mycodeschool/...

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: http://www.mycodeschool.com

Like us on Facebook:   / mycodeschool  

Follow us on twitter:   / mycodeschool  


Auf dieser Seite können Sie das Online-Video Data structures: Array implementation of stacks mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer mycodeschool 07 Oktober 2013 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 855,161 Mal angesehen und es wurde von 7.2 tausend den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!