亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Debug Assertion Failed?為什么

Debug Assertion Failed?為什么

繁花不似錦 2022-08-04 15:11:39
#include<stdio.h>#include<stdlib.h>#define NULL 0#define OK 1#define ERROR 0#define OVERFLOW 0typedef int Status;typedef struct BiTNode{char data; /*數據域*/struct BiTNode *lchild, *rchild; /*左右指針域*/}BiTNode, *BiTree;typedef struct node{BiTree data ;struct node *next ;}StackNode ;void Init_LS(StackNode *top)//置空棧{ top = NULL; }int Empty_LS(StackNode *top ) //判斷??調 return (top==NULL); }StackNode *Push_LS(StackNode *top , BiTree x){ StackNode *p = (StackNode*) malloc(sizeof(StackNode));p->data=x;p->next=top; //建立連接top=p ; //置棧頂return top;}Status Pop_LS(StackNode *top , BiTree &y)//出棧{StackNode *q; //指向將被釋放的結點if (Empty_LS(top)){printf ("Stack Underflow.") ; /* 下溢 */return OVERFLOW;}else{y=top->data ;q=top ;top=top->next ;free (q) ;return OK;}}BiTree CreateBiTree(BiTree &T) {// 按先序次序輸入二叉樹中結點的值(一個字符),空格字符表示空樹,// 構造二叉鏈表表示的二叉樹T。char ch;scanf("%c",&ch);if (ch=='.')T = NULL;else {if (!(T = (BiTNode *)malloc(sizeof(BiTNode))))return ERROR;T->data = ch; // 生成根結點CreateBiTree(T->lchild); // 構造左子樹CreateBiTree(T->rchild); // 構造右子樹}return T;} // CreateBiTreeStatus PrintElement( char e ) { // 輸出元素e的值printf( "%c",e); // 實用時,加上格式串return OK;}Status InOrderTraverse(BiTree T, Status (*Visit)(char)) { //中序遍歷// 采用二叉鏈表存儲結構,Visit是對數據元素操作的應用函數。// 中序遍歷二叉樹T的非遞歸算法,對每個數據元素調用函數Visit。StackNode *S;BiTree p;Init_LS(S); p = T;while (p || !Empty_LS(S)) { //P或stack不為空if (p) {S=Push_LS(S, p);p = p->lchild;} // 非空指針進棧,繼續左進else { // 上層指針退棧,訪問其所指結點,再向右進Pop_LS(S, p); //彈棧,將p指向被彈結點if (!Visit(p->data)) //所訪問結點不存在return ERROR;p = p->rchild;}}return OK;} // InOrderTraversemain(){BiTree L;L=CreateBiTree(L);printf("中序遍歷\n");InOrderTraverse(L,PrintElement);printf("\n");}輸入abd.g...ce...應該輸出dgbaec但是實際上只輸出dg就報錯有沒有類似的情況是不是free掉(StackNode *)類型變量由于其包含BiTree的指針,它所指向的空間也會free掉,反正有點糾結。懂得人幫忙下吧
查看完整描述

1 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

如你所想,問題出在free上
本質是入棧,出棧的函數太復雜,適應性不強,容易出錯
比如S=Push_LS(S, p);這句沒必要,直接Push_LS(S, p);
可以用下面pop,push函數替換
int push(linkstack *s,ElemType *x)//入棧操作,將x的數據元素插入棧s中,使x成為新的棧頂元素
{
linkstack *p,*q;
q=s;
p=(linkstack *)malloc(sizeof(linkstack));
if(!p)exit(-1);
p->stack=*x;
p->next=NULL;
while(q->next)
q=q->next;
q->next=p;
return 1;
}
/////////////////////////////////////////////////
//因為非遞歸遍歷算法 調用的棧不需要 全部出棧
//進棧的鏈表實現的 真正算法為
// q->stack=*x;
// q->next=S->next;
// S->next=q;
////////////////////////////////////////////////

int pop(linkstack *s,ElemType *e)//出棧操作,先將棧s的棧頂結點的值送到e所指向的內存單元,然后刪除棧頂結點
{
linkstack *p,*q;
p=s;
if(s->next==NULL)return 0;
while(p->next)
{
q=p;
p=p->next;
}
q->next=NULL;
*e=p->stack;
free(p);
return 1;
}


查看完整回答
反對 回復 2022-08-08
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號