调用初始化栈函数为什么栈顶指示变量没被初始化(被初始化为-1)

lymboy 2017-06-16 02:59:36
我在用栈实现进制转换时,编译可以通过,但一运行就提示出错。调试程序发现初始化函数本应将st.top赋值为-1,但却是123(这是为什么?)而且数据域data第一个元素的值是256(我调试用的数据是123和2),这其中是不是有什么关系?
求各位佬解惑!蟹蟹^_^

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX_SIZE 100

typedef int SElemType;
char LIST[]="0123456789ABCDEF";

typedef struct
{
SElemType data[MAX_SIZE];
int top;
}STACK;

int InitStack(STACK *s); //初始化栈
int Push(STACK *s, SElemType x); //压栈
int EmptyStack(STACK *s); //判断栈是否为空
int Pop(STACK *s, SElemType *x); //出栈
void DecToOthers(int n, int hex); //进制转换
int main(void)
{
int n, hex;

scanf("%d %d",&n,&hex);
DecToOthers(n,hex);

return 0;
}

/*
* Function: ========初始化栈========
* 形参:传入栈的指针,若成功初始化,则此指针指向栈
* 返回值:0:初始化失败 1:初始化成功
*/
int InitStack(STACK *s)
{
s=(STACK *)malloc(sizeof(STACK));
if (!s)
{
printf("分配空间失败!\n");
return 0;
}
s->top=-1; //赋值为-1
return 1;
}

/*
* Function:======将数据压入栈中======
* 形参: S:指向栈的指针 x: 待压入的数据
* 返回值:0:栈满,无法压入 1:压入成功
*/
int Push(STACK *s, SElemType x)
{
if (s->top==MAX_SIZE-1)
{
printf("\nStack is full!\n");
return 0;
}
s->top++;
s->data[s->top]=x;

return 1;
}

/*
* Function:=======判断栈空=======
* 形参:s: 指向栈的指针
* 返回值:1:栈为空 0:栈不为空
*/
int EmptyStack(STACK *s)
{
return ((s->top==-1)?1:0);
}

/*
* Function: ========出栈========
* 形参:s: 指向栈的指针 x: 指向栈顶元素
* 返回值:0:栈为空 1:出栈成功
*/
int Pop(STACK *s, SElemType *x)
{
if (EmptyStack(s))
{
printf("\nStack is free!\n");
return 0;
}
*x=s->data[s->top];
s->top--;

return 1;
}

/*
* Functiom: ======进制转换======
* 形参:n:十进制数 hex: 进制
*/
void DecToOthers(int n, int hex)
{
int x;
STACK st;
InitStack(&st);

while (n)
{
Push(&st, n%hex);
n/=hex;
}
while (!EmptyStack(&st))
{
Pop(&st, &x);
printf("%c",LIST[x]);
}
}
...全文
397 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-06-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#define MAX_SIZE 100

typedef int SElemType;
char LIST[]="0123456789ABCDEF";

typedef struct
{
    SElemType data[MAX_SIZE];
    int top;
}STACK;

int InitStack(STACK *s);              //初始化栈
int Push(STACK *s, SElemType x);       //压栈
int EmptyStack(STACK *s);                //判断栈是否为空
int Pop(STACK *s, SElemType *x);      //出栈
void DecToOthers(int n, int hex);        //进制转换

int main(void)
{
    int n, hex;

    scanf("%d %d",&n,&hex);
    DecToOthers(n,hex);

    return 0;
}

/*
 *  * Function: ========初始化栈========
 *   * 形参:传入栈的指针,若成功初始化,则此指针指向栈
 *    * 返回值:0:初始化失败 1:初始化成功
 *     */
int InitStack(STACK *s)
{
    /*
    s=(STACK *)malloc(sizeof(STACK));
    if (!s)
    {
        printf("分配空间失败!\n");
        return 0;
    }
    */
    memset(s->data, 0, sizeof(s->data));
    s->top=-1;        //赋值为-1
    return 1;
}

/*
 *  * Function:======将数据压入栈中======
 *   * 形参: S:指向栈的指针  x: 待压入的数据
 *    * 返回值:0:栈满,无法压入  1:压入成功
 *     */
int Push(STACK *s, SElemType x)
{
    if (s->top==MAX_SIZE-1)
    {
        printf("\nStack is full!\n");
        return 0;
    }
    s->top++;
    s->data[s->top]=x;

    return 1;
}

/*
 *  * Function:=======判断栈空=======
 *   * 形参:s: 指向栈的指针
 *    * 返回值:1:栈为空   0:栈不为空
 *     */
int EmptyStack(STACK *s)
{
    return ((s->top==-1)?1:0);
}

/*
 *  * Function: ========出栈========
 *   * 形参:s: 指向栈的指针   x: 指向栈顶元素
 *    * 返回值:0:栈为空   1:出栈成功
 *     */
int Pop(STACK *s, SElemType *x)
{
    if (EmptyStack(s))
    {
        printf("\nStack is free!\n");
        return 0;
    }
    *x=s->data[s->top];
    s->top--;

    return 1;
}

/*
 *  * Functiom: ======进制转换======
 *   * 形参:n:十进制数   hex: 进制
 *    */
void DecToOthers(int n, int hex)
{
    int x;
    STACK st;
    InitStack(&st);

    while (n)
    {
        Push(&st, n%hex);
        n/=hex;
    }
    while (!EmptyStack(&st))
    {
        Pop(&st, &x);
        printf("%c",LIST[x]);
    }
    putchar(10);
}
问题主要是在InitStack函数里,st变量是结构体变量,而不是结构体指针,所以不需要在堆上申请空间。如果定义的st是指针,则需要如下操作;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <malloc.h>

#define MAX_SIZE 100

typedef int SElemType;
char LIST[]="0123456789ABCDEF";

typedef struct
{
    SElemType data[MAX_SIZE];
    int top;
}STACK;

int InitStack(STACK **s);              //初始化栈
int Push(STACK *s, SElemType x);       //压栈
int EmptyStack(STACK *s);                //判断栈是否为空
int Pop(STACK *s, SElemType *x);      //出栈
void DecToOthers(int n, int hex);        //进制转换

int main(void)
{
    int n, hex;

    scanf("%d %d",&n,&hex);
    DecToOthers(n,hex);

    return 0;
}

/*
 *  * Function: ========初始化栈========
 *   * 形参:传入栈的指针,若成功初始化,则此指针指向栈
 *    * 返回值:0:初始化失败 1:初始化成功
 *     */
int InitStack(STACK **s)
{
    *s=(STACK *)malloc(sizeof(STACK));
    if (!(*s))
    {
        printf("分配空间失败!\n");
        return 0;
    }
    memset((*s)->data, 0, sizeof((*s)->data));
    (*s)->top=-1;        //赋值为-1
    return 1;
}

/*
 *  * Function:======将数据压入栈中======
 *   * 形参: S:指向栈的指针  x: 待压入的数据
 *    * 返回值:0:栈满,无法压入  1:压入成功
 *     */
int Push(STACK *s, SElemType x)
{
    if (s->top==MAX_SIZE-1)
    {
        printf("\nStack is full!\n");
        return 0;
    }
    s->top++;
    s->data[s->top]=x;

    return 1;
}

/*
 *  * Function:=======判断栈空=======
 *   * 形参:s: 指向栈的指针
 *    * 返回值:1:栈为空   0:栈不为空
 *     */
int EmptyStack(STACK *s)
{
    return ((s->top==-1)?1:0);
}

/*
 *  * Function: ========出栈========
 *   * 形参:s: 指向栈的指针   x: 指向栈顶元素
 *    * 返回值:0:栈为空   1:出栈成功
 *     */
int Pop(STACK *s, SElemType *x)
{
    if (EmptyStack(s))
    {
        printf("\nStack is free!\n");
        return 0;
    }
    *x=s->data[s->top];
    s->top--;

    return 1;
}

/*
 *  * Functiom: ======进制转换======
 *   * 形参:n:十进制数   hex: 进制
 *    */
void DecToOthers(int n, int hex)
{
    int x;
    STACK *st;
    InitStack(&st);

    while (n)
    {
        Push(st, n%hex);
        n/=hex;
    }
    while (!EmptyStack(st))
    {
        Pop(st, &x);
        printf("%c",LIST[x]);
    }
    putchar(10);
}
这种情况下,建议再加上一个栈的销毁函数,用于free堆上的空间。
qwqwqw408 2017-06-16
  • 打赏
  • 举报
回复
InitStack里面为什么要调用malloc?都已经传指针进去了,你还分配内存干啥,
赵4老师 2017-06-16
  • 打赏
  • 举报
回复
http://edu.csdn.net/course/detail/2344 C语言指针与汇编内存地址-一.代码要素

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧