2013-03-23 96 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