Solved

How do I display what is in a stack?

Posted on 2008-06-20
2
529 Views
Last Modified: 2008-06-21
Here is my code how do I display what is in the stack?


#include <iostream>

#include <stack>

 

using namespace std;

 

void Menu();

void Add();

void Display();

void Remove();

 

stack<int> stackInt;

int name;

 

int main()

{

	Menu();

}

void Menu()

{

	int choice;

 

	cout << "Stack Menu Options\n";

	cout << "1 - Add int to stack\n";

	cout << "2 - Remove from top of stack\n";

	cout << "3 - Size of stack\n";

	cout << "0 - Quit program\n";

	cout << "Enter choice: ";

	cin >> choice;

	cout << endl;

 

	switch(choice)

	{

	case 1:

		Add();

		break;

	case 2:

		Remove();

		break;

	case 3:

		Display();

		break;

	case 0:

		exit(0);

	default:

		cout << "Not a selection, enter again!\n";

		Menu();

		break;

	}

}

 

void Add()

{

	cout << "Enter a number to add: ";

	cin >> name;

	stackInt.push(name);

	cout << endl;

	Menu();

}

 

void Display()

{

 

}

 

void Remove()

{

	stackInt.pop();

	Menu();

}

Open in new window

0
Comment
Question by:jschmuff
2 Comments
 
LVL 10

Accepted Solution

by:
oleber earned 500 total points
ID: 21836524
there is a simple recursive way

void Display()
{
   if (! stackInt.empty()) {
      int top = stackInt.top();
      cout << top << endl;
      stackInt.pop();
      Display();
      stackInt.push(top);
   }
}

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21836747
>> there is a simple recursive way

I wouldn't do it recursively if I were you. Depending on how big the stack is, you might run out of (program) stack space. Which is bad.

It's not very proper either, as the code actually empties the stack. What if the code is multi-threaded ? A display should not modify the contents of the container.

The STL stack does not provide a way to show its contents. So, I'd rather implement my own stack if I really need display functionality.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unresolved External Symbols 3 68
I could not build boost code, 10 78
base64 decode encode 12 119
Installshield for Embarcadero EX 10.1 Berlin 4 18
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now