Having trouble pushing elements onto a stack - c

I'm working on a project where I must take an expression in reverse polish notation, push the integers and operators onto a stack, then pop them out off the stack as they are inserted into a binary search tree.
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
struct snode
{
int datum;
struct snode* bottom;
};
struct tnode
{
int datum;
struct tnode* left;
struct tnode*right;
};
struct snode*
push(struct snode* stack, int x) {
struct snode *S = (struct snode*)malloc(sizeof(struct snode));
S->datum = x;
S->bottom = stack;
return S;
}
struct snode* pop(struct snode* stack) {
struct snode *S;
if (stack == NULL)
return NULL;
S = stack->bottom;
free(stack);
return S;
}
int
peek(struct snode* stack){
return stack->datum;
}
struct tnode*
create_node(int x){
struct tnode* tmp;
tmp = (struct tnode*)malloc(sizeof(struct tnode));
tmp->datum = x;
tmp->right = NULL;
tmp->left = NULL;
return tmp;
}
void
print_table(struct tnode *AST){
if(AST !=NULL){
print_table(AST->left);
printf("%d ", AST->datum);
print_table(AST->right);
}
}
struct tnode*
build_tree(struct snode *S)
{
struct tnode* root;
if (S==NULL){
return NULL;
}else{
int top = peek(S);
if (top == 65 || top == 83 || top == 88 || top == 68 || top == 77){
return create_node(top);
}else{
root= create_node(top);
root->right = build_tree(pop(S));
root->left = build_tree(pop(S));
return root;
}
}
}
int
main(int argc, const char *argv[])
{
int i = 1;
struct tnode *tree = NULL;
struct snode *stack = NULL;
while (argv[i]!= NULL){
stack = push(stack, argv[i][0]);
i++;
}
tree = build_tree(stack);
print_table(tree);
return EXIT_SUCCESS;
}
I feel like this should work. Everything compiles clean. I run it by saying
./project 5 4 A
and what comes out is
135208 135224 135208 135240 135208 135224 135208 0 135208 135224 135208 52 0 53 0
when it should be
4 65 5
I think this is happening where because of how I'm pushing the elements on to the stack.
EDIT:
I initialized i so that i = 1.
this is the result I am getting.
134480 134496 134480 0 5 4 5
EDIT2:
I decided to get rid of the atol(argv[i]) and change it to just argv[i][0].
see code.
Now my out put is just
65

Finally figured out what was going on!
Here is the newly modified sections that should make your code work (please view the // <== EDIT comments) :
struct tnode*
build_tree(struct snode *S)
{
struct tnode* root;
if (S == NULL)
return NULL;
int top = peek(S);
if (top == 65 || top == 83 || top == 88 || top == 68 || top == 77)
{
root = create_node(top);
S = pop(S); // <== EDIT
root->right = build_tree(S);
S = pop(S); // <== EDIT
root->left = build_tree(S);
return root;
}
root= create_node(top);
return root;
}
int
main(int argc, const char *argv[])
{
int i = 1;
struct tnode *tree = NULL;
struct snode *stack = NULL;
int value = 0;
while (argv[i]!= NULL)
{
if ((value = atoi(argv[i])) == 0) // <== EDIT
value = argv[i][0];
stack = push(stack, value);
i++;
}
tree = build_tree(stack);
print_table(tree);
return EXIT_SUCCESS;
}
I have noticed 3 issues that are causing the values that you have.
First issue :
The result that you are looking for is 4 65 5, which means that if the input is a number (4, 5, etc) you must use the result of atoi(). If the input is not a number, you must use the ASCII value.
To do this, I have used the following lines :
if ((value = atoi(argv[i])) == 0) // <== EDIT
value = argv[i][0];
stack = push(stack, value);
In my implementation of atoi(), when the conversion fails, atoi() returns 0. Therefore, I check the value of the assignment : if atoi() returned 0, use the ASCII value.
Second issue :
The second problem you are having is that the build_tree() function seems to have faulty logic. Since this is a tree, I would expect the character A to be the root and the numbers 4 and 5 to be the branches, like so :
A
/ \
4 5
Your logic seems to reverse the situation, the way you are setup, the numbers become root values and the letters are branches. Reversing your logic solves this :
int top = peek(S);
if (top == 65 || top == 83 || top == 88 || top == 68 || top == 77)
{
root = create_node(top);
S = pop(S); // <== EDIT
root->right = build_tree(S);
S = pop(S); // <== EDIT
root->left = build_tree(S);
return root;
}
root= create_node(top);
return root;
Last issue :
This is the big one. When you call build_tree(), you pass the result of pop() directly. The problem with that, is that you are not affecting the new root value to S. Because of this :
When you call build_tree(pop(S)) the first time, you end up freeing the memory space for S.
Later, when you call build_tree(pop(S)) a second time, you end up calling pop(S) on the initial value (the one that has been freed early on).
The solution is to reassign S when you call pop(), and calling build_tree(S) afterwards.
S = pop(S); // <== EDIT
root->right = build_tree(S);
S = pop(S); // <== EDIT
root->left = build_tree(S);
TL;DR :
I reformated your code a bit and the issue should be fixed now. Copy paste and you should be good.

Related

How can I check if two binary trees contain the same nodes?

I am trying the implement a function which checks whether two binary search trees are equal, order of the nodes not matter. But my implementation does not work.
I am not allowed to flatten the trees into arrays.
this is what I have so far:
int isIdentical(struct Node* root1, struct Node* root2)
{
if (root1 == NULL && root2 == NULL)
return 1;
else if (root1 == NULL || root2 == NULL)
return 0;
else {
if (root1->data == root2->data && isIdentical(root1->left, root2->left)
&& isIdentical(root1->right, root2->right))
return 1;
else
return 0;
}
}
when supplied with trees containing the nodes tree A = 2 4 5 6 and Tree B = 2 5 4 6, the output should be:
1, meaning they are equal, but instead I am getting 0. I am not sure where I am going wrong.
This is how Node is implemeted:
struct Node {
int data;
struct Node* left;
struct Node* right;
};
Make a recursive function that traverses treeA and checks that every item is present in treeB. On failure it abandons the search and returns 0 for failure. It can be your function
int isIdentical(struct Node* root1, struct Node* root2)
If success, call the function again with the arguments for treeA and treeB reversed. The 'check if present' operation can be iterative and inline, because it does not need to backtrack.
Example untried code, to give the idea.
int isAllFound(struct Node* root1, struct Node* root2)
{
// recursive parse of tree 1
if (root1 == NULL)
return 1;
// iterative search of tree 2
int found = 0;
struct Node *root = root2;
while(root != NULL) {
if(root1->data == root->data) {
found = 1;
break;
}
if(root1->data < root->data)
root = root->left;
else
root = root->right;
}
if(!found)
return 0;
// continue recursive parse of tree 1
if(!isAllFound(root1->left, root2))
return 0;
if(!isAllFound(root1->right, root2))
return 0;
return 1;
}
Then call like
if(isAllFound(treeA, treeB) && isAllFound(treeB, treeA))
puts("Success!");
If every item of treeA can be found in treeB, and every item of treeB can be found in treeA then they contain the same data. Provided the keys are unique.
Why do you think they are equal? They are not.
tree A is represented as 2 4 5 6 which I guess you obtained by some sort of pre-order or level-order traversal. If your tree B (2, 5, 4, 6) is equal then with the same sort of traversal you'd obtain same order. They are not equal if the traversal is the same.
Order of nodes doesn't matter:
If the order of the nodes doesn't matter. One thing you could do is do an inorder traversal for both trees and you get a sorted array from both. Then compare both arrays element by element and declare equal or not.
Your function will only compare as equal 2 trees that have exactly the same structure. If the trees are balanced differently, the comparison will return 0 even if the values are identical.
Performing this comparison is non trivial as the trees can have an arbitrary depth if they are not balanced.
You can walk the first tree in depth first order to populate an array and then walk the second tree in depth first order, checking that the values are identical to those in the array.
Here is a simple implementation:
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
size_t tree_length(const struct Node *root) {
return root ? 1 + tree_length(root->left) + tree_length(root->right) : 0;
}
void tree_store(int *array, size_t *pos, struct Node *node) {
if (node) {
tree_store(array, pos, node->left);
array[++*pos - 1] = node->data;
tree_store(array, pos, node->right);
}
}
int tree_check(int *array, size_t *pos, struct Node *node) {
if (node) {
return tree_check(array, pos, node->left)
&& array[++*pos - 1] == node->data
&& tree_check(array, pos, node->right);
} else {
return 1;
}
}
/* compare trees: return 0 if different, 1 if same values, -1 if allocation error */
int isIdentical(const struct Node *root1, const struct Node *root2) {
size_t len1 = tree_length(root1);
size_t len2 = tree_length(root2);
size_t pos;
if (len1 != len2)
return 0;
if (len1 == 0)
return 1;
int *array = malloc(sizeof(*array) * len1);
if (!array)
return -1;
pos = 0;
tree_store(array, &pos, root1);
pos = 0;
int res = tree_check(array, &pos, root2);
free(array);
return res;
}
If you are not allowed to convert the trees to arrays, you could:
normalize both trees, then use your simple comparator, but this will modify the trees and is difficult.
implement a stack based iterator and iterate both trees in parallel.
Here is a simple implementation of the latter:
#include <stddef.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
size_t max_size(size_t a, size_t b) {
return a < b ? b : a;
}
size_t tree_depth(const struct Node *root) {
return root ? 1 + max_size(tree_depth(root->left), tree_depth(root->right)) : 0;
}
int tree_next(const struct Node **stack, size_t *ppos, int *value) {
size_t pos = *ppos;
if (stack[pos] == NULL) {
if (pos == 0)
return 0; // no more values
pos--;
} else {
while (stack[pos]->left) {
stack[pos + 1] = stack[pos]->left;
pos++;
}
}
*value = stack[pos]->data;
stack[pos] = stack[pos]->right;
*ppos = pos;
return 1;
}
/* compare trees: return 0 if different, 1 if same values, -1 if allocation error */
int isIdentical(const struct Node *root1, const struct Node *root2) {
if (root1 == NULL || root2 == NULL)
return root1 == root2;
size_t depth1 = tree_depth(root1);
size_t depth2 = tree_depth(root2);
const struct Node *stack1[depth1];
const struct Node *stack2[depth2];
size_t pos1 = 0;
size_t pos2 = 0;
stack1[pos1++] = root1;
stack2[pos2++] = root2;
for (;;) {
int value1, value2;
int has1 = tree_next(stack1, &pos1, &value1);
int has2 = tree_next(stack2, &pos2, &value2);
if (!has1 && !has2)
return 1;
if (!has1 || !has2 || value1 != value2)
return 0;
}
}

My code for pre-order traversal of Binary Search Tree is working but how is the stack whose each element is a pointer to the structure working?

This is my code of preorder traversal of BST. It's working fine on Ubuntu. But I don't understand one thing.
In the function iterative_preorder(), I actually wanted a stack to store the pointers to the structure that I defined on the top. I want to know the concept that how is it working. Since, while allocating memory to the stack, I didn't specify anywhere separately that stack should contain size number of pointers to the structure.
Like, when we define:
int stack[size];
We know that stack[1] will be the second block in the stack. But here, I used malloc, which actually just makes one block of the size specified as size * sizeof(node *).
So when the program executes:
stack[++top] = root;
How does the program understand that it has to go to the next pointer to the structure in the stack? I hope my question is clear.
I made another small program, based on the confusion that I had. Here, instead of structure, I used int. I tried to create a stack of size 2, which stores pointers to the integer. Here's the code:
#include <stdio.h>
#include <stdlib.h>
void main() {
int** stack = (int**)malloc(2 * sizeof(int*));
printf("%d", *stack[0]);
}
But this code is throwing segmentation fault (core dumped). As both the codes used the same logic, just that this one used int instead of structure, I don't understand why this is throwing error.
#include <stdio.h>
#include <stdlib.h>
int size = 0;
typedef struct mylist {
int data;
struct mylist *left;
struct mylist *right;
} node;
node *root;
void create_root(node *root) {
root = NULL;
}
//Inserting nodes
node *insert(node *root, int val) {
node *ptr, *parentptr, *nodeptr;
ptr = (node*)malloc(sizeof(node));
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if (root == NULL)
root = ptr;
else {
parentptr = NULL;
nodeptr = root;
while (nodeptr != NULL) {
parentptr = nodeptr;
if (val < nodeptr->data)
nodeptr = nodeptr->left;
else
nodeptr = nodeptr->right;
}
if (val < parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return root;
}
void iterative_preorder(node *root) {
if (root != NULL) {
int top = -1;
node **stack = (node**)malloc(size * sizeof(node*));
node *cur;
stack[++top] = root;
while (top > -1) {
cur = stack[top--];
printf("%d\t", cur->data);
if (cur->right != NULL)
stack[++top] = cur->right;
if (cur->left != NULL)
stack[++top] = cur->left;
}
}
}
void main() {
int option, val;
node *ptr;
int flag = 1;
create_root(root);
while (flag != 2) {
printf("\nChoose-\n1-Insert\n2-Iterative Preorder Traversal\n3-Exit\n");
scanf("%d", &option);
switch (option) {
case 1: {
printf("\nEnter the value of new node\n");
size++;
scanf("%d", &val);
root = insert(root, val);
}
break;
case 2:
iterative_preorder(root);
break;
case 3:
flag = 2;
break;
default:
printf("\nWrong entry\n");
}
}
}
Your code has a dereference of uninitialized pointer error.
int** stack = (int**)malloc(2*sizeof(int*));
printf("%d",*stack[0]);
In the above code, stack points to an array of two int pointers, what stack[0] points to? it's not initialized.
A live test of your code is available here segfault. you can modify and test it again.

Number of leaves in binary search tree in C

I am a beginner and working on a C binary search tree.I am trying to do a method that will return the number of leaves in my tree.By leaves I mean a node(parent) that has no child(left/right) Heres my tree struct:
struct Node {
int value;
struct Node *left;
struct Node *right;
};
typedef struct Node TNode;
typedef struct Node *binary_tree;
It is created like this:
binary_tree NewBinaryTree(int value_root) {
binary_tree newRoot = malloc(sizeof(TNode));
if (newRoot) {
newRoot->value = value_root;
newRoot->left = NULL;
newRoot->right = NULL;
}
return newRoot;
}
I add elements to it like:
void Insert(binary_tree *tree, int val) {
if (*tree == NULL) {
*tree = (binary_tree)malloc(sizeof(TNode));
(*tree)->value = val;
(*tree)->left = NULL;
(*tree)->right = NULL;
} else {
if (val < (*tree)->value) {
Insert(&(*tree)->left, val);
} else {
Insert(&(*tree)->right, val);
}
}
}
My actual method to count the number of leaves:
int nbleaves(binary_tree tree)
{
int nb;
if(tree->right==NULL && tree->left ==NULL){
nb=nb+1;
}
printf("%d",nb);
}
Of course this doesnt work first theres no actual loop,however I tried it it doesnt return any error but 0(ex after adding element 2222 and 3 to the tree this function return 0).I dont know how to do this function.
thank you!
Because you MUST initialize nb.
int nb = 0;
Since nb is uninitialized it contains a "random" or "garbage" value so the behavior you see is because that value can be very large. But there is no way to predict what that value is.
NOTE: Don't be "stingy" with white spaces, don't use too many of them but let your code breath a little.
Compare
if(tree->right==NULL && tree->left ==NULL){
nb=nb+1;
}
with
if ((tree->right == NULL) && (tree->left == NULL)) {
nb = nb + 1;
}
Besides initializing as #iharob pointed out, you just need to recurse on the left and right halves of the tree and add that to your total (as said in the comments). This approach worked for me on my tests so I'm not sure what error you're getting when you tried it. Here's my nbleaves() function:
int nbleaves(binary_tree tree)
{
int nb=0;
if(tree->right==NULL && tree->left ==NULL){
nb=nb+1;
}
else {
if(tree->left!=NULL)
nb += nbleaves(tree->left);
if(tree->right!=NULL)
nb += nbleaves(tree->right);
}
return nb;
}
For example, on this test case:
int main() {
binary_tree root=NULL;
root=NewBinaryTree(5);
Insert(&root,3);
Insert(&root,7);
Insert(&root,2);
Insert(&root,8);
Insert(&root,6);
Insert(&root,1);
Insert(&root,4);
Insert(&root,9);
traverse(root); /*Just a function I created for testing*/
printf("%d\n",nbleaves(root));
free_tree(root); /*Also a function I wrote*/
return 0;
}
It produces this output:
5: 3 7
3: 2 4
2: 1 NULL
1: NULL NULL
4: NULL NULL
7: 6 8
6: NULL NULL
8: NULL 9
9: NULL NULL
4
The last line is the leaves count and the rest are outputs of traverse().
For my full program: https://repl.it/Epud/0

Binary Tree segmentation fault after implementing search function

i am trying to write a program that will do the following
-read a file from std in
-read each line, and add each line to a binary tree
*if name is already in binary tree,dont add the name to the tree again but update its count of repititions
-print out the binary tree
the file being read in looks something like
dylan
bob
dylan
randall
randall
so when i print out the binary tree i would like it to print out
bob 1
dylan 2
randall 2
i was able to successfully print out the names without worrying about repetitions. I have commented out the blocks of code that mess my program up which is anything interacting with my search function that i added after the fact to take care of repetitions. The code builds a binary tree with each "leave" being a structure of 4 parts,the name,thecount,and the pointers to left and right childs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char* name;
int count;
struct node* left;
struct node* right;
};
struct node* addNode(char* string);
void insert(struct node *root, char* stringgg);
void preorder(struct node *root);
int search(struct node* leaf,char* string2find);
int main()
{
char buffer[20];
struct node *root = NULL;
while( fgets(buffer, sizeof(buffer), stdin) != NULL )
{
if(root == NULL)
root = addNode(buffer);
else
insert(root,buffer);
}
preorder(root);
}
struct node* addNode(char* string)
{
struct node *temp = malloc(sizeof(struct node));
temp->name = malloc(strlen(string) + 1);
strcpy(temp->name,string);
temp->left = NULL;
temp->right = NULL;
return temp;
}
void insert(struct node *root, char* stringgg)
{
/* int flag = 5;
flag = search(root,stringgg);
if(flag == 1)
return; */
if(strcmp(stringgg,root->name) < 0)
{
if(root->left == NULL)
root->left = addNode(stringgg);
else
insert(root->left, stringgg);
}
else
{
if(root->right == NULL)
root->right = addNode(stringgg);
else
insert(root->right,stringgg);
}
}
/*int search(struct node* leaf,char* string2find)
{
if(strcmp(string2find,leaf->name) == 0)
{
leaf->count = leaf->count + 1;
return 1;
}
else if(strcmp(string2find,leaf->name) < 0)
{
return search(leaf->left,string2find);
}
else
{
return search(leaf->right,string2find);
}
return 0;
} */
void preorder(struct node *root)
{
if(root == NULL)
return;
printf("%s",root->name);
preorder(root->left);
preorder(root->right);
}
the above code prints out all the names even if there already in a tree. I was hoping that someone would be able to point out my search function errors so that it wont cause a segmentation fault when printing. Possible causes may be my inappropriate use of the return function in which i am trying to return to main if flag == 1 which means match was found so dont addnodes. but if flag does not equal 1 no match was found so go about adding nodes.
at main
while( fgets(buffer, sizeof(buffer), stdin) != NULL ){
char *p = strchr(buffer, '\n');
if(p) *p=0;//remove '\n'
at addNode
temp->count = 1;//initialize counter
return temp;
at insert
void insert(struct node *root, char* stringgg){
int cmp_stat = strcmp(stringgg,root->name);
if(cmp_stat == 0)
root->count++;
else if(cmp_stat < 0) {
if(root->left == NULL)
root->left = addNode(stringgg);
else
insert(root->left, stringgg);
} else {
if(root->right == NULL)
root->right = addNode(stringgg);
else
insert(root->right,stringgg);
}
}
at preorder
printf("%s %d\n",root->name, root->count);
The error is in searching for the very first item in the empty tree — you call
search(root, stringgg)
but root is NULL, so in search() you call
strcmp(string2find, leaf->name)
with leaf == NULL and the program crashes.
A cure: do not search BEFORE you update your tree, but rather search TO update it.
struct node* update(struct node* nd, const char* str)
{
int cmp;
// (sub)tree is empty? - create a new node with cnt==1
if(nd == NULL)
return CreateNode(str);
// test if the node found
cmp = strcmp(str, nd->name);
if(cmp == 0) // YES
nd->count ++; // update the counter
else if(cmp < 0) // NO - search in a subtree
nd->left = update(nd->left, str);
else
nd->right = update(nd->right, str);
return nd; // return the updated subtree
}
Then in main() you just update the tree and store it:
root = update(root, buffer);
Actually, the root value will change only once, on the first call, and all subsequent assignments will not change its value. However that makes the code much more readable.

C: Evaluating Expression in a queue not working

I have created a queue using linked list that takes a string in the format 1 + 3 + 5 =or 1-3+2-4 = but the following code which is supposed to do the calculation given the sign and produce the answer is not working right for example if I passed a string like 66 - 31 - 21 + 43 = is passed the answer turns out to be -47 instead of 57. Can someone help me solve it or point to what I'm doing wrong.
void printQueue(struct node* head)
{
struct node* temp;
char *token, *del=" ";
int total = 0;
while (head != NULL)
{
token = strtok(head->data, del);
while (token != NULL) {
int a = strcmp(token, "+");
int b = strcmp(token, "-");
if (a == 0)
{
printf("+");
total = total + *token;
}
else if (b == 0) {
printf("+");
total = total - *token;
}
printf("%s ", token);
token = strtok(NULL, del);
}
printf(" %d\n",subtraction);
head = head->next;
}
}
Your code is ignoring the numbers, adding up character codes of '+' and '-' characters instead. The number -47 is the result of computing -45-45+43: 45 is the character code for the dash; 43 is the character code for the plus.
In order to work properly, make these changes to your code:
Add a sign variable of int type, with the initial value of 1
When you see a plus sign, set the sign to 1
When you see a minus sign, set the sign to -1
When you see a token that is neither a plus nor minus, convert it to an int val (e.g. by calling atoi), and add val * sign to the running total.
This change will make your code work for valid expressions, but it would not fail for some invalid ones. For example, it would take "expressions" such as 1 2 3 as if it were 1+2+3, and so on.
There are a few oddities in your code, here's a few:
Your inner loop that checks if token != NULL always has 1 iteration, it probably should be an if statement
You are searching only for the tokens '+' and '-' and adding their character value to your total, rather than the next number, you are ignoring the tokens that hold numbers
When subtracting you are still printing '+' to the screen
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct node{
int value;
struct node *next;
} Node;
typedef struct queque {
Node *top;
Node *tail;
} Queque;
Queque *Q_new(void){
return calloc(1, sizeof(Queque));
}
void Q_enq(Queque *q, int value){
Node *node = calloc(1, sizeof(Node));
if(!node) exit((printf("failed Securing memory\n"),1));
node->value = value;
q->tail = q->top ? (q->tail->next = node) : (q->top = node);
}
Node *Q_deq(Queque *q){
if(q->top){
Node *node = q->top;
q->top = q->top->next;
return node;
}
return NULL;
}
void Q_drop(Queque *q){
Node *node;
while(node = Q_deq(q))
free(node);
free(q);
}
void strToQ(const char *str, Queque *q){
char *p = (char*)str;
int num;
while(*p){
if(isspace(*p)){
++p;
continue;
} else if(*p == '+' || *p == '-' || *p == '='){//not used to a number sign
Q_enq(q, *p++);
continue;
}
num = strtol(p, &p, 10);
Q_enq(q, num);
}
}
int Q_calc(Queque *q){
int total = 0;
Node *node = Q_deq(q);
if(!node)
return 0;
total = node->value;
free(node);
while(node = Q_deq(q)){
if(node->value == '='){
free(node);
break;
} else {
int op = node->value;
free(node);
node = Q_deq(q);//NOT NULL(if NULL then invalid syntax)
if(op == '+')
total += node->value;
else if(op == '-')
total -= node->value;
free(node);
}
}
return total;
}
int main(){
Queque *q = Q_new();
strToQ("1 + 3 + 5 =", q);
printf("%d\n", Q_calc(q));//9, q to be empty
strToQ("1-3+2-4 =", q);
printf("%d\n", Q_calc(q));//-4
strToQ("66 - 31 - 21 + 43 =", q);
printf("%d\n", Q_calc(q));//57
Q_drop(q);
}

Resources