回 帖 发 新 帖 刷新版面

主题:入栈与出栈出现的错误(图片地址已修改)

这段程序代码功能是输入一个整数,实现将其转换成8进制,前几天我贴上来过,因为很多语法错误的问题,现在我已经都改了。为了测试栈的PUSH和POP操作,我在程序的PUSH和POP函数中加入了一句代码,就是将入栈和出栈的元素e输出到屏幕上,结果程序的运行结果如图,输入1046 将其转换成八进制,结果每次循环 入栈的元素分别为 e=6,e=2,e=0,e=2    出栈的元素就是如图中所示e=2,e=0,e=2,e=6,说明push和pop函数都没错误。可是在最后的结果输出中,按理分别输出2,0,2,6才对  为什么同一个循环中输出的会变成这样  -858993460,  -858993460,    -858993460, -858993460?
我用的是vc++6.0。

[img]http://club.joyes.com/images/upload7/2006/08/18/194955.jpg[/img]


include <stdio.h>                                      
#include <stdlib.h>                                    
                                                       
#include <string.h>                                    
                                                      
                                                      
                                                       
                                                       
#define INIT_SIZE 100                                                      
#define TRUE 1
#define FALES 0
#define OK 1
#define ERROR 0
#define OVERFLLOW 0                                                       
                                                       
                                           
                                                        
typedef int    Status;                                                       
typedef int    SElemType; //栈元素类型                  
typedef struct                                          
{                                                       
    SElemType *base;                                   
    SElemType *top;                                    
    int stacksize;                                      
}Stack;

回复列表 (共9个回复)

沙发

void InitStack (Stack *S)                               
{                                                       
                                                        
    S->base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType));       
    if(!S->base)                                                    
        exit (1);                                                 
    S->top=S->base;                                                    
    S->stacksize=INIT_SIZE;                                            
                                                                    
                                                                
}//InitStack   初始化栈                                                    
                                                                    
                                                                    
Status push(Stack *S,SElemType e)                                        
{                                                                        
    if (S->top-S->base>=S->stacksize)                                                    
        exit(OVERFLLOW);
    else{
        ++S->top;
       *S->top=e;
    [b]printf ("PUSH:e=%d\n",e);    //测试入栈的元素,并输出到屏幕[/b]
    }
    return (OK);                                                    
}//push                                                                    
                                                                    
Status pop(Stack *S,SElemType e)                                        
{                                                                    
                                                                    
    if (S->top==S->base)                                                        
        exit(OVERFLLOW);                                                    
    else{    
        e=*S->top;
        --S->top;
        [b]printf ("POP:e=%d\n",e);    //测试出栈的元素,并输出到屏幕[/b]
        
    }
    return (OK);                                                        
    }//pop        

板凳

Status StackEmpty(Stack *S)                                               
{                                                                     
                                                                        
    if (S->top==S->base)                                                       
        return(OK);                                                  
    else                                                              
        return (ERROR);                                
}//StackEmpty                                                        
                                                                        
Status DestroyStack(Stack *S){  //销毁栈S                                
                                                                       
         free(S->base);                                                 
                                                                      
         S->top=S->base;                                                  
                                                                        
         return (OK);                                                    
                                                                        
}//DestroyStack

3 楼

void conversion_10to8 (int m)                                        
{               //将输入的十进制转为八进制                               
                                                               
    SElemType e;
    
    Stack S;                                        
    InitStack (&S)    ;                                                            
                                                                       
    while (m)                                                        
    {                                                            
        push(&S,m%8);                                                            
        m=m/8;                                                
                                                         
                                                                    
    }//while        
    printf ("The result is:");
    while (!StackEmpty(&S))                                                
    {                                                                
      [b]pop(&S,e);                                                    
      printf ("%d\n",e);[/b]                                            
    }//while                                                            
                                                                        
    DestroyStack(&S);                                                    
}//conversion_10to8                                                        
                                                                                            
                                                                    
                                                                    

4 楼

main ()                                                                
{                                                                    
    int number;                                                            
    printf ("Please input a num:\n");                                    
    scanf ("%d",&number);                                                
    conversion_10to8 (number);    
                 
}//main    

5 楼

入栈的时候top所指的空间里放入e,再top指针+1
出栈的时候top指针先-1,再取出e的值.
top指针指向栈顶元素的下一个空间地址的.

Status Push(SqStack *S,ElemType e)
{
    if(((*S).top - (*S).base)>=(*S).stacksize)
    {
        (*S).base = (ElemType *)realloc((*S).base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(ElemType));
        if (!(*S).base)
        {
            exit(OVERFLOW);
        }
        (*S).top = (*S).base + (*S).stacksize;
        (*S).stacksize = STACK_INIT_SIZE+STACKINCREMENT;    
    }
    [b][color=FF0000]*((*S).top)++ = e;[/color][/b] //也就是[color=0000FF]*(S->top) = e;自S->top = S->top + 1;[/color]    return OK;
}

Status Pop(SqStack *S,ElemType *e)
{
    if (S->base == S->top)
    {
        return ERROR;
    }
    [b][color=FF0000]*e = *(--S->top);[/color][/b] // 也就是[color=0000FF]S->top = S->top-1;*e=*(S->top)[/color]    
    return OK;
}

6 楼

感谢您的回复,  我还是有点摸不着头脑,可是为什么在最后conversion_10to8 中 出栈元素的输出e就变成一串,在POP函数中程序中出栈元素的输出都很正确啊, 会不会是POP函数调用过程中e的值不传递给conversion_10to8  ?

7 楼

void conversion_10to8 (int m)                                        
{               //将输入的十进制转为八进制                               
                                                               
    SElemType e;
    
    Stack S;                                        
    InitStack (&S)    ;                                                            

    while (m)                                                        
    {                                                            
        push(&S,m%8);                                                            
        m=m/8;                                                
                                                                 
    }//while        
    printf ("The result is:");
    while (!StackEmpty(&S))                                                
    {                                                                
      pop(&S,e);      //[b][color=0000FF]写成pop(&S,&e);                 [/color][/b]                             
      printf ("%d\n",e);   //[b][color=FF0000]这里的e,在pop函数里没有修改过来,可以写成pop(&S,&e),pop定义的函数也修改一下参数                            [/color][/b]          
    }//while                                                                                                                             
    DestroyStack(&S);                                                    
}//conversion_10to8   



Status pop(Stack *S,SElemType e)   // [b][color=FF0000]修改成Status pop(Stack *S,SElemType *e)                         [/color][/b]            
{                                                                    
                                                                    
    if (S->top==S->base)                                                        
        exit(OVERFLLOW);                                                    
    else{    
        e=*S->top;// [b][color=FF0000]*e = *(--S->top);[/color][/b]
        --S->top;
        printf ("POP:e=%d\n",e);    //测试出栈的元素,并输出到屏幕
        
    }
    return (OK);                                                        
    }//pop

8 楼

谢谢楼上的老师回复,我明白了,看来我还得好好复习一下指针了。

9 楼

Status pop(Stack *S,SElemType e)
你是想用e来存储出栈的数据吧,这里请用引用,SElemType &e, 用指针也可以 

    while (!StackEmpty(&S))                                                
    {                                                                
      pop(&S,e);                                                    
      printf ("%d\n",e);                                            
    }//while    
不然在这里e是不会记录你出栈的值的,你这里输出的是一个没有赋过值的变量。

其他好像没有什么错误

我来回复

您尚未登录,请登录后再回复。点此登录或注册