#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
void swap(void) /* Swap two top elements of stack */
{
double first = pop();
double second = pop();
push(second);
push(first);
}
Answer to this question is answer to your problem: you just need to check if stack state after swap is expected one.
In your case you need to insert two elements in order, perform swap and check if two first elements of stack are in expected order.
YOu have three ways to do so:
1) Pop element and insert it back
2) Implement function which does that
3) Put your hands elbow deep in innards of stack and take value directly
Slight hint: what happens if you remove -- from line 108 in your code?
Before swap:
4 8 9 >
after line 12:
first = 9
4 8 >
after line 13:
first = 9
second = 8
4 >
after line 15:push(second)
first = 9
4 8 >
after line 16:push(first)
4 8 9 >
See the problem now?
Hint: liik at my swap implementation (lines 7-12)