I have a program where i store colored gumballs into a stack. We buy then store if we have space. Now I am asked to eat that color gumball if it is found but I can't figure out how to search within a stack and remove the top one (if is it at the top). How do i create a second stack to hold the gumballs moved in order to get to one that may in the middle??????
#include "Stack.h"
//#include <iostream>
usingnamespace std;
// Constructor to initialize the stack
Stack::Stack()
{
top = -1;
}
// Function to add item x to stack
void Stack::push(int x)
{
if(!isfull()){
top++;
gumballs[top] = x;
return; }
elsereturn;
}
// Function to remove and return top item of stack
int Stack::pop()
{
int x;
if(!isempty()) {
x = gumballs[top];
top--;
return x; }
elsereturn x;
}
// Function to check if stack is empty
bool Stack::isempty()
{
if (top == -1)
returntrue;
elsereturnfalse;
}
// Function to check if stack is full
bool Stack::isfull()
{
if (top == 6)
returntrue;
elsereturnfalse;
}
Im having a problem with searching within the stack, looking for the color then moving it to this stack ... is more of what i meant. sorry about that. Where is appropriate to put this second stack, in main or in the .h file?
How do you write something like that, popping all of the items until you remove the one you want, then push them back onto the stack in the appropriate order??