Solved

How do I display what is in a stack?

Posted on 2008-06-20
2
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 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