Sunday, 5 October 2025

Imp


stack prefix:
#include 
#include 

// Function to check if a pair of opening and closing symbols match
int is_match(char open, char close) {
    if (open == '(' && close == ')') return 1;
    if (open == '{' && close == '}') return 1;
    if (open == '[' && close == ']') return 1;
    return 0;
}

int main() {
    char exp[100];
    char stack[100];
    int top = -1; // Stack pointer
    int i;
    
    printf("Enter expression: ");
    scanf("%s", exp);
    
    for (i = 0; exp[i] != '\0'; i++) {
        char current_char = exp[i];
        
        // Push opening symbols
        if (current_char == '(' || current_char == '{' || current_char == '[') {
            if (top < 99) {
                stack[++top] = current_char; // PUSH
            }
        } 
        // Process closing symbols
        else if (current_char == ')' || current_char == '}' || current_char == ']') {
            
            // Check for closing symbol with empty stack
            if (top == -1) { 
                printf("Expression is Invalid\n");
                return 0;
            }
            
            // Check for correct match
            if (is_match(stack[top], current_char)) {
                top--; // POP
            } else {
                printf("Expression is Invalid\n"); // Mismatch (e.g., {[}] )
                return 0;
            }
        }
    }
    
    // Final check: Stack must be empty
    if (top == -1) {
        printf("Expression is Valid\n");
    } else {
        printf("Expression is Invalid\n"); // Unmatched opening symbol (e.g., (a+b)
    }
    
    return 0;
}

hanoi problem:
#include 

void tower_of_hanoi(int n, char source, char destination, char auxiliary) {
    if (n == 1) {
        printf("Move disk 1 from rod %c to rod %c\n", source, destination);
        return;
    }
    
    tower_of_hanoi(n - 1, source, auxiliary, destination);
    
    printf("Move disk %d from rod %c to rod %c\n", n, source, destination);
    
    tower_of_hanoi(n - 1, auxiliary, destination, source);
}

int main() {
    int num_disks = 3; 
    
    // Start with 3 disks on 'A', moving to 'C', using 'B' as auxiliary
    tower_of_hanoi(num_disks, 'A', 'C', 'B');
    
    return 0;
}

linked list prefix:
#include 
#include 

#define MAX 50
char stack[MAX];
int top = -1;

void push(char op) {
    if (top < MAX - 1) {
        stack[++top] = op;
    }
}

char pop() {
    if (top != -1) {
        return stack[top--];
    }
    return '\0';
}

char peek() {
    if (top != -1) {
        return stack[top];
    }
    return '\0';
}

int is_empty() {
    return (top == -1);
}

int precedence(char op) {
    switch(op) {
        case '+':
        case '-': return 1;
        case '*':
        case '/': return 2;
        case '^': return 3;
        default: return 0;
    }
}

void infix_to_postfix(char *infix) {
    char postfix[MAX];
    int i = 0, j = 0;
    char next_char;
    
    top = -1; 
    
    for (i = 0; infix[i] != '\0'; i++) {
        next_char = infix[i];

        if ((next_char >= 'a' && next_char <= 'z') || (next_char >= 'A' && next_char <= 'Z') || (next_char >= '0' && next_char <= '9')) {
            postfix[j++] = next_char;
        } 
        else if (next_char == '(') {
            push(next_char);
        } 
        else if (next_char == ')') {
            while (!is_empty() && peek() != '(') {
                postfix[j++] = pop();
            }
            if (!is_empty() && peek() == '(') {
                pop();
            } 
        } 
        else { 
            while (!is_empty() && precedence(peek()) >= precedence(next_char)) {
                postfix[j++] = pop();
            }
            push(next_char);
        }
    }

    while (!is_empty()) {
        postfix[j++] = pop();
    }

    postfix[j] = '\0';
    printf("Postfix expression: %s\n", postfix);
}

int main() {
    // Example expression: (a+b)*c-d
    char expression[] = "(a+b)*c-d"; 
    
    infix_to_postfix(expression);
    
    return 0;
}

No comments:

Post a Comment