0% found this document useful (0 votes)
14 views3 pages

Lab2 Q3a

Uploaded by

Milica Markovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Lab2 Q3a

Uploaded by

Milica Markovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

// Structure for a node in the stack


typedef struct Node {
char data;
struct Node* next;
} Node;

// Structure for the stack


typedef struct Stack {
Node* top;
int size;
} Stack;

// Function to initialize a stack


Stack* initStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
if (stack == NULL) {
printf("Memory allocation error\n");
exit(EXIT_FAILURE);
}
stack->top = NULL;
stack->size = 0;
return stack;
}

// Function to push a character onto the stack


void push(Stack* stack, char data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation error\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
stack->size++;
}

// Function to pop a character from the stack


void pop(Stack* stack) {
if (stack->size == 0) {
printf("Stack is empty. Cannot pop.\n");
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
stack->size--;
}

// Function to check if brackets are balanced


bool areBracketsBalanced(const char* expr) {
Stack* stack = initStack();
int len = strlen(expr);
for (int i = 0; i < len; i++) {
if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') {
push(stack, expr[i]);
} else if (expr[i] == ')' || expr[i] == ']' || expr[i] == '}') {
if (stack->size == 0) {
// Unbalanced closing bracket
free(stack);
return false;
}
char topChar = stack->top->data;
if ((expr[i] == ')' && topChar == '(') ||
(expr[i] == ']' && topChar == '[') ||
(expr[i] == '}' && topChar == '{')) {
pop(stack);
} else {
// Mismatched opening and closing brackets
free(stack);
return false;
}
}
}

bool balanced = (stack->size == 0);


free(stack);
return balanced;
}

// Function to get the top character of the stack


char top(Stack* stack) {
if (stack->size == 0) {
printf("Stack is empty.\n");
exit(EXIT_FAILURE);
}
return stack->top->data;
}

int main() {
const char* expr1 = "(9*[3*{[(3+3)/5]*7}])";
const char* expr2 = "{3*(2+[3-[4/[6/9]]])}";
const char* expr3 = "((3*(9-(4*(6-5))))";
const char* expr4 = "{2-{3*{6/[[[(((9-0)))]]]}}/7}";

printf("Expression 1: %s\n", expr1);


if (areBracketsBalanced(expr1)) {
printf("Expression 1 is balanced.\n");
} else {
printf("Expression 1 is not balanced.\n");
}

printf("Expression 2: %s\n", expr2);


if (areBracketsBalanced(expr2)) {
printf("Expression 2 is balanced.\n");
} else {
printf("Expression 2 is not balanced.\n");
}

printf("Expression 3: %s\n", expr3);


if (areBracketsBalanced(expr3)) {
printf("Expression 3 is balanced.\n");
} else {
printf("Expression 3 is not balanced.\n");
}

printf("Expression 4: %s\n", expr4);


if (areBracketsBalanced(expr4)) {
printf("Expression 4 is balanced.\n");
} else {
printf("Expression 4 is not balanced.\n");
}

return 0;
}

You might also like