I need some help here.
basically i have a linked list of strings of the following format.
"integer operator integer ...="
it will contain atleast 2 integers but can be of varied length.
head->expression contains the string. I'm trying to calculate the sum and print it along the string.
Code:
void printQueue(struct node *head)
{
char *del = " =";
char* token;
char* op;
int sum = 0;
while(head != NULL)
{
token = strtok(head->expression,del);
while(token!=NULL)
{
op = strtok(NULL,del);
if (strcmp(op,"+")==0)
{
sum = atoi(token) + atoi(strtok(NULL,del));
}
else if (strcmp(op,"-") == 0)
{
sum = atoi(token) - atoi(strtok(NULL,del));
}
}
printf("%s %d\n", head->expression,sum);
head = head->next;
sum = 0;
}
}
I'm getting seg fault or just plain blank output.
works fine if i just print the strings.