一道简单的栈数制转换问题(10-d),很不明白指针哪里出问题了。
/*****************************************/
/**** stack.h ****/
/*****************************************/
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0);
# endif
typedef int datatype;
typedef struct node{
datatype data;
struct node* next;
} stacknode,* slink;
void Clearstack(slink top)
{
top=NULL;
}
int Emptystack(slink top)
{
if(top == NULL)
{
return (1);
}
else
{
return (0);
}
}
//ERROR
void Push(slink top,datatype x)
{
slink p;
if(Emptystack(top))
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
free(p);
}
else
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
}
}
datatype Pop(slink top)
{
datatype x;
slink p;
if(Emptystack(top))
{
return NULL;
}
else
{
x=top-> data;
p=top;
top=top-> next;
free(p);
return (x);
}
}
/***********************************************/
/**** ERROR! ****/
/**** stack.h指针方面有问题! ****/
/***********************************************/
# include "stack.h "
# include <iostream.h>
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0) ;
# endif
void Conver10_d(int N,int d)
{
cout < < ": " < <endl;
int x=N;
if(N <0)
{
cout < < "... " < <endl;
x=-x;
}
//Clearstack(top);
slink top;
top=(slink)malloc(sizeof(stacknode));
top-> next=NULL;
top-> data=x%d;
x=x/d;
while(x)
{
Push(top,x%d);
x=x/d;
}
if(N <0)
{
cout < < "- ";
}
while(!Emptystack(top))
{
x=Pop(top);
cout < <x;
}
}
void main()
{
Conver10_d(-201,8);
}
链接时:
--------------------Configuration: Conver10_d - Win32 Debug--------------------
Compiling...
Conver10_d.cpp
Linking...
Conver10_d.exe - 0 error(s), 0 warning(s)
运行时:
Microsoft Visual C++ Debug library
Debug Assertion Failed!
Program:c:\Conver10_d.exe
File:dbgheap.c
Line:1017
Expression:_BLOCK_TYPE_IS_VALId(pHead-> nBlockUse)
For information on how your program can cause an assertion
failure,see the Visual C++ documentation on asserts.
调试时:
-> _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse));
我才学,什么都不懂,还望高手能给予指点迷津。
都两天了,我还是找不到问题出在哪里,恳求高手解答。
[解决办法]
错误只有一个:函数Push(slink top,datatype x)以及函数Pop(slink top)参数错误!
由于你在Push()以及Pop()中所做的操作,均使参数top的值改变了,而你希望的是:执行完Push()或Pop()后,main函数中的top是执行完Push()或Pop()后的top!这就是说,要使top的改变能够在main函数的操作中体现,因此,参数应该是引用,而非传值!
因此:函数Push(slink top,datatype x)以及函数Pop(slink top)参数中的“slink top”应该全改为“slink &op”!
改正后的代码如下:
/*****************************************/
/**** stack.h ****/
/*****************************************/
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0);
# endif
typedef int datatype;
typedef struct node{
datatype data;
struct node* next;
} stacknode,* slink;
void Clearstack(slink top)
{
top=NULL;
}
int Emptystack(slink top)
{
if(top == NULL)
{
return (1);
}
else
{
return (0);
}
}
//ERROR
void Push(slink &top,datatype x)
{
slink p;
if(Emptystack(top))
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
free(p);
}
else
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
}
}
datatype Pop(slink &top)
{
datatype x;
slink p;
if(Emptystack(top))
{
return NULL;
}
else
{
x=top-> data;
p=top;
top=top-> next;
free(p);
return (x);
}
}
/*****************************************/
/**** stack.h ****/
/*****************************************/
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0);
# endif
typedef int datatype;
typedef struct node{
datatype data;
struct node* next;
} stacknode,* slink;
void Clearstack(slink top)
{
top=NULL;
}
int Emptystack(slink top)
{
if(top == NULL)
{
return (1);
}
else
{
return (0);
}
}
//ERROR
void Push(slink &top,datatype x)
{
slink p;
if(Emptystack(top))
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
free(p);
}
else
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
}
}
datatype Pop(slink &top)
{
datatype x;
slink p;
if(Emptystack(top))
{
return NULL;
}
else
{
x=top-> data;
p=top;
top=top-> next;
free(p);
return (x);
}
}
以上代码在VC6.0编译通过。