找BUG
找一找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,名字注意大小写
{
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)
{
int t;
if(S.top == S.base)
{
printf("栈为空!\n");
return FALSE;
}
else
{
t=*(S.top - 1);
return t;
}
}
//插入
//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;
printf("push %d.\n",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;
InitStack(stackData);//初始化在主函数内部wk
InitStack(stackMin);//
// while((newIn = getchar()) != -1)
while((scanf("%d",&newIn)) != -1)//获取字母更改为数字wk
{
push(newIn);
}
printf("pop is %d\n", pop());
printf("min is %d\n", getmin());
return 0;
}
再次更正之后代码
这次代码是在纯c里面编译通过的,主要是&符号在c里面作为形参形参似乎不行,只能全部换了,考虑到要赋值处理,就没有直接用结构体作为形参.
而是将指针作为形参进行操作,这次代码应该是可以运行了.
#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,名字注意大小写
{
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)
{
int t;
if(S.top == S.base)
{
printf("栈为空!\n");
return FALSE;
}
else
{
t=*(S.top - 1);
return t;
}
}
//插入
//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;
printf("push %d.\n",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;
InitStack(&stackData);//初始化在主函数内部wk
InitStack(&stackMin);//
// while((newIn = getchar()) != -1)
while((scanf("%d",&newIn)) != -1)//获取字母更改为数字wk
{
push(newIn);
}
printf("pop is %d\n", pop());
printf("min is %d\n", getmin());
return 0;
}
备注
其实,有个大小写的问题找了好一会,因为没找到对应的函数,一个大小写不同,会导致函数的声明不对应的.
目前是指针对vc6.0的环境测试成功了,但是纯c的环境有些问题,后面继续更新.
找BUG的更多相关文章
- 海王星给你好看!FineUI v4.0公测版发布暨《你找BUG我送书》活动开始(活动已结束!)
<FineUI v4.0 你找BUG我送书>活动已结束,恭喜如下三位网友获得由 FineUI 作者亲自翻译的图书<jQuery实战 第二版>! 奋斗~ 吉吉﹑ purplebo ...
- 第二次作业:找Bug
引子 我真的想了一个小时,上哪里去找bug.我昨天还留意到一个bug,今天就不见了.灵光不断,我想起来了.我就要找大公司的产品的bug... 第一部分 调研, 评测 体验. <腾讯桌球>是 ...
- 附加题程序找bug
private: void Resize(int sz){ ){ return; } if(maxSize != sz){ T *arr = new T[sz]; if(arr == NULL){ r ...
- 在无法单步调试的情况下找Bug的技巧
比如说你有一个大的模块A,其组成部分有B,C,D这3个小的模块,现在A出了一个BUG,因为某种原因的限制你无法单步调试.怎么较快地定位BUG发生的根源? 这里记录一下刚才我在找BUG的时候采用的思路, ...
- 判断空间上三个点是否共线问题【找bug篇】
判断空间上三个点是否在同一直线上[找bug篇] 作者:Vashon 时间:20150601 发布时间:20150718 一.拿到问题,首先分析并理清思路. 判断三点是否在同一条直线上需满足以下几点 ...
- 找bug的过程
关于昨天程序出差我找bug的过程记录 昨天才程序 https://www.cnblogs.com/pythonywy/p/11006273.html ├── xxxx │ ├── src.py │ └ ...
- 如何正确的找BUG
什么是BUG 漏洞是在硬件.软件.协议的具体实现或系统安全策略上存在的缺陷,从而可以使攻击者能够在未授权的情况下访问或破坏系统.具体举例来说,比如在Intel Pentium芯片中存在的逻辑错误,在S ...
- 一起找bug
帮同学找的一个bug,错误代码如下: package dai_test; public class Test1 { public static void main(String[] args) { / ...
- 程序员怎样在复杂代码中找 bug?(简单)
分享下我的debug的经验 1. 优先解决那些可重现的,可重现的bug特别好找,反复调试测试就好了,先把好解决的干掉,这样最节约时间. 2. 对于某些bug没有头绪或者现象古怪不知道从哪里下手,找有经 ...
随机推荐
- UVALive - 3490 Generator (AC自动机+高斯消元dp)
初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ...
- 【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- Extjs5.0 学习之路【结构篇】
Extjs5.0在原有的MVC模式下增加了一个MVVM Extjs5.0项目文件执行顺序. 新增特性一 bind---data
- Day2-VIM(一):移动
基础 字符移动 k 上移 k h 左移 h l l 右移 j j 下移 你也可以使用键盘上的方向键来移动,但这么做h j k l的存在就失去了意义 之所以使用h j k l来控制方向,其主要目的是让你 ...
- Python:列表中,增加元素、删除元素、切片、其它
一.向列表中增加元素 list.append(单个元素):在list列表末端增加一个元素: list.extend([元素1,元素2]):在list列表末端增加多个元素: list.insert(元素 ...
- install命令和cp命令的区别
基本上,在Makefile里会用到install,其他地方会用cp命令. 它们完成同样的任务——拷贝文件,它们之间的区别主要如下: 1.最重要的一点,如果目标文件存在,cp会先清空文件后往里写入新文件 ...
- Java多线程中的常用方法
本文将带你讲诉Java多线程中的常用方法 Java多线程中的常用方法有如下几个 start,run,sleep,wait,notify,notifyAll,join,isAlive,current ...
- Oracle 多表查询(1)
一.基本概念 多表查询的语法如下: SELECT [DISTINCT] * | 字段 [别名] [,字段 [别名] ,…]FROM 表名称 [别名], [表名称 [别名] ,…][WHERE 条件(S ...
- EF CODEFIRST WITH ORACLE
摸索了半天,运行通过了,但是还是有一点坑的,对于初次使用的人来说,可能会遇到几个问题 首先安装两个dll 如果你已经下载好了dll Oracle.ManagedDataAccess.dll Oracl ...
- lombok与spring的恩怨
下面是lombok按照 Java Bean 的规范生成的 下面是spring mvc里jackson 需要的 xXxx问题还是顺势而为吧