是课程平时练习要求写的代码,在运行时遇到断点后闪退,错误提示如下图
这个问题之前也出现过,依旧是不知道怎么解决,希望能为我解答一下!
#include<stdio.h>
#include<stdlib.h>
typedef struct Stack
{
int *base;
int top;
int size;
int lenth;
}SqStack;
SqStack InitStack(int n)
{
SqStack L;
L=(SqStack)malloc(sizeof(Stack));
L->base=(int)malloc(nsizeof(int));
L->top=L->base;
L->size=n;
L->lenth=0;
return L;
}
SqStack Push(SqStack &L,int e)
{
(L->top)=e;
L->top++;
L->lenth++;
return L;
}
int Pop(SqStack &L)
{
int e;
e=(L->top);
L->top--;
L->lenth--;
return e;
}
int GetHead(SqStack &L)
{
return *(L->top);
}
int CountByHeigt(SqStack &L)
{
int aim;
aim=Pop(L);
int cnt=0;
while(L->lenth!=0)
{
if(GetHead(L)<aim)
{
Pop(L);
cnt++;
}
else
{
aim=Pop(L);
}
}
return cnt;
}
void Destroy(SqStack &L)
{
free(L->base);
L->base=NULL;
L->top=NULL;
L->size=0;
L->lenth=0;
}
int main()
{
int num,height,count;
scanf("%d",&num);
SqStack L;
L=InitStack(num);
for(int i=0;i<num;i++)
{
scanf("%d",&height);
Push(L,height);
}
count=CountByHeigt(L);
Destroy(L);
printf("%d\n",count);
return 0;
}
代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct Stack
{
int* base;
int* top;
int size;
int lenth;
}SqStack;
SqStack* InitStack(int n)
{
SqStack* L;
L = (SqStack*)malloc(sizeof(Stack));
L->base = (int*)malloc(n*sizeof(int));
L->top = L->base;
L->size = n;
L->lenth = 0;
return L;
}
SqStack* Push(SqStack* &L, int e)
{
*(L->top) = e;
L->top++;
L->lenth++;
return L;
}
int Pop(SqStack* &L)
{
int e;
//修改 判断是否已经为空
if (L->lenth == 0 || L->base == L->top)
return -1;
L->top--;
e = *(L->top);
L->lenth--;
return e;
}
int GetHead(SqStack* &L)
{
return *(L->top);
}
int CountByHeigt(SqStack* &L)
{
int aim;
aim = Pop(L);
int cnt = 0;
int* p;
while (L->lenth > 0)
{
p = L->top;
p--;
while (1)
{
if (*p < aim)
{
cnt++;
if (p == L->base)
break;
else
p--;
}
else
break;
}
aim = Pop(L);
}
return cnt;
}
void Destroy(SqStack* &L)
{
free(L->base);
L->base = NULL;
L->top = NULL;
L->size = 0;
L->lenth = 0;
//修改,是否L的空间
free(L);
L = 0;
}
int main()
{
int num, height, count;
scanf("%d", &num);
SqStack* L;
L = InitStack(num);
for (int i = 0; i < num; i++)
{
scanf("%d", &height);
Push(L, height);
}
count = CountByHeigt(L);
Destroy(L);
printf("%d\n", count);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!