在堆栈中找到一个项目

Finding One Item in a Stack

本文关键字:一个 项目 堆栈      更新时间:2023-10-16

在我的代码中,我试图找到一个特定的项目放在堆栈上。为此,我将所有项目移动到临时堆栈中,将其从原始堆栈中弹出。在弹出后,我将按照原来的顺序将所有项目移回原来的堆栈中。我的代码从未识别出该项目在堆栈中,所以当它实际上在堆栈中时,我得到它没有找到。你能帮我调试我的循环吗?

int主要:

#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(堆栈。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;
}

我看到了你所说的问题,我把temp放回堆栈多次…绝对不是我的本意,谢谢你指出来。我如何让它在堆栈中添加每个不等于我正在寻找的项目,而不是相同的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;
}

应该是

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将为空。

除了同意tom的观点并指出你考虑其他解决方案(例如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();
}

如果没有看到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;
};

在上述问题中,isempty()const性在以下问题中尤为重要

case 'E':
  s.isempty();  //This should be redundent? 
                // isempty is a question, it shouldnt change s.
  temp = s.pop();
  if(s.isempty() && temp.color == g.color)
  {
     cout << "The " << g.color << " gumball has been eaten." << endl << endl;
  }  
  //Here isempty is being used as a question (as if it doesn't change s 
  // - I presume this is the intended use.
  //Also, .color is not a function, it should be, as will be seen. 
  //Also, temp never gets updated in your loop.
  while(!s.isempty() && g.color != temp.color)
  {
    gumballStack.push(temp);  //Why are you pushing multiple copies 
                              // of the same temp
    g.counter++;   //The counter should be an implementation detail, 
        // it should not be exposed like this. 
        // perhaps overload operator++()?
        //Presumably you want g.color to update when you increase the counter? 
        //this doesn't currently happen because g.color is not a function -
        // it points to data.  
        //I'm guessing that When you call color(), 
        // the function should check the value of the counter
        // and obtain the appropriate color.
    s.pop();  //Did you want to update temp here?
    cout << " " << temp.counter << endl << endl;
  }

理想情况下,您可以使用迭代器重写整个内容。看一下std::find.

的接口
相关文章: