When implementing a preorder traversal of a binary tree using a non-recursive approach, I wrote my own stack. An error occurred during runtime.
When I try to debug and find the problem, it runs normally again. Why is that?
When I copied the contents from .h and .c files into the main program, the problem also disappeared. Why is that?
The development environment is CLion 2024.2+MinGw
CMakeLists.txt
cmake_minimum_required(VERSION 3.29)
project(CLionProjectC11 C)
set(CMAKE_C_STANDARD 11)
add_executable(BT BT.c Stack.c)
add_executable(BT2 BT2.c)
stack.h
#ifndef STACK_H
#define STACK_H
#include <stdbool.h>
#ifndef STACK_DATA_T
#define STACK_DATA_T int
#endif // STACK_DATA_T
typedef struct StackNode {
STACK_DATA_T data;
struct StackNode *next;
} Stack, *StackNode;
bool initStack(StackNode stack);
bool isEmptyStack(StackNode stack);
bool pushStack(StackNode stack, STACK_DATA_T data);
STACK_DATA_T popStack(StackNode stack);
#endif //STACK_H
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
bool initStack(StackNode stack) {
stack->data = 0;
stack->next = NULL;
return true;
}
bool isEmptyStack(StackNode stack) {
return stack->next == NULL;
}
bool pushStack(StackNode stack, STACK_DATA_T data) {
StackNode node = malloc(sizeof(Stack));
if (node == NULL) return false;
node->data = data;
node->next = stack->next;
stack->next = node;
return true;
}
STACK_DATA_T popStack(StackNode stack) {
if (isEmptyStack(stack)) return false;
StackNode node = stack->next;
stack->next = stack->next->next;
STACK_DATA_T data = node->data;
free(node);
return data;
}
BT.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct TreeNode {
char element;
struct TreeNode *left;
struct TreeNode *right;
} Tree, *TreeNode;
#define STACK_DATA_T TreeNode
#include "Stack.h"
void preOrder(TreeNode root) {
Stack head;
initStack(&head);
while (root || !isEmptyStack(&head)) {
while (root) {
printf("%c ", root->element);
pushStack(&head, root);
root = root->left;
}
root = popStack(&head);
root = root->right;
}
}
int main() {
TreeNode a = malloc(sizeof(Tree));
TreeNode b = malloc(sizeof(Tree));
TreeNode c = malloc(sizeof(Tree));
a->element = 'A';
b->element = 'B';
c->element = 'C';
a->left = b;
a->right = c;
b->left = b->right = c->left = c->right = NULL;
preOrder(a);
printf("n");
free(a);
free(b);
free(c);
}
BT2.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct TreeNode {
char element;
struct TreeNode *left;
struct TreeNode *right;
} Tree, *TreeNode;
#define STACK_DATA_T TreeNode
typedef struct StackNode {
STACK_DATA_T data;
struct StackNode *next;
} Stack, *StackNode;
bool initStack(StackNode stack) {
stack->data = 0;
stack->next = NULL;
return true;
}
bool isEmptyStack(StackNode stack) {
return stack->next == NULL;
}
bool pushStack(StackNode stack, STACK_DATA_T data) {
StackNode node = malloc(sizeof(Stack));
if (node == NULL) return false;
node->data = data;
node->next = stack->next;
stack->next = node;
return true;
}
STACK_DATA_T popStack(StackNode stack) {
if (isEmptyStack(stack)) return false;
StackNode node = stack->next;
stack->next = stack->next->next;
STACK_DATA_T data = node->data;
free(node);
return data;
}
void preOrder(TreeNode root) {
Stack head;
initStack(&head);
while (root || !isEmptyStack(&head)) {
while (root) {
printf("%c ", root->element);
pushStack(&head, root);
root = root->left;
}
root = popStack(&head);
root = root->right;
}
}
int main() {
TreeNode a = malloc(sizeof(Tree));
TreeNode b = malloc(sizeof(Tree));
TreeNode c = malloc(sizeof(Tree));
a->element = 'A';
b->element = 'B';
c->element = 'C';
a->left = b;
a->right = c;
b->left = b->right = c->left = c->right = NULL;
preOrder(a);
printf("n");
free(a);
free(b);
free(c);
}
I tried to discover the problem through debugging, but it ran correctly during debugging. I also copied the. h and. c files to the main program, and it ran normally. Why is this?
ShiZiMiao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.