TC 段错误

Terriblee 2011-09-27 10:05:37
我用TC写的一个程序,没有错误也没有警告,当时交作业的时候系统提示有 段错误 。 我找不到错在哪里 ,希望各路大神搭救一下,谢谢啦!程序是比较两棵二叉树是否一样。

源代码如下
#include<stdio.h>
#include<stdlib.h>

struct tree{
struct tree *left ;
int initial; /*the number of initial sequence */
int test; /*the number of sequence needing to be tested*/
struct tree *right ;
} ;
struct tree *root;
struct tree *Insert(int tmp, struct tree *T ); /*insert tmp to T->initial and return T*/
struct tree *Judge(int tmp, struct tree *T ); /*insert tmp to T->test and judge */
struct tree *Emptytest(struct tree *T); /*make the empty in order to read the next sequence*/

int result=0 , pri[10] , flag=1;
int main()
{
int nmb , line , i , j , p ,tmp;

root=NULL;
for(i=0;i<10;i++)
pri[i]=0;
while(1){
fflush(stdin);
scanf( "%d" , &nmb ) ; /* read in the total number of integers */
if(nmb==0)
break;
scanf( "%d" , &line ) ;
fflush(stdin);
for( i=0 ; i<nmb ; i++ ) /*read in the initial integers and insert them to root*/
{
scanf( "%d", &tmp ) ;
root=Insert( tmp , root ) ;
}
for( j=0 ; j<line ; j++ ) /* read in the sequences to be tested*/
{ fflush(stdin);
for( i=0 ; i<nmb ; i++ )
{
scanf( "%d", &tmp ) ;
root=Judge( tmp , root ) ; /* every time read in a integer then we test it */
if(flag==0) /* the two trees are different then flag will be made equal to 0 */
break;
}
if(i==nmb) /* the for loop above come to an end without break , the trees are the same */
{
pri[result++]=1; /* we need to print one YES when the program finishes */
for(p=0 ; p<nmb && root->test!=0 ; p++) /* finish testing a sequence and we make every Node->test empty so we can read in the next sequence */
root=Emptytest(root);
}
else {
pri[result++]=0; /* we need to print one NO when the program finishes */
for(p=0;p<nmb&&root->test!=0;p++) /* finish testing a sequence and we make every Node->test empty so we can read in the next sequence */
root=Emptytest(root);
flag=1; /* initialize the flag before a following judge */
}
}
}

for(i=0;i<result-1;i++) /* print out the result */
{
if(pri[i]==1)
printf("YES\n");
else printf("NO\n");
}
if(pri[i]==1)
printf("YES");
else printf("NO");
return 0 ;
}

struct tree * Insert(int tmp, struct tree *T ) /* insert tmp to a binary tree T */
{
if( T==NULL )
{
T=(struct tree *)malloc(sizeof(struct tree));
if( T==NULL )
{
printf("Error:Out of space!");
exit(0);
}
else /* create a new node */
{
T->initial = tmp ;
T->test=0;
T->left=T->right = NULL ;
}
}
else
if( tmp < T->initial ) /* we always attach the smaller integer to the left end */
T->left = Insert( tmp , T->left ) ;
else
if( tmp > T->initial ) /* we always attach the larger integer to the right end */
T->right = Insert( tmp , T->right ) ;
return T ;
}

struct tree * Judge(int tmp, struct tree *T )
{

if(T->test==0) /* T->test is empty and a integer can be placed here */
{
T->test=tmp;
if((T->test)!=(T->initial)) /* two trees are different at this place */
{
flag=0;
return T ;
}
}

else
if( tmp < T->initial ) /* we insert tmp and compare tmp with T->initial just like buiding a binary tree */
{
if(T->left==NULL)
{
flag=0;
return T ;
}

else T->left = Judge( tmp , T->left ) ;
}
else
if( tmp > T->initial )
{
if(T->right==NULL)
{
flag=0;
return T ;
}
else T->right = Judge( tmp , T->right ) ;
}
return T ;
}

struct tree *Emptytest(struct tree *T)
{
if(T->left->test!=0 && T->left!=NULL) /* node T has a left child */
T->left=Emptytest(T->left); /* before we empty T ,we meke its child empty recursively */
else if(T->right->test!=0 && T->right!=NULL) /* node T has a right child */
T->right=Emptytest(T->right);
else T->test=0 ; /* make T empty if it has no child */
return T ;

}
...全文
70 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jason-xu 2011-09-27
  • 打赏
  • 举报
回复
一般是边界没考虑,你的代码格式都不弄一下,这么乱实在不想看呀,自己单步调一下
quwei197874 2011-09-27
  • 打赏
  • 举报
回复
内存乱了,设断点调试吧
Terriblee 2011-09-27
  • 打赏
  • 举报
回复
我的同学帮我找到了,我正在学习修改过后的,谢谢各位。
Terriblee 2011-09-27
  • 打赏
  • 举报
回复
谢谢大家,我是第一次发帖,所以程序乱了点,有人能教我怎么弄可以让你们看的清楚吗?另外我是个菜鸟。所以只会用TC.
赵4老师 2011-09-27
  • 打赏
  • 举报
回复
用TD调试,看Stack窗口里面的函数调用历史

VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习汇编以及C和汇编的对应关系。
从汇编的角度理解和学习C语言的指针,原本看似复杂的东西就会变得非常简单!
指针即地址。“地址又是啥?”“只能从汇编语言和计算机组成原理的角度去解释了。”

提醒:
“学习用汇编语言写程序”

“VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习C和汇编的对应关系。”
不是一回事!
Qlaiaqu 2011-09-27
  • 打赏
  • 举报
回复
你们求求你们那些老古董老师放过你们用vs2010吧
薛定谔之死猫 2011-09-27
  • 打赏
  • 举报
回复
我了个去,还在玩TC,看到就头疼啊

69,369

社区成员

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

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