找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没有头绪或者现象古怪不知道从哪里下手,找有经 ...
随机推荐
- LeetCode Target Sum
原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...
- MMS(mongodb监控工具)
今天好几个人问我如何查看mongodb的连接数,在mongo shell中执行: shard1:PRIMARY> db.serverStatus().connections { "cu ...
- node.js 笔记(一)
参考:https://github.com/alsotang/node-lessons 感谢!!! 本文属于小白入门级笔记,请大牛自动屏蔽!!! 1. 开发环境 os: 10.12.6 nod ...
- Verilog数组表示及初始化
(转)Verilog数组表示及初始化 这里的内存模型指的是内存的行为模型.Verilog中提供了两维数组来帮助我们建立内存的行为模型.具体来说,就是可以将内存宣称为一个reg类型的数组,这个数组中的任 ...
- Log4net系统日志
首先:引用Log4net.dll,按照说明进行web.config配置 然后:在Global中写入: protected void Application_Start(object sender, E ...
- IIS配置文档
IIS配置文档: 1.安装IIS.控制面板→程序→打开关闭Windows功能,Web管理服务和万维网服务都勾上. 2.部署网站:ASP.Net项目的发布:项目中点右键“发布”,选择“文件系统”,发布到 ...
- catkin 工作空间
catkin 工作空间:组织和管理功能包的文件夹,以 catkin 工具编译 建立工作空间 sch01ar@ubuntu:~$ mkdir -p ~/catkin_ws/src sch01ar@ubu ...
- 无人零售的黑科技:RFID技术
无人零售的黑科技:RFID技术说起最近的热门话题,“无人零售商店”当属其一.自去年底,亚马逊推出第一家无人实体超市Amazon Go,到阿里.京东.大润发等各大企业纷纷加入,无人商店被推上了风口浪尖. ...
- DAY10-python并发之IO模型
一 IO模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问 ...
- c#.net常用字符串函数 字符串常用方法 string
RegionsStr = RegionsStr.Remove(RegionsStr.LastIndexOf(","), 1); //去掉最后一个逗号 string html = ...