执行case6(求根结点到指定结点的路径)时程序停止运行

图片说明
#include
#include
#define OVERFLOW 0
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define Stack_Size 50
typedef int Status;
typedef char ElemType;
typedef struct BiTree
{
ElemType data;
struct BiTree Lchild;
struct BiTree *Rchild;
}BiTree,*Binary_Tree;
typedef struct Node
{
BiTree *data;
struct Node *next;
} LinkQueueNode;
typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
int InitQueue(LinkQueue *Q)
{LinkQueueNode *p;
p=(LinkQueueNode
)malloc(sizeof(LinkQueueNode));
Q->front=p;
Q->rear=p;
(Q->front)->next=NULL;
}
int EnterQueue(LinkQueue Q,Binary_Tree x)
{
LinkQueueNode *NewNode;
NewNode=(LinkQueueNode
)malloc(sizeof(LinkQueueNode));
if(NewNode!=NULL)
{NewNode->data=x;
NewNode->next=NULL;
Q->rear->next=NewNode;
Q->rear=NewNode;
return(TRUE);
}
else return(FALSE);
}
int IsEmpty(LinkQueue *Q)
{if(Q->front==Q->rear)
return(TRUE);
else return(FALSE);
}
int OutQueue(LinkQueue *Q)

{LinkQueueNode *s;

if (IsEmpty(Q))
{exit(0);return 0;}
else
{s=(Q->front)->next;
(Q->front)->next=s->next;
if(s->next==NULL)
Q->rear=Q->front;
free(s);
return 1;}
}
Binary_Tree GetHead(LinkQueue *Q)
{
LinkQueueNode *p;
BiTree *q;
if(IsEmpty(Q))
return q;
else{p=Q->front->next;
return p->data;
}
}
void visit(Binary_Tree T)
{
printf("%c",T->data);
}
Status CreateBiTree(Binary_Tree*T)
{
char ch;
*T=(Binary_Tree)malloc(sizeof(BiTree));
if(!(*T))
exit(OVERFLOW);
do{scanf("%c",&ch);
}while(ch=='\n');
if(ch=='0')
*T=NULL;
else
{
(*T)->data=ch;
CreateBiTree(&((*T)->Lchild));
CreateBiTree(&((*T)->Rchild));
}
return OK;
}
Status PreShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
printf("%c",T->data);
PreShowBiTree(T->Lchild);
PreShowBiTree(T->Rchild);
}
return OK;
}
Status MidShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
MidShowBiTree(T->Lchild);
printf("%c",T->data);
MidShowBiTree(T->Rchild);
}
return OK;
}
Status BehShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
BehShowBiTree(T->Lchild);
BehShowBiTree(T->Rchild);
printf("%c",T->data);
}
return OK;
}
Status LayerOrder(Binary_Tree T)
{LinkQueue Q;
Binary_Tree p;
InitQueue(&Q);
if(T==NULL)
return ERROR;
EnterQueue(&Q,T);
while(!IsEmpty(&Q))
{ p=GetHead(&Q);
OutQueue(&Q);
visit(p);
if(p->Lchild) EnterQueue(&Q,p->Lchild);
if(p->Rchild) EnterQueue(&Q,p->Rchild);
}
return OK;
}
void path(Binary_Tree T,char r)
{BiTree *p,*q;
int i,top=0;
Binary_Tree s[Stack_Size];
q=NULL;
p=T;
while(p!=NULL||top!=0)
{top++;
if(top>=Stack_Size)
exit(0);
s[top]=p;
p=p->Lchild;
}
if(top>0)
{p=s[top];
if(p->Rchild==NULL||p->Rchild==q)
{if(p->data==r)
{for(i=1;i<=top;i++)
printf("%c",s[i]->data);
return;
}
else
{
q=p;
top--;
p=NULL;
}
}
else p=p->Rchild;
}
}
int main()
{ BiTree *T;
int a;
char r;
printf("1.建立二叉树存储结构\n");
printf("2.求二叉树的先序遍历序列\n");
printf("3.求二叉树的中序遍历序列\n");
printf("4.求二叉树的后序遍历序列\n");
printf("5.求二叉树的层序遍历序列\n");
printf("6.求根结点到指定结点的路径\n");
printf("0.退出");
do{
printf("please int a number:");
scanf("%d",&a);
switch(a)
{
case 1:printf("输入二叉树:");
CreateBiTree(&T);
printf("\n");
break;
case 2:
PreShowBiTree(T);
printf("\n");
break;
case 3:
MidShowBiTree(T);
printf("\n");

break;
case 4:
BehShowBiTree(T);
printf("\n");
break;

case 5:LayerOrder(T);
printf("\n");
break;
case 6:printf("输入结点:");
do{scanf("%c",&r);}
while(r=='\n');
path(T,r);
break;
}
}while(a!=0);
printf("exit\n");
}

图片说明

 void path(Binary_Tree T,char r)
{
    BiTree *p,*q;
    int i,top=0;
    Binary_Tree s[Stack_Size];
    q=NULL;
    p=T;
    while(p!=NULL||top!=0)
    {
        top++;
        if(top>=Stack_Size)
            exit(0);
        s[top]=p;
        p=p->Lchild;
    }
    if(top>0)
    {
        p=s[top];
        if(p->Rchild==NULL||p->Rchild==q)
        {
            if(p->data==r)
            {
                for(i=1;i<=top;i++)  //这里的下标应该从0开始,这里数据越界
                    printf("%c",s[i]->data);   
                return;
            }
            else
            {
                q=p;
                top--;
                p=NULL;
            }
        }
        else 
            p=p->Rchild;
    }
}