{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Undo sequence in a text editor chain of method calls

Info icon This preview shows pages 6–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Undo sequence in a text editor Chain of method calls in the C++ run-time  system Indirect applications Auxiliary data structure for algorithms Component of other data structures ©  2010 Goodrich, Tamassia
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks 7 C++ Run-Time Stack The C++ run-time system  keeps track of the chain of  active functions with a stack When a function is called, the  system pushes on the stack a  frame containing Local variables and return value Program counter, keeping track of  the statement being executed  When the function ends, its  frame is popped from the stack  and control is passed to the  function on top of the stack Allows for  recursion main () { int i = 5; foo(i); } foo (int j) { int k; k = j+1; bar(k); } bar (int m) { } bar   PC = 1   m = 6 foo   PC = 3   j = 5   k = 6 main   PC = 2   i = 5 ©  2010 Goodrich, Tamassia
Image of page 7
Stacks 8 Array-based Stack A simple way of  implementing the  Stack ADT uses an  array We add elements  from left to right A variable keeps  track of the  index of  the top element  S 0 1 2 t Algorithm size () return t + 1 Algorithm pop () if empty () then throw StackEmpty else t t - 1 return S [ t + 1] ©  2010 Goodrich, Tamassia
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks 9 Array-based Stack (cont.) The array storing the  stack elements may  become full A push operation will  then throw a  StackFull   exception   Limitation of the array- based  implementation Not intrinsic to the  Stack ADT S 0 1 2 t Algorithm push ( o ) if t = S.size () - 1 then throw StackFull else t t + 1 S [ t ] o ©  2010 Goodrich, Tamassia
Image of page 9
Stacks 10 Performance and Limitations Performance Let  n  be the number of elements in the stack The space used is  O ( n ) Each operation runs in time  O (1) Limitations The maximum size of the stack must be defined a  priori and cannot be changed Trying to push a new element into a full stack  causes an implementation-specific exception ©  2010 Goodrich, Tamassia
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks 11 Array-based Stack in C++ template <typename E> class ArrayStack { private: E* S; // array holding the stack int cap; // capacity int t; // index of top element public: // constructor given capacity ArrayStack( int c ) : S (new E[c]), cap (c), t (-1) { } void pop() { if ( empty() ) throw StackEmpty (“ Pop from empty stack ”); t--; } void push( const E& e ) { if ( size() == cap ) throw StackFull (“Push to full stack”); S[++ t] = e; } (other methods of Stack interface) ©  2010 Goodrich, Tamassia
Image of page 11
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern