2013-03-23 49 views
1

我使用这个功能在树上程序上的代码块它显示在我在哪里释放树node.Segmentation故障流行功能有段错误就像是计划接收信号SIGSEGV分割,我理解这个错误出来,由于isEmptyStack()没有得到回报曾经1(仅适用于0)value.It似乎有在流行音乐功能的错误,我需要在这方面的帮助,我在这里坚持从很多天plz帮助我出。分割过错树在C语言

//堆栈实现了树的节点类型

typedef struct TreeStructure 
{ 
    int data; 
    struct TreeStructure *left; 
    struct TreeStructure *right; 
}Tree; 

typedef struct SListNode 
{ 
    struct TreeStructure *data; 
    struct ListNode *next; 
}SList; 

typedef struct StackList 
{ 
    struct ListNode *Node; 
}Stack; 

Stack *CreationStack() 
{ 
    return NULL; 
} 

int isEmptyStack(Stack *top) 
{ 
    return top==NULL; 
} 
void Push(Stack **top,Tree *data) 
{ 
    SList *new,*tmp; 
    new=malloc(sizeof *new); // Modification here according to comments 
    new->data=data; 
    new->next=*top; 
     *top=new; 
} 

Tree *Pop(Stack **top) 
{ 
    Tree *data; 
    SList *tmp; 
    if(isEmptyStack(*top)) 
    { 
     printf("Underflow") ;return NULL; 
    } 
    else 
    { 
     tmp=*top; 
     *top=tmp->next; 
     data=tmp->data; 
     if(tmp)   // using do not let occur case of the dangling pointer 
      free(tmp);  // Showing fault here only on Debugging 
     return data; 
    } 
} 

这是为了保留一平次序树....从左到右,自下而上的顺序打印,

#include<stdlib.h> 
typedef struct TreeStructure 
{ 
    int data; 
    struct TreeStructure *left; 
    struct TreeStructure *right; 
}Tree; 
typedef struct ListQueue 
{ 
    struct ListNode *Rear; 
    struct ListNode *Front; 
}Queue; 

typedef struct ListNode 
{ 
    struct TreeStructure *node; 
    struct Listnode *next; 
}List; 

typedef struct SListNode 
{ 
    struct TreeStructure *data; 
    struct ListNode *next; 

}SList; 

typedef struct StackList 
{ 
    struct ListNode *Node; 
}Stack; 

void Reverseorder(Tree *Root) 
{ 
    Stack *top; Queue *Q; 
    Tree *tmp; 
    if(!Root) 
     return ; 
    top=CreationStack(); 
    Q=Creation(); 
    Enqueue(Q,Root); 

    while(!isEmpty(Q)) 
    { 

     tmp=Dequeue(Q); 
     Push(&top,tmp); 
     if(tmp->right) 
      Enqueue(Q,tmp->right); 
     if(tmp->left) 
      Enqueue(Q,tmp->left); 
    } 


    while(!isEmptyStack(top))  // Here Empty checker is going into infinite loop 
            // due to this error occurs 
     printf("\nReverse Element is %d",Pop(&top)->data); 

} 

由于我已经检查等功能工作的权利,每当我试着开始来扩大我的代码有点多,从那里的问题,PLZ不要混淆有关的其他功能

+0

请不要投入malloc。如果您在使用malloc而没有强制转换时收到警告,请向我们提问。否则,使用C编译器来编译C代码,而不是C++编译器。 – Sebivor 2013-03-23 06:22:11

+2

此外,屏蔽typedefs后面的指针会导致其他人阅读的代码非常混乱。当你看到'int'时,你期望'int *'?不可以。为什么不编写代码来与其他C编程语言保持一致? – Sebivor 2013-03-23 06:24:38

+0

我可以看到,这个问题还没有任何答案......让我们知道你什么时候让它看起来好像你希望我们读代码,而不是继续下一个问题,因为它是不可读*。 – Sebivor 2013-03-23 07:09:41

回答

0

好像data是pop函数中的一个悬挂指针。当你释放tmp时,你还可以释放数据指向的TreeStructure。

+1

Sir bcz程序数据是Tree Node,这就是为什么我要返回Tree类型数据而不是删除它。 – Atiq 2013-03-23 07:44:21

1

请在这里张贴前仔细检查自己的代码。这是我第一眼看到的东西,其中最有可能是其他东西,因为你根本没有足够的注意力来让事情正确。

你的功能Push

  • 有一个未使用的变量tmp
  • 一个假的来malloc呼叫
  • 使用typedef版指针
  • 区分两种案件,但其随后是完全等价的
+1

我发现你的回复很有用,但仍然无法正常工作......你能帮我进一步吗?我很感激你的回复 – Atiq 2013-03-23 07:49:21