0% found this document useful (0 votes)
8 views14 pages

Week - 7

The document contains multiple C programs that demonstrate stack operations, including converting infix expressions to postfix, validating parentheses, and evaluating reverse Polish notation (RPN). It also includes functions for calculating expressions with various operators and checking for balanced parentheses. Additionally, it features a function to reverse a prefix of a word based on a specified character.

Uploaded by

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

Week - 7

The document contains multiple C programs that demonstrate stack operations, including converting infix expressions to postfix, validating parentheses, and evaluating reverse Polish notation (RPN). It also includes functions for calculating expressions with various operators and checking for balanced parentheses. Additionally, it features a function to reverse a prefix of a word based on a specified character.

Uploaded by

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

In-Lab – 1(page No: 142)

#include <stdio.h>
#include <ctype.h>

#define MAX 400


char stack[MAX];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

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

int getPriority(char x)
{
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
if (x == '^')
return 3;
return 0;
}

void infixToPostfix(char exp[])


{
int i = 0;
char x;

while (exp[i] != '\0')


{

if (isalnum(exp[i]))
printf("%c", exp[i]);
else if (exp[i] == '(')
push(exp[i]);

else if (exp[i] == ')')


{
while ((x = pop()) != '(' && x != '\0')
printf("%c", x);
}
else
{
while (getPriority(stack[top]) >= getPriority(exp[i]))
printf("%c", pop());
push(exp[i]);
}
i++;
}
while (top != -1)
{
printf("%c", pop());
}
}

int main()
{
int t;
char expression[MAX];
scanf("%d", &t);
while (t--)
{
scanf("%s", expression);
infixToPostfix(expression);
printf("\n");
}
return 0;
}
In-Lab – 2(page No: 144)

#include <stdio.h>
#include <ctype.h>

#define MAX 400


char stack[MAX];
int top = -1;

void push(char x)
{
stack[++top] = x;
}
char pop()
{
if (top == -1)
return '\0';
else
return stack[top--];
}
int getPriority(char x)
{
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
if (x == '^')
return 3;
return 0;
}
void infixToPostfix(char exp[])
{
int i = 0;
char x;

while (exp[i] != '\0')


{

if (isalnum(exp[i]))
printf("%c", exp[i]);

else if (exp[i] == '(')


push(exp[i]);
else if (exp[i] == ')')
{
while ((x = pop()) != '(' && x != '\0')
printf("%c", x);
}
else
{
while (getPriority(stack[top]) >= getPriority(exp[i]))
printf("%c", pop());
push(exp[i]);
}
i++;
}
while (top != -1)
{
printf("%c", pop());
}
}
int main()
{
int n;
scanf("%d", &n);
char expression[n];
scanf("%s", expression); // Reading the infix expression
infixToPostfix(expression); // Convert to RPN
printf("\n");
return 0;
}

In-Lab – 3(page No: 146)


#include <stdio.h>
#include <string.h>
char stack[100005];
int top = -1;

void push(char x)
{
stack[++top] = x;
}
void pop()
{
top--;
}
int isValidParenthesis(char expr[])
{
int i;
top = -1;
for (i = 0; expr[i] != '\0'; i++)
{
if (expr[i] == '(')
{
push(expr[i]);
}
else if (expr[i] == ')')
{
if (top == -1)
return 0;
pop();
}
}
return top == -1;
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
char expr[100005];
scanf("%s", expr);

if (isValidParenthesis(expr))
printf("1\n");
else
printf("0\n");
}
return 0;
}
Post-Lab – 1(page No: 148)

int stack[100000];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int calculate(char* s)
{
int result = 0;
int num = 0;
int sign = 1;
top = -1;
for (int i = 0; s[i] != '\0'; i++)
{
if (isdigit(s[i]))
{
num = num * 10 + (s[i] - '0');
}
else if (s[i] == '+')
{
result += sign * num;
num = 0;
sign = 1;
}
else if (s[i] == '-')
{
result += sign * num;
num = 0;
sign = -1;
}
else if (s[i] == '(')
{
push(result);
push(sign);
result = 0;
sign = 1;
}
else if (s[i] == ')')
{
result += sign * num;
num = 0;
result *= pop();
result += pop();
}
}
result += sign * num;
return result;
}

Post-Lab – 2(page No:150)

int stack[100000];
int top = -1;

void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}

int calculate(char* s)
{
int num = 0;
char operator = '+';
top = -1;

for (int i = 0; s[i] != '\0'; i++)


{
if (isdigit(s[i]))
{
num = num * 10 + (s[i] - '0');
}
if ((!isdigit(s[i]) && s[i] != ' ') || s[i+1] == '\0')
{
if (operator == '+')
{
push(num);
}
else if (operator == '-')
{
push(-num);
}
else if (operator == '*')
{
int temp = pop();
push(temp * num);
}
else if (operator == '/')
{
int temp = pop();
push(temp / num);
}
operator = s[i];
num = 0;
}
}
int result = 0;
while (top != -1)
{
result += pop();
}
return result;
}

Skill Lab-1(page No:152)

int stack[50001];
int top=-1;
void operation(char op)
{
if (top==-1) return;
int a=stack[top--];
if (top==-1) return;
int b=stack[top--];
int res;
switch(op)
{
case '+': res=b+a; break;
case '-': res=b-a; break;
case '*': res=b*a; break;
case '/': res=b/a; break;
}
stack[++top]=res;
}
int evalRPN(char** tokens, int tokensSize)
{
for(int i=0; i<tokensSize; i++)
{
char* s=tokens[i];
if (strlen(s)>1 || isdigit(s[0]))
{
int x=atoi(s);
stack[++top]=x;
}
else operation(s[0]);
}
if (top==-1)
return 0;
return stack[top];
}

Skill Lab-2(page No:154)

#include <stdio.h>
#include <string.h>
char stack[10002];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

void pop()
{
top--;
}

int isBalanced(char expr[])


{
int i;
top=-1;
for (i = 0; expr[i] != '\0'; i++)
{
if (expr[i] == '(' || expr[i] == '{' || expr[i] == '[')
{
push(expr[i]);
}
else if (expr[i] == ')' || expr[i] == '}' || expr[i] == ']')
{
if (top == -1)
return 0;
if ((expr[i] == ')' && stack[top] != '(') ||
(expr[i] == '}' && stack[top] != '{') ||
(expr[i] == ']' && stack[top] != '['))
{
return 0;
}
pop();
}
}
return top == -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char expr[10002];
scanf("%s", expr);

if (isBalanced(expr))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

Skill Lab-3(page No:156)

int minInsertions(char* s)
{
int open = 0, insertions = 0;
int n = strlen(s);
for (int i = 0; i < n; i++)
{
if (s[i] == '(')
{
open++;
}
else
{
if (i + 1 < n && s[i + 1] == ')')
{
i++;
}
else
{
insertions++;
}
if (open > 0)
{
open--;
}
else
{
insertions++;
}
}
}
insertions += open * 2;
return insertions;
}

Skill Lab-4(page No:158)

#include<stdio.h>
#include <ctype.h>
int stack[100002];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
if (top == -1)
return -1;
else
return stack[top--];
}
void postfix(char exp[])
{
int i=0;
int A,B,res,num;
while(exp[i] != '\0')
{
if(isdigit(exp[i]))
{
num = exp[i] - '0';
push(num);
}
else
{
A = pop();
B = pop();
switch(exp[i])
{
case '+': res = B + A;
break;
case '-': res = B - A;
break;
case '*': res = B * A;
break;
case '/':
if(A==0)
{
printf("Error: Division by zero\n");
return;
}
res = B / A;
break;
case '%': if(A==0)
{
printf("Error: Division by zero\n");
return;
}
res = B / A;
break;
case '^': res=(int)pow(B,A);
break;
default:
printf("Error: Unknown operator %c\n", exp[i]);
return;
}
push(res);
}
i++;
}
printf("%d\n",pop());
}
int main()
{
int n;
scanf("%d",&n);
char exp[n];
scanf("%s",exp);
postfix(exp);
return 0;
}

Skill Lab-5(page No:160)

https://leetcode.com/problems/reverse-prefix-of-word/

#define MAX 251


char stack[MAX];
int top = -1;
void push(char ch)
{
stack[++top] = ch;
}
char pop()
{
return stack[top--];
}
char *reversePrefix(char word[], char ch)
{
int i, index = -1;
int len = strlen(word);
for (i = 0; i < len; i++)
{
if (word[i] == ch)
{
index = i;
break;
}
}
if (index == -1)
{
return word;
}
for (i = 0; i <= index; i++)
{
push(word[i]);
}
for (i = 0; i <= index; i++)
{
word[i] = pop();
}
return word;
}

You might also like