コードブロックのツリー プログラムでこの関数を使用しています。ツリー ノードを解放している Pop 関数にセグメンテーション違反が表示されています。セグメンテーション違反は、プログラム受信信号 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);
}
他の機能が正しく機能していることを確認したので、コードをもう少し拡張しようとすると、そこから問題が発生し始めます。他の機能について混同しないでください