Searching in a Stack

Oct 12, 2011 at 12:00am
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??????

my main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include "Stack.h"

using namespace std;

class Gumball {
    public:
        string color;
        int counter;
};

typedef Gumball Type;

int main()
{
    Stack s;
    Gumball g;
    char choice;
    bool choice_flag = true;

    do {
        cin >> choice;
        cin >> g.color;
        switch(choice)
        {
            case 'b':
            case 'B':
                cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
                s.isempty();
                s.push(1);
                if(!s.isfull())
                    cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
                else
                    cout << "There is no room for another gumball." << endl << endl;
                break;
            case 'e':
            case 'E':
                s.isempty();
                s.pop();
                cout << "A" << " " << g.color << " gumball has been eaten." << endl << endl;
                break;
            case 'q':
            case 'Q':
                choice_flag = false;
                break;
        }
    } while(choice_flag);

    return 0;
}


.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STACK_H
#define STACK_H


// Interface file  - Gumball class definition
class Stack {
    public:
        Stack();
        void push(int);
        int pop();
        bool isempty();
        bool isfull();
    private:
        int gumballs[6+1];
        int top;
};


#endif // STACK_H 


.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "Stack.h"
//#include <iostream>

using namespace 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; }
    else
        return;
}

// Function to remove and return top item of stack
int Stack::pop()
{
    int x;

    if(!isempty()) {
        x = gumballs[top];
        top--;
            return x; }
    else
        return x;
}

// Function to check if stack is empty
bool Stack::isempty()
{
     if (top == -1)
          return true;
     else
          return false;
}

// Function to check if stack is full
bool Stack::isfull()
{
    if (top == 6)
          return true;
    else
          return false;
}
Last edited on Oct 12, 2011 at 12:01am
Oct 12, 2011 at 12:02am
How do i create a second stack to hold the gumballs moved in order to get to one that may in the middle??????


Stack secondStack;
?
Oct 12, 2011 at 12:08am
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?
Last edited on Oct 12, 2011 at 12:13am
Oct 12, 2011 at 12:21am
Where is appropriate to put this second stack, in main or in the .h file?

Wherever you need it, so in main() or another function.

looking for the color then moving it to this stack ... is more of what i meant.

You remove elements with pop() and put them back with push().
Oct 12, 2011 at 2:32am
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??
Topic archived. No new replies allowed.