/* There is a problem that has not been solved. The single-step debugging finds that the elements that have been normally written into the stack are. Why is the pop-up stack element 1 and printing it on the screen a smiling face? */
#include <iostream>#include "bitree.h"using namespace std;#define STACK_INIT_SIZE 100
Typedef struct bitnode {char data; struct bitnode * lchid, * rchild;} bitnode, * bitree; // Binary Tree struct typedef struct {struct bitnode * base; struct bitnode * top; int stacksize;} sqstack; void initstack (sqstack & S)
{S. base = new bitnode [stack_init_size]; If (! S. Base) {cout <"\ n execution of the sequential storage allocation failed! "; Return;} s. top = S. base; S. stacksize = stack_init_size;} bool push (sqstack & S, bitree e) {/* If (S. top-S.base> = S. stacksize) {S. base = (bitnode *) realloc (S. base, stackincrement); If (! S. base) {cout <"\ n stack is empty and cannot be inserted"; return false;} s. top = S. base + S. stacksize; S. stacksize + = stackincrement;} */s. top ++; S. top = E; return true;} bool POP (sqstack & S, bitree & E) {If (S. top = S. base) {cout <"\ n is used as an empty stack, and no elements in the stack can be deleted! "; Return false;} e = (-- S. top); Return true;} bool isemptystack (sqstack s) {If (S. top = S. base) return true; else return false;} bool gettop (sqstack S, bitree & E) {If (S. top = S. base) {cout <"\ n this stack is empty and cannot take the top element of its stack! "; Return false;} e = S. Top; return true ;}
/* Memory conflicts occur during non-recursive sequential execution. The reason is that P is empty. After a single-step debugging finds a problem, the value of top stack is 1, not equal to the value previously saved */void repreordertraverse (bitree t) // The binary tree {sqstack s; initstack (s); push (S, T ); bitree P = T; while (p |! Isemptystack (s) {If (p) {cout <p-> data; push (S, P); P = p-> lchid;} else {bitree Q; gettop (S, P); POP (S, q); P = p-> rchild ;}}}
// Enter abk # y # A # CF ### ("#" indicates the empty tree)
// One-Step debugging finds that all elements stored in stack s are normal,
// An error occurs after running to Pop. The value of the original Top pointer is null. The single-step debugging result