Page 1 of 1

STL: Stack A simple "how to" Rate Topic: -----

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 14 July 2008 - 06:43 AM

STL: Stacks
Mega-simple, but still good to know!

What is a stack?
A stack is basically like a pile of objects. Much like a queue, only a stack serves a "first come, last served" rule.
Basically, when we call pop() to remove a value from our stack, it will remove the last addition, as opposed to the first. In this demonstration, I will use a simple deck of cards to show you how a stack can be implemented.

Including the stuff that needs to be included
Very simple, just #include <stack> and we'll be using std::stack; But, as I want to show you how this works, I'm gonna #include <iostream> too :)
#include <iostream>
#include <stack>

using namespace std;

int main ()
{

Creating a stack
Very simple, similar syntax to all other STL stuff:
stack <string> cards; /* Simple enough to create a stack */

Adding values to the stack
Again, this is really simple. All we need to do is use the push() function to add values to our stack:
    cards.push("King of Hearts"); /* push() will add a value, think of queues */
    cards.push("King of Clubs");  /* adding some cards to the deck */
    cards.push("King of Diamonds");
    cards.push("King of Spades");

Accessing the size of the stack
This can be done with a quick and simple call to the size() function, like so:
    cout << "There are " << cards.size () << " cards in the deck" << endl;

Access to the values stored in a stack
Think of a deck of cards. You are only allowed to take the top card, right? The same applies to a stack, we can only access the top() value stored in a stack:
cout << "The card on the top of the deck is " << cards.top() << endl;
This should output "The card on the top of the deck is King of Spades" since this was the last card added to the deck.

Removing a value from the stack
The only value that we can remove from the stack is the top one. (This is the only value that we actually have access to) To remove a value from a stack, we need to make a call to the pop() function, like so:
cards.pop();

Finally, I'm going to output the top card and the size again, so that we can now see that "King of Spades" has been removed from the stack:
    cout << "The top card is now " << cards.top() << endl;
    cout << cards.size();

And don't forget to close off main! }

And there we have it. That's all there is to using a stack!
Also, you can refer to

Here is the entire code which I used in this tutorial:
#include <iostream>
#include <stack>

using namespace std;

int main ()
{
    stack <string> cards; /* Simple enough to create a stack */
    cards.push("King of Hearts"); /* push() will add a value, think of queues */
    cards.push("King of Clubs");  /* adding some cards to the deck */
    cards.push("King of Diamonds");
    cards.push("King of Spades");
    cout << "There are " << cards.size () << " cards in the deck" << endl;
    cout << "The card on the top of the deck is " << cards.top() << endl;
    /* Will output King of Spades, since this was the last one to be added */
    cards.pop();
    cout << "The top card is now " << cards.top() << endl;
    cout << cards.size();

    cin.get ();
    return EXIT_SUCCESS;
}


Is This A Good Question/Topic? 4
  • +

Replies To: STL: Stack

#2 sopritenada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-July 10

Posted 01 July 2010 - 09:45 AM

THANKs by the way i understand that i'll try to write the code of the calculator thanks again
Was This Post Helpful? 0
  • +
  • -

#3 owkowk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-September 10

Posted 25 September 2010 - 01:38 AM

this is an array based stack right? how bout linked list based stack? i totally not understand this... :no: can you show some steps to do linked list based stack? thanks! ^_^
Was This Post Helpful? 0
  • +
  • -

#4 animecrazy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-May 10

Posted 02 November 2010 - 05:22 AM

View Postowkowk, on 25 September 2010 - 12:38 AM, said:

this is an array based stack right? how bout linked list based stack? i totally not understand this... :no: can you show some steps to do linked list based stack? thanks! ^_^


I dont have the code with me but i can give you a good idea of what to do...all you need to do is insert nodes(create them and insert them either at the head or the tail in a specific manner), if you are adding at the head, then stick with it, and when it come to POP(), just delete the node at the head and move the head forward.... that is the basic idea behind it....hope it helped
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Amit Birla*


Reputation:

Posted 18 January 2011 - 05:57 AM

In this code thing is missing which is giving compilation error.

"Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion) d:\new folder\shehzad sir's\stack(stl)\stack(stl)\stack(stl).cpp 67
"

So, include <string> header file.
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Posted 18 January 2011 - 06:23 AM

If you're having trouble with a program, please post a topic in the C and C++ Forum, rather than here. Post all your code IN CODE TAGS as seen below, or reference the first link in my signature.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1