我使用這個功能在樹上程序上的代碼塊它顯示在我在哪裏釋放樹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