Tuesday, July 19, 2011

Stacks and its Implementation using C++

Stuck:
Stack is a last in, first out (LIFO) abstract data type and data structure. It is a container that implements the last in first out (LIFO) Protocol. That means that the only accessible object in the container is the last one among them that was inserted. A stack of book is a good analogy: you can’t read any in the stack without first removing the books that are stacked on top of it.
It has three fundamental operations: push, pop and stack top. When we using stack, we need a header file called stack.


Example: Stuck Implementation on C++

#include<stack>
using namespace std;
int main(){
stack<string> s;

s.push("Linux");

s.push("JAVA");

s.push("Oracle");

s.push("PHP");

cout<< "s.size() = " << s.size() << "\n";

while(!s.empty()){
cout << "s.top() = " <<s.top() <<"\n";    s.pop();

}

cout<< "s.size() = " << s.size() << "\n";

getchar();

return 0;

}

Now guess the out put.
If you are interested to learn Data Structure, This wonderful book for you:

1 comments:

Post a Comment

Give your valuable Comment...

 

Programming, Data Structures & Algorithms Copyright © 2011 -- Template created by JAK -- Powered by Blogger