Is there a way to check if an element already exists in a stack without popping the entire stack?

huangapple go评论67阅读模式
英文:

Is there a way to check if an element already exists in a stack without popping the entire stack?

问题

问题的目标是从字符串中提取所有元音字母、数字和符号,并将每个字符/整数放入相应的堆栈中(一个元音堆栈、一个数字堆栈和一个符号堆栈)。在迭代字符串并提取所需内容之后,必须根据字符串中的最后一个符号对堆栈中的前两个数字进行算术运算。

其中一个约束条件是堆栈中不能有重复项;处理这个约束条件的方法是使用一个包含已经使用过的元音字母、数字和符号的向量,每次在逻辑上检查字符串的当前字符是否是元音字母、数字或符号时,都要检查字符是否已经“使用过”(即是否存在于已使用的向量中)。

然而,我刚刚被告知在这个任务中不能使用向量,所以我真的不知道该怎么办了。我考虑过创建一个“查找”方法来搜索堆栈,但这需要弹出整个堆栈,而我认为这对我来说不是一个选项,因为我必须显示和处理数字和符号堆栈中的项目。

另外,在评分标准中明确规定我只能使用push()、pop()、isEmpty()、peek()和isFull()。

以下是负责检查并将元素添加到堆栈中的代码:

// 遍历字符串
for (int i = 0; i < toParse.length(); i++)
{

    // 如果字符是元音字母,将其添加到元音堆栈中(如果它还没有在堆栈中)
    if (isVowel(toParse[i]) == true)
    {

        if (find(used.begin(), used.end(), tolower(toParse[i])) == used.end())
        {
            vowels.push(tolower(toParse[i]));
            used.push_back(tolower(toParse[i]));
        }

    }

    // 如果字符是数字,将其添加到数字堆栈中(如果它还没有在堆栈中)
    if (isdigit(toParse[i]))
    {

        if (find(used.begin(), used.end(), toParse[i]) == used.end())
        {

            digits.push(atoi(&toParse[i]));
            used.push_back(toParse[i]);
        }

    }

    // 如果字符是符号,将其添加到符号堆栈中,无论它是否已经在堆栈中
    if (isSymbol(toParse[i]))
    {

        if (find(used.begin(), used.end(), toParse[i]) == used.end())
        {

            symbols.push(toParse[i]);
            used.push_back(toParse[i]);

        }

    }

}
英文:

The objective of my problem is to extract all of the vowels, digits, and symbols from a string and put each char/int in their respective stacks (a vowel stack, a digit stack, and a symbol stack). After iterating through the string and extracting what's needed, I have to do arithmetic with the first two digits in the stack (the last two in the string) depending on what the last symbol in the string was.

One of the constraints is that I cannot have duplicates in the stack; the way that I dealt with this constraint is by having a vector that contained vowels, digits, and symbols that have already been used and each time I go through the logic of checking if the current character of the string is a vowel, digit, or symbol, I check to see if the character is already 'used' (aka present in the used vector).
However, I was just informed that we aren't allowed to use vectors in this assignment so I am genuinely at a loss. I thought about creating a 'find' method to search through the stack, but that would require popping the whole stack and I don't think that's an option for me as I have to display and do things w the items in the digit and symbol stacks.
Also, in the rubric it is specified that I can only use push(), pop(), isEmpty(), peek(), isFull().

Here is the code responsible for checking and adding to the stack:

// traverse through the string
    for (int i = 0; i &lt; toParse.length(); i++)
    {

        // if the char is a vowel, add it to the vowel stack (if it&#39;s not already in the stack)
        if(isVowel(toParse[i]) == true)
        {

            if(find(used.begin(), used.end(), tolower(toParse[i])) == used.end())
            {
                vowels.push(tolower(toParse[i]));
                used.push_back(tolower(toParse[i]));
            }

        }
        
        // if the char is a digit, add it to the digit stack (if it&#39;s not already there)
        if(isdigit(toParse[i]))
        {

            if(find(used.begin(), used.end(), toParse[i]) == used.end() )
            {

                digits.push(atoi(&amp;toParse[i]));
                used.push_back(toParse[i]);
            }

        }

        // if the char is a symbol, add it to the symbol stack regardless of whether it&#39;s   already there or not
        if(isSymbol(toParse[i]))
        {

            if(find(used.begin(), used.end(), toParse[i]) == used.end() )
            {

                symbols.push(toParse[i]);
                used.push_back(toParse[i]);

            }

        }

    }

Thank you for reading; also I'm not expecting anyone to do my homework I would just like a little guidance in the right direction!

Edit 1: As requested, here is the exact text of my task:
[1]: https://i.stack.imgur.com/crM1P.png

答案1

得分: 0

你可以将一个栈中的所有元素弹出,然后逐个检查每个值。这样可以颠倒栈的顺序,然后你需要再次将元素放回原始的栈中。这不是最优雅的解决方案,但或许能满足你的需求。

演示示例如下:

while (!stack1.isEmpty())
{
     var value = stack1.pop();
     // 对值执行某些逻辑或检查
     stack2.push(value);
}

// 恢复原始顺序的栈
while (!stack2.isEmpty())
{
     stack1.push(stack2.pop());
}

另一种方法是在将输入字符串拆分并将其push到各自的栈之前,处理输入字符串并去除重复项。这样,你就不需要检查栈中已经有什么了,因为你已经移除了任何重复项,并确保每个字符只会出现一次。我不确定你的任务有什么限制,但是这个链接可以帮助你开始使用这种方法:https://stackoverflow.com/questions/24496063/remove-duplicates-in-string-algorithm

英文:

You could pop all the elements from a stack into another and check each value as you go. This would reverse the order of the stack so you would need to then repeat this process again into the original stack. It is not the most elegant solution but perhaps will do what you desire.

Example to demonstrate what I meant:

while (!stack1.isEmpty())
{
     var value = stack1.pop();
     // perform some logic or check on the value
     stack2.push(value);
}

// To restore the original stack in the original order
while (!stack2.isEmpty())
{
     stack1.push(stack2.pop());
}

Another approach could be to process the input string and remove duplicates before you split it up and push them onto their respective stacks. This way you don't have to check what is already in the stack as you've removed any duplicates and guaranteed each character will show up once and only once. I'm not sure what the limitations of your assignment are but this link can get you started with this approach: https://stackoverflow.com/questions/24496063/remove-duplicates-in-string-algorithm

huangapple
  • 本文由 发表于 2023年3月7日 22:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定