从简单连接的列表中删除最大值

Remove max value from simply-connected list

本文关键字:删除 最大值 列表 简单 连接      更新时间:2023-10-16

如何从简单连接列表中删除最大值?

我尝试的两种解决方案产生了错误的结果。请向我解释我做错了什么。使用代码,如果不难的话。

叠:

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);
}