1) Implementing a stack data structure using C structs 2) Creating a function to convert infix notation to postfix notation 3) Evaluating the postfix expression to calculate the final result
3. Date
Nov, 15, 2024
4. Category
"C Programming", "Data Structure"
5. Key Concepts
6. Code Review
typedef struct {
int top;
int items[MAX];
} Stack;
void init(Stack* stack)
{
stack->top = -1;
}
int isEmpty(Stack* stack)
{
return stack->top == -1;
}
int isFull(Stack* stack)
{
return stack->top == MAX - 1;
}
void push(Stack* stack, int value)
{
if (isFull(stack))
{
printf("Stack overflow\n");
exit(1);
}
stack->items[++(stack->top)] = value;
}
int pop(Stack* stack)
{
if (isEmpty(stack))
{
printf("Stack underflow\n");
exit(1);
}
return stack->items[(stack->top)--];
}
int peek(Stack* stack)
{
return stack->items[stack->top];
}
This code defines a Stack structure and implements its basic operations (init, isEmpty, isFull, push, and pop) in C. It uses an array to store elements and a 'top' variable to keep track of the stack's current position, with error handling for overflow and underflow conditions.
int precedence(char op)
{
switch (op)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default: return 0;
}
}
int isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int isDigit(char ch)
{
return ch >= '0' && ch <= '9';
}
This code defines helper functions for expression parsing: precedence() determines the priority of operators, while isOperator() and isDigit() check if a character is an operator or a digit, respectively.
void infixToPostfix(char* infix, char* postfix)
{
Stack stack;
init(&stack);
int j = 0;
for (int i = 0; infix[i] != '\0'; i++)
{
char ch = infix[i];
if (isDigit(ch))
{
while (isDigit(infix[i]))
{
postfix[j++] = infix[i++];
}
postfix[j++] = ' ';
i--;
}
else if (ch == '(')
{
push(&stack, ch);
}
else if (ch == ')')
{
while (!isEmpty(&stack) && peek(&stack) != '(')
postfix[j++] = pop(&stack);
pop(&stack);
}
else if (isOperator(ch))
{
while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(ch))
postfix[j++] = pop(&stack);
push(&stack, ch);
}
}
while (!isEmpty(&stack))
{
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
This function converts an infix expression to a postfix expression using a stack.
int evaluatePostfix(char* postfix) {
Stack stack;
init(&stack);
int i = 0;
while (postfix[i] != '\0') {
if (isDigit(postfix[i])) {
int num_buffer = 0;
while (isDigit(postfix[i])) {
num_buffer = num_buffer * 10 + (postfix[i] - '0');
i++;
}
push(&stack, num_buffer);
}
else if (isOperator(postfix[i])) {
int val2 = pop(&stack);
int val1 = pop(&stack);
switch (postfix[i]) {
case '+': push(&stack, val1 + val2); break;
case '-': push(&stack, val1 - val2); break;
case '*': push(&stack, val1 * val2); break;
case '/': push(&stack, val1 / val2); break;
}
i++;
}
else {
i++;
}
}
return pop(&stack);
}
This function evaluates a postfix expression by iterating through each character, pushing numbers onto a stack, and performing calculations when operators are encountered.