Stack

Posted: September 1, 2011 in Uncategorized

  1. One way to think about this implementation is to think of functions as being stacked on top of each other; the last one added to the stack is the first one taken off. In this way, the data structure itself enforces the proper order of calls. Conceptually, a stack is simple: a data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack; the only element that can be removed is the element that was at the top of the stack.

    Consequently, a stack is said to have “first in last out” behavior (or “last in, first out”). The first item added to a stack will be the last item removed from a stack. So what’s the big deal? Where do stacks come into play? As you’ve already seen, stacks are a useful way to organize our thoughts about how functions are called. In fact, the “call stack” is the term used for the list of functions either executing or watiing for other functions to return. In a sense, stacks are part of the fundamental language of computer science. When you want to express an idea of the “first in last out” variety, it just makes sense to talk about it using the common terminology. Moreover, such operations show up an awful lot, from theoretical computer science tools such as a push-down automaton to AI, including implementations of depth-first search.

    Stacks have some useful terminology associated with them:

    1. Push To add an element to the stack
    2. Pop To remove an element from the stock
    3. Peek To look at elements in the stack without removing them
    4. LIFO Refers to the last in, first out behavior of the stack
    5. FILO Equivalent to LIFO

    General Algorithm

    1. [Prologue] Save the parameters, Local Variables and return address.
    2. [Body] If the base criterion has been reached, then perform the final computation and go to step 3; otherwise, perfprm the partial computation and go to step 1(initialize a recursive call)
    3. [Epilogue] Restore the most recently saved parameters, local variables, and return address. Go to this return address.
    4. simple program is given below
    5. #include <iostream>
      #include <stack>
      using namespace std;

      int main()
      {
        stack<char> stackObject;

        stackObject.push(‘A’);
        stackObject.push(‘B’);
        stackObject.push(‘C’);
        stackObject.push(‘D’);

        while(!stackObject.empty()) {
          cout << “Popping: “;
          cout << stackObject.top() << endl;
          stackObject.pop();
        }

        return 0;
      }

Advertisements
Comments
  1. Mr WordPress says:

    Hi, this is a comment.
    To delete a comment, just log in, and view the posts’ comments, there you will have the option to edit or delete them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s