A stack is a list with the restriction that inserts and deletes can only be performed on the top of the list.
typedef Struct{
int tos;
int stk[size];
} Stack;
The index where the top most/left most value of an array. Starts at index -1.
The actual array of the Stack
The function increases the tos by 1 and adds a value in that index.
void push(Stack* stack, int val){
stack->tos += 1;
stack->stk[stack->tos] = val;
}
The function outputs the value in tos and moves tos lower by 1.
int pop(Stack* stack){
int data = stack->stk[stack->tos];
stack->tos -= 1;
return data;
}
Even though the numbers are not removed in the array because its above tos, we cant really see them anymore.
The function checks if the stack is full by checking if tos is equal to the last index.
bool isFull(int tos){
bool full = false;
if(tos == size - 1){
full = true;
}
return full;
}
The function checks if the stack is empty by checking if tos is equal to -1.
bool isEmpty(int tos){
bool empty = false;
if(tos == -1){
empty = true;
}
return empty;
}
Function will output the value in tos but will not move tos.
int peek(Stack stack){
int data = stack.stk[stack.tos];
return data;
}
Binary Convertion
PostFix Calculator
InFix to PostFix Converter