Upload files to "DS/C/Lab/Week5"

This commit is contained in:
Aadit Agrawal 2024-09-03 00:26:38 +05:30
parent 0589cce66a
commit 0cdc830d54
4 changed files with 277 additions and 0 deletions

View File

@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
char stack[MAX_SIZE];
int top = -1;
void push(char item) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = item;
}
char pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
int precedence(char symbol) {
switch (symbol) {
case '^': return 3;
case '*':
case '/': return 2;
case '+':
case '-': return 1;
default: return 0;
}
}
void infixToPostfix(char infix[], char postfix[]) {
int i, j = 0;
char symbol, next;
for (i = 0; i < strlen(infix); i++) {
symbol = infix[i];
if (isalnum(symbol)) {
postfix[j++] = symbol;
} else if (symbol == '(') {
push(symbol);
} else if (symbol == ')') {
while ((next = pop()) != '(') {
postfix[j++] = next;
}
} else {
while (top > -1 && precedence(stack[top]) >= precedence(symbol)) {
postfix[j++] = pop();
}
push(symbol);
}
}
while (top > -1) {
postfix[j++] = pop();
}
postfix[j] = '\0';
}
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE];
printf("Enter infix expression: ");
fgets(infix, MAX_SIZE, stdin);
infix[strcspn(infix, "\n")] = 0; // Remove newline if present
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}

View File

@ -0,0 +1,66 @@
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int item) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = item;
}
int pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1; // Return a sentinel value instead of exiting
}
return stack[top--];
}
int isDigit(char c) {
return c >= '0' && c <= '9';
}
int evaluatePostfix(char* exp) {
int i, operand1, operand2, result;
for (i = 0; exp[i] != '\0'; i++) {
if (isDigit(exp[i])) {
push(exp[i] - '0');
} else {
operand2 = pop();
operand1 = pop();
switch (exp[i]) {
case '+': push(operand1 + operand2); break;
case '-': push(operand1 - operand2); break;
case '*': push(operand1 * operand2); break;
case '/':
if (operand2 == 0) {
printf("Error: Division by zero\n");
return -1; // Return a sentinel value instead of exiting
}
push(operand1 / operand2);
break;
}
}
}
return pop();
}
int main() {
char exp[MAX_SIZE];
printf("Enter a postfix expression: ");
fgets(exp, MAX_SIZE, stdin);
exp[strcspn(exp, "\n")] = 0; // Remove newline if present
int result = evaluatePostfix(exp);
if (result != -1) {
printf("Result: %d\n", result);
}
return 0;
}

View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
#define MAX_EXPR_LEN 200
char stack[MAX_SIZE][MAX_EXPR_LEN];
int top = -1;
void push(const char* item) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
strcpy(stack[++top], item);
}
char* pop() {
if (top < 0) {
printf("Stack Underflow\n");
return NULL;
}
return stack[top--];
}
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
void postfixToInfix(const char* postfix) {
char op1[MAX_EXPR_LEN], op2[MAX_EXPR_LEN], temp[MAX_EXPR_LEN];
for (int i = 0; postfix[i] != '\0'; i++) {
if (!isOperator(postfix[i])) {
temp[0] = postfix[i];
temp[1] = '\0';
push(temp);
} else {
strcpy(op2, pop());
strcpy(op1, pop());
sprintf(temp, "(%s%c%s)", op1, postfix[i], op2);
push(temp);
}
}
printf("Infix expression: %s\n", stack[top]);
}
int main() {
char postfix[MAX_SIZE];
printf("Enter postfix expression: ");
gets(postfix);
postfixToInfix(postfix);
return 0;
}

View File

@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
Stack* createStack() {
Stack* s = (Stack*)malloc(sizeof(Stack));
s->top = -1;
return s;
}
void push(Stack* s, int x) {
if (s->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
s->items[++(s->top)] = x;
}
int pop(Stack* s) {
if (s->top == -1) {
printf("Stack Underflow\n");
return -1;
}
return s->items[(s->top)--];
}
int isEmpty(Stack* s) {
return s->top == -1;
}
typedef struct {
Stack* s1;
Stack* s2;
} Queue;
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->s1 = createStack();
q->s2 = createStack();
return q;
}
void enqueue(Queue* q, int x) {
push(q->s1, x);
}
int dequeue(Queue* q) {
int x;
if (isEmpty(q->s1) && isEmpty(q->s2)) {
printf("Queue is empty\n");
return -1;
}
if (isEmpty(q->s2)) {
while (!isEmpty(q->s1)) {
x = pop(q->s1);
push(q->s2, x);
}
}
return pop(q->s2);
}
int main() {
Queue* q = createQueue();
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
printf("%d\n", dequeue(q));
printf("%d\n", dequeue(q));
enqueue(q, 4);
printf("%d\n", dequeue(q));
printf("%d\n", dequeue(q));
return 0;
}