Hi all. I am having a problem creating a stack for defined structures. I've created stacks in C before, but, until now, used them only for predefined data types like char. I want a stack for binary tree nodes, some of which possibly link to each other (i.e., the son of a node might be in the stack as well).
First off, some structures, and initializing functions
Code:
typedef char BTData;
typedef struct BinaryTreeNode BTNode;
typedef struct LStack stack;
typedef struct StackNode stacknode;
typedef BTNode stackelement;
/*Binary Tree Structure*/
struct BinaryTreeNode{
BTNode *LeftSon;
BTData data;
BTNode *RightSon;
};
void InitBTNode(BTNode *bt, BTData dat){
bt->data = dat;
bt->LeftSon = NULL;
bt->RightSon = NULL;
}
/*Linked Stack Implementation*/
struct StackNode{
stackelement *data;
stacknode *link;
};
struct LStack{
stacknode *top;
};
void InitStack(stack *s){
s->top = NULL;
}
Now, painfully inserting printf's in between function lines, I have determined that my push is the one doing the illegal operation (pardon me if I haven't removed the printf's yet):
Code:
void push(stack *s, stackelement *element){
stacknode *newnode;
printf("Declared new stacknode\n");
newnode = (stacknode *) malloc(sizeof(stacknode));
printf("malloced stacknode\n");
if(newnode = NULL){
printf("newnode is null\n");
StackOverflow();
printf("Called StackOverflow\n");
}
else{
printf("newnode is not null\n");
newnode->data = element; //###THE PROBLEMATIC LINE###, as figured out from printf's
printf("newnode->data = element\n");
newnode->link = s->top;
printf("newnode->link = s->top\n");
s->top = newnode;
printf("s->top = newnode\n");
}
}
This push was called from another function noted here (again, pardon the debugging printf's):
Code:
void parse(char data[], char tag[], BTNode *finishedBT){
stack NodeStack;
printf("Declared Stack\n");
InitStack(&NodeStack);
printf("Initialized Stack\n");
int datalen = strlen(data);
printf("Computed for datalen Stack\n");
int taglen = strlen(tag);
printf("Computed for taglen\n");
if(datalen == taglen && isValid(tag)){
printf("Entered if clause\n");
int i = 0;
printf("Set i to 0\n");
while(i < taglen){
printf("In loop!\n");
BTNode thisNode;
printf("Declared a BTNode\n");
InitBTNode(&thisNode, data[i]);
printf("Initialized the BTNode\n");
if(tag[i] == '0'){
printf("tag[i] is zero\n");
push(&NodeStack, &thisNode); //###PUSH WAS CALLED HERE###
printf("Pushed it to stack\n");
}
else{
printf("tag[i] is not zero\n");
BTNode RNode;
printf("Declared RNode\n");
pop(&NodeStack, &RNode);
printf("Popped something to RNode\n");
BTNode LNode;
printf("Declared LNode\n");
pop(&NodeStack, &LNode);
printf("Popped something to\n");
pointLeftTo(&thisNode, &LNode);
printf("Pointed the left\n");
pointRightTo(&thisNode, &RNode);
printf("Pointed the right\n");
push(&NodeStack, &thisNode);
printf("Pushed created node to stack\n");
}
i++;
printf("Incremented i\n");
}
pop(&NodeStack, finishedBT);
printf("Popped node\n");
}
else{
printf("Entered else clause\n");
ValidTagSeq = FALSE;
printf("Set ValidTagSeq to FALSE\n");
}
}
Called from the main function which is
Code:
main(){
BTNode *aNode = NULL;
aNode = (BTNode *) malloc(sizeof(BTNode));
char algorithm_word[] = "ORLHMITGA";
char algorithm_tag[] = "001001011";
parse(algorithm_word, algorithm_tag, aNode);
}
Now, I've tried variations such as making the arguments of push a stack *s and a stackelement element, the way I do for primitive data types, but they didn't seem to do any difference (I actually tried that approach first).
Any suggestions?
Thanks!