我使用这个功能在树上程序上的代码块它显示在我在哪里释放树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不要混淆有关的其他功能
请不要投入malloc。如果您在使用malloc而没有强制转换时收到警告,请向我们提问。否则,使用C编译器来编译C代码,而不是C++编译器。 – Sebivor 2013-03-23 06:22:11
此外,屏蔽typedefs后面的指针会导致其他人阅读的代码非常混乱。当你看到'int'时,你期望'int *'?不可以。为什么不编写代码来与其他C编程语言保持一致? – Sebivor 2013-03-23 06:24:38
我可以看到,这个问题还没有任何答案......让我们知道你什么时候让它看起来好像你希望我们读代码,而不是继续下一个问题,因为它是不可读*。 – Sebivor 2013-03-23 07:09:41