我在 Codeblocks 上的树程序中使用此函数,它在 Pop 函数中显示分段错误,我正在释放 Tree 节点。分段错误就像程序接收信号 SIGSEGV 分段,我理解这个错误是由于isEmptyStack()
没有得到返回曾经1(只有0)值。Pop函数似乎有错误,我需要这方面的帮助,我被困在这里很多天了,请帮助我。
// 树型节点的栈实现
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);
}
当我检查其他功能是否正常工作时,每当我尝试进一步增加我的代码时,从那里开始出现问题,请不要混淆其他功能