Stack issues about the use of Binary Tree non-Recursive Algorithms

Source: Internet
Author: User
/* 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

 
 
 
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.