在堆栈中查找一个项目

时间:2011-10-17 08:11:49

标签: c++ loops stack

在我的代码中,我试图找到放入堆栈的特定项目。为此,我将所有项目移动到临时堆栈,将其从原始堆栈中弹出。弹出后,我将按原始顺序将所有项目移回原始堆栈中。我的代码永远不会识别该项目在堆栈中,所以当它实际上在堆栈中时我找不到它。你能帮我调试一下我的循环......

int main:

#include <iostream>
#include "Stack.h"
#include "Gumball.h"

using namespace std;

int main()
{
  Stack s, gumballStack;
  Gumball g, temp;
  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;
            g.counter = 0;
            s.isempty();
            s.push(g);
            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();
            temp = s.pop();
            if(s.isempty() && temp.color == g.color)
            {
                cout << "The " << g.color << " gumball has been eaten." << endl << endl;
            }

从现在开始,我相信是错误:

            while(!s.isempty() && g.color != temp.color)
            {
                gumballStack.push(temp);
                g.counter++;
                s.pop();
                cout << " " << temp.counter << endl << endl;
            }
            if(!s.isempty())
            {
                cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl;
            }
            else
            {
                cout << "The gumball cannot be found." << endl << endl;
            }
            while(!gumballStack.isempty())
            {
                //gumballStack.pop();
                s.push(gumballStack.pop());
                gumballStack.pop();
            }
            break;
        case 'q':
        case 'Q':
            choice_flag = false;
            break;
    }
} while(choice_flag);

return 0;
}

.h文件:

#ifndef STACK_H
#define STACK_H
#include "Gumball.h"

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


#endif // STACK_H

回答您的问题@TOM:

好.cpp(对于stack.h)是,我认为它会回答你提出的大部分问题:

#include "Stack.h"
#include "Gumball.h"

using namespace std;

// Constructor to initialize the stack
Stack::Stack()
{
   top = -1;
}

// Function to add item x to stack
void Stack::push(Gumball x)
{
   if(!isfull()){
    top++;
    gumballs[top] = x;
    return; }
   else
    return;
}

// Function to remove and return top item of stack
Gumball Stack::pop()
{
    Gumball 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;
}

我看到你说过的问题,我正在多次将温度重新放回堆栈......绝对不是我的意图,谢谢你指出了这一点。如何让它在堆栈中添加不等于我正在寻找的项目的每个gumball,而不是相同的?

我认为添加Gumball.h和.cpp会回答你的其他问题,所以这里是:

gumball.h文件:

#ifndef GUMBALL_H
#define GUMBALL_H
#include <iostream>

using namespace std;

// Interface file  - Gumball class definition
class Gumball
{
    public:
        Gumball();
        string color;
        int counter;
    private: 
};

#endif // GUMBALL_H

gumball.cpp文件:

#include "Gumball.h"

Gumball::Gumball()
{
    color = " ";
    counter = 0;
}

2 个答案:

答案 0 :(得分:1)

如果没有看到Stack的实施,很难说出问题是什么。但是,由于我发现您的代码的几个部分令人困惑,我认为您可以指出在哪里有用。如果将界面更改为代码以使其更清晰,则可能是您的问题变得明显。

// Interface file  - Stack class definition
class Stack {
    public:
        Stack();
        void push(Gumball); //Does this push to the front or back?  
                            //  The stl uses push_back, and push_front, 
                            //  its good to keep this convention 
        Gumball pop();   //Does this pop the front or back?
        bool isempty();  //This function doesn't change Stack, right? 
                         // if so it should be marked const.
        bool isfull();   //Mark as const?
    private:
        Gumball gumballs[6+1];
        int top;
};

在上述问题中,const的{​​{1}}在以下内容中尤为重要

isempty()

理想情况下,您可以使用迭代器重写整个事物。查看std::find的界面。

答案 1 :(得分:1)

应该是

while(!s.isempty() && g.color != temp.color)
        {
            gumballStack.push(temp);
            g.counter++;
            temp = s.pop();  //temp has been updated
            cout << " " << temp.counter << endl << endl;
        }

但请注意,当吃掉gumball是堆栈中的最后一个时,此代码将无效,因为 s 将为空。

除了同意汤姆并指出你考虑其他解决方案(例如stl :: list)之外,你应该使用这样的东西

if (s.isempty()) {
    cout << "The gumball cannot be found." << endl << endl;
}
while(!s.isempty()) {
    Gumball temp = s.pop();
    if(temp.color == g.color) {
        cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl;
    } else {
        gumballStack.push(temp);
        g.counter++;
        if (s.isempty()) {
             cout << "The gumball cannot be found." << endl << endl;
        }
    }
}
while(!gumballStack.isempty()) {
      s.push(gumballStack.pop());
      gumballStack.pop();
}