单调栈实现连续矩形最大面积的算法,报错如上,求大神指点

# Microsoft C++ 异常: std::bad_alloc,位于内存位置 0x00CDD8DC 处,我写的单调栈实现连续矩形最大面积的算法,报错如上,求大神指点

图片说明图片说明

代码思想用的是这位博主的https://blog.csdn.net/u012534831/article/details/74356851

#include "pch.h"
#include <iostream>
#include<stdio.h>
#include<string.h>
typedef struct LNode{
    int data;
struct LNode*next;
} LNode,*LinkList;
typedef LinkList LinkStack;
void InitStack_L(LinkStack &S) {
    S = NULL;
}
void DestroyStack_L(LinkStack &S) {
    LinkList p;
    while (S) {
        p = S;
        S = S->next;
        delete p;
    }
}
bool GetTop_L(LinkStack &S,int &e) {
    if (!S)
        return false;
    e = S->data;
    return true;
}
void Push_L(LinkStack &S, int e) {
    LinkList p;
    p = new LNode;
    p->data = e;
    p->next = S;
    S = p;
}
bool Pop_L(LinkStack &S, int &e) {
    LinkList p;
    if (!S)
        return false;
    p = S;
    S = S->next;
    e = p->data;
    delete p;
    return true;
}
int StackEmpty_L(LinkStack &S) {
    if (S = NULL)
        return true;
    return false;
}
int main()
{
    int n, b, c, max = 0, sum = 0, L, R, i, a[100005];
    LinkStack S;
    LinkList p;
    memset(a, 0, sizeof(a));
    InitStack_L(S);
    scanf_s("%d", &n);
    for (i = 0; i < n; i++) {
        scanf_s("%d", &a[i],1);
    }
    i = 0;
    while (i < n) {
        if (i = 0) {
            Push_L(S, i);
            i++;
            continue;
        }
        GetTop_L(S,b);
        if (a[i] > b) {
            Push_L(S, i);
            i++;
        }
        else if (a[i] = b) {
            continue;
        }
        else {
            Pop_L(S, c);
            p = S;
            L = p->next->data + 1;
            R = i - 1;
            sum = a[c] * (R - L + 1);
        }
        if (sum > max) {
            max = sum;
        }
    }
    while (!StackEmpty_L(S)) {
        Pop_L(S, c);
        if (StackEmpty_L(S))
            L = 0;
        else {
            p = S;
            L = p->next->data + 1;
        }
        R = i - 1;
        sum = a[c] * (R - L + 1);
        if (sum > max) {
            max = sum;
        }
    }
    DestroyStack_L(S);
    printf("%d", max);
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐