从简单连接的列表中删除最大值
Remove max value from simply-connected list
如何从简单连接列表中删除最大值?
我尝试的两种解决方案产生了错误的结果。请向我解释我做错了什么。使用代码,如果不难的话。
叠:
struct Stack
{
int info;
Stack *next;
} *top;
错误的解决方案 1:
void delMaxValue(Stack **stck, int maxValue){
Stack *tmp = NULL;
do {
if ((*stck)->info != maxValue)
tmp = *stck;
cout << tmp->info << endl;
tmp = tmp->next;
*stck = (*stck)->next;
} while ((*stck)->next != NULL);
while (tmp != NULL)
{
*stck = tmp;
*stck = (*stck)->next;
tmp = tmp->next;
}
错误的解决方案 2:
Stack* deleteMaxValue(Stack *begin) {
Stack *t = begin, *p = begin->next;
for (; p; p = p->next)
if (p->info > t->info) t = p;
p = begin;
if (p != t) {
while (p->next != t) p = p->next;
p->next = t->next;
}
else
begin = t->next;
delete t;
return begin;}
#include <cstdio>
#include <iostream>
struct Stack
{
int info;
Stack *next;
// added just to easy initialization
Stack(int _info, Stack *_next) : info(_info), next(_next) {}
} *top;
void delMaxValue(Stack *&head)
{
// first - find MaxValue in the list
// as you can see, i save pointer to the previous element in the list
Stack* max_prev = nullptr;
Stack* max = head;
for(Stack *i_prev = nullptr, *i = head; i; i_prev = i, i = i->next) {
if (max->info < i->info) {
max_prev = i_prev;
max = i;
}
}
// max has the maximum value and max_prev is the element before max in the list
// now we remove max
if (max_prev == nullptr) {
// max has no prev, so max is the head of the list. We assign the new head
head = max->next;
} else {
max_prev->next = max->next;
max->next = NULL;
}
}
void printStack(Stack *head) {
std::cout << "Priting " << head << std::endl;
for(Stack *i = head; i; i = i->next) {
std::cout << i << " " << i->info << std::endl;
}
}
int main()
{
Stack *head = new Stack(1, new Stack(15, new Stack(10, nullptr)));
printStack(head);
delMaxValue(head);
printStack(head);
return 0;
}
您可能对来自 bsd 的列表帮助宏感兴趣,现在可在 glibc、newlib、openbsd 等中使用,请参阅此处。
第一个解决方案将最大值作为参数,而第二个解决方案则不采用。我假设我们没有最大值,并将在处理堆栈时计算它。
基本方法应该是首先考虑一个逻辑。
步骤 1.(我们需要弹出所有元素以找到堆栈中的最大元素。此外,我们需要将弹出的所有值存储在另一个堆栈(例如辅助(中。现在,我们知道最大值(例如MAX(。
步骤 2.(请注意,我们现在将反向堆栈。从辅助堆栈中弹出所有元素,如果值不是 max,则将它们推送到原始堆栈中。
数据最初,
Original Stack: 1->2->3->4->100->5->7->NULL
Auxiliary Stack: NULL
第一步后的数据,
Original Stack: NULL
Auxiliary Stack: 7->5->100->4->3->2->1->NULL
MAX: 100
最后
Original Stack: 1->2->3->4->5->7->NULL
Auxiliary Stack: NULL
尝试为此编写代码。您的两种解决方案的工作方式都与预期不同。
我希望这会有所帮助。
#include <iostream>
struct LList
{
int info;
LList *next;
//constructer
LList(int info_) :info(info_) {
next = nullptr;
}
};
void removeMaxValue(LList *&root) {
int max = 0;
LList *temp = root;
//Searching for max value
while (temp!=nullptr)
{
if (temp->info > max)
max = temp->info;
temp = temp->next;
}
temp = root;
//Find max value and remove
while (temp->next->info != max)
temp = temp->next;
LList *maxNode = temp->next;
temp->next = temp->next->next;
delete maxNode;
}
void print(const LList *root)
{
while (root!=nullptr)
{
std::cout << root->info << " ";
root = root->next;
}
std::cout << std::endl;
}
int main() {
LList *root = new LList(15);
root->next= new LList(10);
root->next->next= new LList(45);
root->next->next->next = new LList(85);
root->next->next->next->next = new LList(5);
//before removing
print(root);
removeMaxValue(root);
//After removing
print(root);
std::cin.get();
}
这两个函数采用两种不同的方法。我选择了函数不知道实际最大值是多少的函数,因此它必须首先找到它。
首先,该函数只是遍历元素并选择最大值。 然后,它会搜索包含此值的第一个节点并删除该节点。
void stackRemoveMaxValue(Stack*& top) {
if(top == nullptr) {
return;
}
// Find max value.
int maxValue = top->info;
Stack* node = top->next;
for(; node != nullptr; node = node->next) {
if(maxValue < node->info) {
maxValue = node->info;
}
}
// Remove first node that contains maxValue.
Stack* previous = nullptr;
Stack* current = top;
do {
if(current->info != maxValue) {
previous = current;
current = current->next;
} else {
if(previous != nullptr) {
previous->next = current->next;
} else {
top = current->next;
}
delete current;
return;
}
} while(current != nullptr);
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何使用可变参数模板类使用模板元编程获得最大值
- C++不同的最小最大值实现
- Vec3b:r/g/b 颜色的最大值为 254,而不是 255
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 是否可以将无符号 int 的最大值转换为 int 并将结果转换为 -1?
- 查找包含 N 个元素的数组的最小值和最大值
- C++ 从数组中删除一个最大值和零
- 从简单连接的列表中删除最大值
- 查找最大值并删除 2D 数组中的该行.C++