layout: default
title: 找BUG
category: [技术, C/C++]
comments: true
---
找一找BUG
一段代码,实现一个pop,push,和getmin都是O(1)的方法.
源代码
伙伴代码如下,代码的地址可以通过这个访问:
Ubuntu Pastebin
https://paste.ubuntu.com/p/cX2Cq56PYt/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 1000
#define STACKINCREMENT 100
#define OVERFLOW -2
#define INFEASIBLE -1
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize, maxstacksize;
}SqStack;
Status InitStack(SqStack); //初始化栈
Status DestoryStack(SqStack); //销毁栈
Status ClearStack(SqStack); //清空栈
ElemType StackLength(SqStack); //返回栈长度
ElemType GetTop(SqStack); //返回栈顶元素
Status Push(SqStack, ElemType); //入栈
ElemType Pop(SqStack); //出栈
Status StackTraverse(SqStack); //遍历栈
Status StackEmpty(SqStack); //检查栈空
//实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作
//要求:1. pop、push、getMin操作的时间复杂度都是O(1)
// 2. 设计的栈类型可以使用现成的栈结构
#include <stdio.h>
#include "stack.h"
void push(int);
int pop(void);
int getmin(void);
int main()
{
int newIn;
while((newIn = getchar()) != EOF)
{
push(newIn);
}
printf("pop is %d\n", pop());
printf("min is %d\n", getmin());
return 0;
}
SqStack stackData;
SqStack stackMin;
InitStack(stackData);
InitStack(stackMin);
//设计的满足题目要求的入栈操作
void push(int newNum)
{
if(StackEmpty(stackMin))
{
Push(stackMin, newNum);
}
else if(newNum <= GetTop(stackMin))
{
Push(stackMin, newNum);
}
Push(stackData, newNum);
}
//满足题目要求的出栈操作
int pop(void)
{
int value;
if(StackLength(stackData) == 0)
{
printf("stack is empty.\n");
return FALSE;
}
value = Pop(stackData);
if(value == GetTop(stackMin))
{
Pop(stackMin);
}
return value;
}
//满足题目要求的获取最小元素
int getMin(void)
{
if(StackEmpty(stackMin))
{
printf("stack is empyt,no min number.\n");
return FALSE;
}
else
{
return GetTop(stackMin);
}
}
//栈的九种操作
//构造
Status InitStack(SqStack &S)
{
S.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!S.base)
{
printf("栈空间分配失败!\n");
return ERROR;
}
S.top = S.base;
S.stacksize = 0;
S.maxstacksize = STACK_INIT_SIZE;
return OK;
}
//销毁
Status DestoryStack(SqStack &S)
{
S.top = NULL;
free(S.base);
return OK;
}
//清空
Status ClearStack(SqStack &S)
{
S.top = S.base;
S.stacksize = 0;
return OK;
}
//栈长
ElemType StackLength(SqStack S)
{
return S.stacksize;
}
//栈顶
ElemType GetTop(SqStack S)
{
if(S.top == S.base)
{
printf("栈为空!\n");
return FALSE;
}
else
{
return *(S.top - 1);
}
}
//插入
Status Push(SqStack &S, ElemType newNum)
{
if(S.stacksize >= S.maxstacksize)
{
S.base = (ElemType*)realloc(S.base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(ElemType));
if(!S.base)
{
printf("重新分配栈空间失败.\n");
return ERROR;
}
S.top = S.base + STACK_INIT_SIZE;
S.maxstacksize = S.maxstacksize + STACKINCREMENT;
}
*S.top = newNum;
S.top++;
S.stacksize++;
return OK;
}
//删除
ElemType Pop(SqStack &S)
{
int newNum;
if(S.top == S.base)
{
printf("栈为空,删除失败.\n");
return ERROR;
}
S.top--;
newNum = *S.top;
S.stacksize--;
return newNum;
}
//遍历
Status StackTraverse(SqStack S)
{
if(S.top == S.base)
{
printf("栈中没有元素.\n");
return FALSE;
}
else
{
ElemType *p;
p = S.top;
while(p > S.base)
{
p--;
printf("%d ", *p);
}
}
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top == S.base)
{
return TRUE;
}
else
{
return FALSE;
}
}
更改之后代码如下,同样代码可以访问:
Ubuntu Pastebin
https://paste.ubuntu.com/p/rV8B5NHJ9h/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 1000
#define STACKINCREMENT 100
#define OVERFLOW -2
#define INFEASIBLE -1
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize, maxstacksize;
}SqStack;
/*
//先声明也需要添加完整的类型,比较规范wk
Status InitStack(SqStack); //初始化栈
Status DestoryStack(SqStack); //销毁栈
Status ClearStack(SqStack); //清空栈
ElemType StackLength(SqStack); //返回栈长度
ElemType GetTop(SqStack); //返回栈顶元素
Status Push(SqStack, ElemType); //入栈
ElemType Pop(SqStack); //出栈
Status StackTraverse(SqStack); //遍历栈
Status StackEmpty(SqStack); //检查栈空
*/
Status InitStack(SqStack &S); //初始化栈
Status DestoryStack(SqStack &S); //销毁栈
Status ClearStack(SqStack &S); //清空栈
ElemType StackLength(SqStack S); //返回栈长度
ElemType GetTop(SqStack S); //返回栈顶元素
Status Push(SqStack &S, ElemType &E); //入栈
ElemType Pop(SqStack &S); //出栈
Status StackTraverse(SqStack S); //遍历栈
Status StackEmpty(SqStack S); //检查栈空
//实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作
//要求:1. pop、push、getMin操作的时间复杂度都是O(1)
// 2. 设计的栈类型可以使用现成的栈结构
//#include <stdio.h>//这里不该再次出现,要么开头一次性wk
//#include "stack.h"
//void push(int);
void push(int n);//声明需要参数,不只是类型wk
//int pop(void);
int pop();//为空可以省略,一般也没人会去写void wk
//int getmin(void);
int getmin();//同上
/*
//为了排除错误,直接诶移到后面wk
int main()
{
int newIn;
while((newIn = getchar()) != EOF)
{
push(newIn);
}
printf("pop is %d\n", pop());
printf("min is %d\n", getmin());
return 0;
}
*/
SqStack stackData;
SqStack stackMin;
//InitStack(stackData);
//InitStack(stackMin);
//设计的满足题目要求的入栈操作
void push(int newNum)
{
if(StackEmpty(stackMin))
{
Push(stackMin, newNum);
}
else if(newNum <= GetTop(stackMin))
{
Push(stackMin, newNum);
}
Push(stackData, newNum);
}
//满足题目要求的出栈操作
//int pop(void)
int pop()//同上wk
{
int value;
if(StackLength(stackData) == 0)
{
printf("stack is empty.\n");
return FALSE;
}
value = Pop(stackData);
if(value == GetTop(stackMin))
{
Pop(stackMin);
}
return value;
}
//满足题目要求的获取最小元素
//int getMin(void)
int getmin()//同上,同时大小写不可以写错,不然会找不到 wk
{
int t;
if(StackEmpty(stackMin))
{
printf("stack is empyt,no min number.\n");
return FALSE;
}
else
{
t=GetTop(stackMin);
return t;
}
}
//栈的九种操作
//构造
Status InitStack(SqStack &S)
{
S.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!S.base)
{
printf("栈空间分配失败!\n");
return ERROR;
}
S.top = S.base;
S.stacksize = 0;
S.maxstacksize = STACK_INIT_SIZE;
return OK;
}
Status DestoryStack(SqStack &S)
//销毁
{
S.top = NULL;
free(S.base);
return OK;
}
//清空
Status ClearStack(SqStack &S)
{
S.top = S.base;
S.stacksize = 0;
return OK;
}
//栈长
ElemType StackLength(SqStack S)
{
return S.stacksize;
}
//栈顶
ElemType GetTop(SqStack S)
{
if(S.top == S.base)
{
printf("栈为空!\n");
return FALSE;
}
else
{
return *(S.top - 1);
}
}
//插入
//Status Push(SqStack &S, ElemType newNum)
Status Push(SqStack &S, ElemType &newNum)//同声明处理
{
if(S.stacksize >= S.maxstacksize)
{
S.base = (ElemType*)realloc(S.base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(ElemType));
if(!S.base)
{
printf("重新分配栈空间失败.\n");
return ERROR;
}
S.top = S.base + STACK_INIT_SIZE;
S.maxstacksize = S.maxstacksize + STACKINCREMENT;
}
*S.top = newNum;
S.top++;
S.stacksize++;
return OK;
}
//删除
ElemType Pop(SqStack &S)
{
int newNum;
if(S.top == S.base)
{
printf("栈为空,删除失败.\n");
return ERROR;
}
S.top--;
newNum = *S.top;
S.stacksize--;
return newNum;
}
//遍历
Status StackTraverse(SqStack S)
{
if(S.top == S.base)
{
printf("栈中没有元素.\n");
return FALSE;
}
else
{
ElemType *p;
p = S.top;
while(p > S.base)
{
p--;
printf("%d ", *p);
}
}
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top == S.base)
{
return TRUE;
}
else
{
return FALSE;
}
}
int main()
{
int newIn;
while((newIn = getchar()) != EOF)
{
push(newIn);
}
printf("pop is %d\n", pop());
int min=getmin();
printf("min is %d\n", min);
return 0;
}
备注
其实,有个大小写的问题找了好一会,因为没找到对应的函数,一个大小写不同,会导致函数的声明不对应的.