Can anyone direct me where to go for doing stacks in C++. I have to make a program using stacks but can you initialize a stack with given values? If so can you give an example?

Recommended Answers

All 2 Replies

en……maybe STL template lib may fulfill you need ;)

but i always write a simple data-struct for the program...

This would be an example of a stack of integers using a list ...

// example of a stack (Last In First Out = LIFO) using a list
// compares to a stack of dinner plates
// Dev-C++

#include 
#include 

using namespace std;

int main()
{
  list iL;
  int k;

  // load the stack with numbers 0 to 9, last number entered would be on top
  for(k = 0; k < 10; k++)
    iL.push_front(k);

  // now unload the stack and display
  cout << "Unloading the stack in order:\n";
  for(k = 0; k < 10; k++)
  {
    // read the top element of the list
    cout << iL.front() << endl;
    // now pop it (removes top element)
    iL.pop_front();
  }

  cin.get();  // wait
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.