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

时间:2018-06-06 12:46:37

标签: c++ stack

如何从Simply-Connected列表中删除最大值?

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

堆栈:

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

4 个答案:

答案 0 :(得分:0)

您的第一个解决方案将最大值作为参数,而第二个解决方案则不是。我假设我们没有最大值,并在处理堆栈时计算它。

基本方法应该是首先考虑逻辑。

步骤1.)我们需要弹出所有元素以找到堆栈中的最大元素。此外,我们需要存储我们在另一个堆栈中弹出的所有值(例如,辅助)。现在,我们知道最大值(比如 MAX )。

步骤2.)注意我们现在将反向堆栈。弹出辅助堆栈中的所有元素,如果值不是max,则将它们推入原始堆栈中。

数据最初,

<!DOCTYPE html>
<html>
  <title>Test</title>
  <head>
  <script type="text/javascript">
    function createWindow() {
        var win    = window.open()
        var html   = document.getElementById('foo').innerHTML;
        win.document.open();
        win.document.write(html);
        win.document.close();
    }
  </script>
  </head>
  <body>
      <div id="foo">
        I come home in the morning light <br/>
        My mother says when you gonna live your life right <br/>
        Oh mother dear we're not the fortunate ones <br/>
        And girls they wanna have fun <br/>
        Oh girls just want to have function <br/>
      </div>
       <button onclick="createWindow()">Click me</button> 
  </body>
</html>

第一步后的数据,

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

尝试为此编码。您的两种解决方案都采用与预期不同的方式。

答案 1 :(得分:0)

我希望它会有所帮助。

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

答案 2 :(得分:0)

#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等中使用,请参阅here

答案 3 :(得分:0)

您的两个功能采用两种不同的方法。我选择的函数不知道实际的最大值是什么,所以必须先找到它。

首先,函数只迭代元素并选择最大值。 然后它搜索包含该值的第一个节点并删除该节点。

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