在堆栈中移动元素
Moving Elements In A Stack
我有一个使用数组实现的堆栈,它将6个字符作为用户的输入(小案例和大写字母)。然后将上箱字母转移到另一个堆栈(将其从原始堆栈中删除)
我有以下代码,它将元素沿堆栈移动到顶部,然后将其推到顶部并将其推到新数组。
for (int i = P->top(); i >= 0; i--)
{
if (P->data[i] >= 65 && P->data[i] <= 90)
{
for (int j = i; j < P->top(); i++)
{
char swap = P->data[i + 1];
P->data[i + 1] = P->data[i];
P->data[i] = swap;
stackT.push(P->pop());
i = P->top();
}
}
}
我应该还有另一个带有大写字母的阵列,而原始的则带有小写字母。
它得到了奇怪的结果,有时它会完全摧毁原始数组,有时它只会传递一个高层元素并弄乱了其他元素。
我只是没有想法。
是的,问题仅限于堆栈的使用,而不是这不是作业。
预先感谢!
不要在内部移动事物
(我希望本练习的目的是您仅使用堆栈操作,并且只能在"顶部"上修改堆栈 - 其他任何东西都不是堆栈。)
而是使用第三个临时的堆栈。
stack original = .. whatever ...;
stack uppercase;
stack other;
// Classify the contents
while (!original.empty())
{
char c = original.top();
original.pop();
if (std::isupper(c))
{
uppercase.push(c);
}
else
{
other.push(c);
}
}
// Restore the non-uppercase stack.
while (!other.empty())
{
original.push(other.top());
other.pop();
}
相关文章:
- 为什么复制而不是移动数据元素?
- 设计将引用元素移动到开头的数据结构.C++
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 如何将数组中的元素向右移动?
- 在同一容器中移动元素
- 移动数组C++中的特定元素
- C++:我可以重用/移动 std::list 元素从中间到结尾吗?
- 在数组的开头添加一个元素并移动其余元素,但保留大小
- 如何将 std::vector 的某些元素移动到向量中的新索引?
- boost multi_index - 如果元素类型仅支持移动语义,如何遍历它?
- 在 C++ 中移动数组元素
- 从标准容器中移动元素是否合法?
- 为什么我不能将元素移动到不可复制的向量中?
- 如何将 std::vector 中的元素移动 1
- 将列表元素移动到列表末尾的函数多次不起作用
- 将 vector<vector> 的最后一个元素移动到<int>开头
- 将数组的特定元素移动到新数组
- 在 C 和 C++ 中,如何将数组中的某些元素移动到新数组中
- 我可以将一个QObject数组的单个元素移动到另一个QThread吗
- 将向量元素移动到向量的后面