You should be more consistent with your indentation:
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;
}
}
Looking at your string format, I suggest a slightly different approach, i.e., start with the first operand, then parse the operator and next operand together:
Code:
token = strtok(head->expression, del);
if (token)
{
sum = atoi(token);
while ((op = strtok(NULL, del)) && (token = strtok(NULL, del)))
{
if (strcmp(op, "+") == 0)
{
sum += atoi(token);
}
else if (strcmp(op, "-") == 0)
{
sum -= atoi(token);
}
}
}