大侠们再来解释下VC++6.0编译过不去的原因
- C/C++ code
#include <stdio.h>
#include <stdlib.h>
typedef char type;
#define num 10
#define NULL 0
typedef struct node
{
type date;
struct node *right,*left;
}node;
typedef struct shed
{
node *p;
int top;
int size;
}shed;
int empty(shed *s)
{
if(s->top==0)
return 0;
return 1;
}
newshed(shed *s)/////////////////初始化栈
{
s->p=(node *)malloc(num*sizeof(node *));
s->top=0;
s->size=num;
}
int push(shed *s,node *p)//////////////////////压栈
{
if(s->top==s->size)
{
s->p=(node *)realloc(s->p,s->size*sizeof(node *));
if(!s->p)
return 0;
s->size++;
}
s->p[s->top]=p;///////////////这里错误
s->top++;
}
node *pop(shed *s)/////////////////弹栈
{
node *e;
if(s->top==0)
return 0;
e=s->p[s->top-1];
s->top--;
return e;
}
int gettop(shed *s)///////////////////////取栈顶元素
{
int e;
if(s->top==0)
return 0;
e=s->p[s->top-1].date;////////////////这里错误
return e;
}
node *buildtree()/////////////////树的建立
{
node *p;
type x;
scanf("%c",&x);
getchar();
if(x=='?')
p=NULL;
else
{
p=(node *)malloc(sizeof(node));
p->date=x;
p->left=buildtree();
p->right=buildtree();
}
return p;
}
int search(node *T,type x)/////////////////搜索
{
node *p=T,*q=NULL,*s=T;
int l=0,t;
while(p||!empty(s))
{
if(p!=q)
while(p)
{
push(s,p);
if(p->left)
p=p->left;
else p=p->right;
}
q=gettop(s);
if(q->date==x)
break;
if(q->right==p)
{
p=pop(s);
printf("%d",q->date);
}
else p=q->right;
}
while(!empyt(s))
{
t=pop(s);
l++;
}
return l;
}
main()
{
node *s;
shed p;
type t;
type z;
newshed(&p);
s=buildtree();
printf("input a No. to search\n");
scanf("%c",&t);
z=search(s,t);
}
我这次是用VC++6.0创建的C程序
2个编译过不去上面有
d:\luanma\g.c(5) : warning C4005: 'NULL' : macro redefinition
d:\vc98\include\stdio.h(212) : see previous definition of 'NULL'
d:\luanma\g.c(41) : error C2115: '=' : incompatible types
d:\luanma\g.c(49) : error C2115: '=' : incompatible types
d:\luanma\g.c(84) : warning C4133: 'function' : incompatible types - from 'struct node *' to 'struct shed *'
d:\luanma\g.c(89) : warning C4133: 'function' : incompatible types - from 'struct node *' to 'struct shed *'
d:\luanma\g.c(94) : warning C4133: 'function' : incompatible types - from 'struct node *' to 'struct shed *'
d:\luanma\g.c(94) : warning C4047: '=' : 'struct node *' differs in levels of indirection from 'int '
d:\luanma\g.c(100) : warning C4133: 'function' : incompatible types - from 'struct node *' to 'struct shed *'
d:\luanma\g.c(106) : warning C4013: 'empyt' undefined; assuming extern returning int
d:\luanma\g.c(108) : warning C4133: 'function' : incompatible types - from 'struct node *' to 'struct shed *'
d:\luanma\g.c(108) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
执行 cl.exe 时出错.
g.exe - 1 error(s), 0 warning(s)
[解决办法]
s->p[s->top]=p;///////////////这里错误
s->p是一个指针,不是一个指针数组
typedef struct shed
{
node **p;
int top;
int size;
}shed;
可解决
[解决办法]
#include<stdio.h>
#include<stdlib.h>
typedef char type;
#define num 10
//#define NULL 0
typedef struct node
{
type date;
struct node *right,*left;
}node;
typedef struct shed
{
node **p; //这里改掉!
int top;
int size;
}shed;
int empty(shed *s)
{
if(s->top==0)
return 0;
return 1;
}
void newshed(shed *s)/////////////////初始化栈
{
s->p=(node **)malloc(num*sizeof(node *));//定义节点栈
s->top=0; //节点数量初始 0
s->size=num; //节点栈初始大小num
}
int push(shed *s,node *p)//////////////////////压栈
{
if(s->top==s->size) //空间是否满
{
s->p=(node **)realloc(s->p,s->size*sizeof(node *)); //栈扩大1倍
if(!s->p)
return 0;
s->size++;//这里错误! 改为 s->size=s->size *2 !
}
s->p[s->top]=p;///////////////这里错误 改
s->top++;
}
node *pop(shed *s)/////////////////弹栈
{
node *e;
if(s->top==0)
return 0;
e=s->p[s->top-1];
s->top--;
return e;
}
node *gettop(shed *s)///////////////////////取栈顶元素
{
node *e;
if(s->top==0)
return 0;
e=s->p[s->top-1];////////////////这里错误 ?栈顶当然是 p[0]
return e;
}
node *buildtree()/////////////////树的建立
{
node *p;
type x;
scanf("%c",&x);
getchar();
if(x=='?')
p=NULL;
else
{
p=(node *)malloc(sizeof(node));
p->date=x;
p->left=buildtree();
p->right=buildtree();
}
return p;
}
int search(node *T,shed *s,type x)/////////////////搜索 在哪里找?可以在2叉树直接找,
{
node *p=T,*q=NULL;
int l=0,t;
while(p||!empty(s))//
{
if(p!=q)
while(p)
{
push(s,p);
if(p->left)
p=p->left;
else p=p->right;
}
q=gettop(s);
if(q->date==x)
break;
if(q->right==p)
{
p=pop(s);
printf("%d",q->date);
}
else p=q->right;
}
while(!empty(s))
{
pop(s);
l++;
}
return l;
}
main()
{
node *s;// 定义一个节点指针
shed p; //定义栈
type t; //键盘赋值变量
type z; // 查询的结果变量
newshed(&p); //初始化栈, 1个num 大小的节点空间
s=buildtree(); //递归生成2叉树
printf("input a No. to search\n");//提示
scanf("%c",&t);//取值
z=search(s,&p,t); //压 栈 查找
}
/////////////////////////
编译能通过了,不过 搂主, 不客气地说,不知道你在干什么!特别那个search 函数!