找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没有头绪或者现象古怪不知道从哪里下手,找有经 ...
随机推荐
- Soldier and Badges (set的检索简单运用)
Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolne ...
- LeetCode Predict the Winner
原题链接在这里:https://leetcode.com/problems/predict-the-winner/description/ 题目: Given an array of scores t ...
- LeetCode Student Attendance Record I
原题链接在这里:https://leetcode.com/problems/student-attendance-record-i/description/ 题目: You are given a s ...
- Tomcat 工作原理 1 (转)
Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...
- [转] AS3地图拼接与战争迷雾的实现
在开发游戏的过程中,特别是地图编辑器中,需要利用最少的资源,实现最丰富的地形地貌.虽然现在众多的RPG开始使用整图,但是我们偶尔还是需要能够让玩家自己编辑地图,或者其他需要自动进行地图构建的功能.另外 ...
- python3之编码
这个符号(#!)的名称,叫做"Shebang"或者"Sha-bang"Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件 ...
- HDU5475(线段树)
An easy problem Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- shell脚本 - 快速到达目录
服务器中存放很多工程目录,通过ssh登录,需要手动敲命令,很麻烦,可以建立自动登录脚本 1.在服务器登录是默认的目录中建立脚本文件 vi drivers.sh #建立登录脚本 cd data/work ...
- 机器学习:PCA(人脸识别中的应用——特征脸)
一.思维理解 X:原始数据集: Wk:原始数据集 X 的前 K 个主成分: Xk:n 维的原始数据降维到 k 维后的数据集: 将原始数据集降维,就是将数据集中的每一个样本降维:X(i) . WkT = ...
- java代码包装类----------Integer
总结:我的笔试挂了..基础的继承不懂不会.我不知道到底是哪里的问题. 要好好反思 package com.da.ima2; public class jrfk { // int整型不能直接转化为Int ...