C++ 从简单连接列表中删除最大值
如何从简单连接列表中删除最大值 我尝试的两种解决方案产生了错误的结果。请向我解释我做错了什么。用代码,如果不难的话 堆栈:C++ 从简单连接列表中删除最大值,c++,stack,C++,Stack,如何从简单连接列表中删除最大值 我尝试的两种解决方案产生了错误的结果。请向我解释我做错了什么。用代码,如果不难的话 堆栈: struct Stack { int info; Stack *next; } *top; void delMaxValue(Stack **stck, int maxValue){ Stack *tmp = NULL; do { if ((*stck)->info != maxValue) tmp = *stck;
struct Stack
{
int info;
Stack *next;
} *top;
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;
}
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;}
错误的解决方案1:
struct Stack
{
int info;
Stack *next;
} *top;
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;
}
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;}
第一个解决方案将最大值作为参数,而第二个解决方案则没有。我假设我们没有最大值,并在处理堆栈时计算它 基本方法应该是首先考虑逻辑 步骤1.)我们需要弹出所有元素,以找到堆栈中的最大元素。此外,我们还需要将弹出的所有值存储在另一个堆栈中(例如,辅助堆栈)。现在,我们知道了最大值(比如MAX) 步骤2.)注意,现在我们将使堆栈反向。从辅助堆栈中弹出所有元素,如果该值不是最大值,则将它们推入原始堆栈 数据最初
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();
}
#包括
结构主义者
{
国际信息;
下一步;
//建设者
LIST(内部信息):信息(信息){
next=nullptr;
}
};
void removeMaxValue(LList*&根){
int max=0;
LList*temp=根;
//搜索最大值
while(temp!=nullptr)
{
如果(临时->信息>最大值)
最大值=温度->信息;
温度=温度->下一步;
}
温度=根;
//找到最大值并删除
同时(临时->下一步->信息!=最大值)
温度=温度->下一步;
LList*maxNode=temp->next;
临时->下一步=临时->下一步->下一步;
删除maxNode;
}
无效打印(常量列表*根)
{
while(root!=nullptr)
{
std::cout info next;
}
std::cout next=新的LList(10);
root->next->next=new-LList(45);
root->next->next->next=new-LList(85);
root->next->next->next->next=新的LList(5);
//移除前
打印(根);
removeMaxValue(根);
//移除后
打印(根);
std::cin.get();
}
#包括
#包括
结构堆栈
{
国际信息;
堆栈*下一步;
//添加了简单的初始化
堆栈(int\u info,Stack*\u next):info(\u info),next(\u next){
}*顶部;
void delMaxValue(堆栈*&head)
{
//首先-在列表中查找MaxValue
//如您所见,我保存了指向列表中上一个元素的指针
堆栈*max_prev=nullptr;
堆栈*最大值=头;
对于(堆栈*i_prev=nullptr,*i=head;i;i_prev=i,i=i->next){
如果(最大->信息信息){
max_prev=i_prev;
max=i;
}
}
//max具有最大值,max_prev是列表中max之前的元素
//现在我们移除max
if(max_prev==nullptr){
//max没有prev,因此max是列表的头。我们分配新的头
head=max->next;
}否则{
max\u prev->next=max->next;
max->next=NULL;
}
}
无效打印堆栈(堆栈*头){
std::cout您的两个函数采用两种不同的方法。我选择了一种方法,其中函数不知道实际的最大值是多少,因此必须首先找到它
首先,函数只是遍历元素并选择最大值。
然后搜索包含此值的第一个节点并删除该节点
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);
}
void stackRemoveMaxValue(堆栈*&顶部){
if(top==nullptr){
返回;
}
//找到最大值。
int maxValue=top->info;
堆栈*节点=顶部->下一步;
对于(;node!=nullptr;node=node->next){
如果(最大值<节点->信息){
maxValue=节点->信息;
}
}
//删除包含maxValue的第一个节点。
Stack*previous=nullptr;
堆栈*当前=顶部;
做{
如果(当前->信息!=最大值){
先前=当前;
当前=当前->下一步;
}否则{
如果(上一个!=nullptr){
上一个->下一个=当前->下一个;
}否则{
顶部=当前->下一步;
}
删除当前文件;
返回;
}
}while(当前!=nullptr);
}
很抱歉,您似乎重新排列了所有指针,而不仅仅是删除的节点。遍历链接,直到找到您要查找的值。然后更改一个指针prev->next=this->next;
。现在此
已离开列表,可以处理。