1.
Write functions in C language for array implementation of a stack:
(a) Stack create (b) Stack full (c) Stack empty (d) On top
```c
#include <stdio.h>
#define SIZE 100
int stack[SIZE];
int top = -1;
void stack_create() {
top = -1;
}
int stack_full() {
return top == SIZE - 1;
}
int stack_empty() {
return top == -1;
}
int stack_top() {
if (!stack_empty())
return stack[top];
else
return -1;
}
```
2. Stack operations using linked list:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int value) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = top;
top = newNode;
}
int pop() {
if (top == NULL) return -1;
int val = top->data;
struct Node* temp = top;
top = top->next;
free(temp);
return val;
}
int isEmpty() {
return top == NULL;
}
```
3. Check if an expression has balanced parentheses:
```c
#include <stdio.h>
#include <string.h>
char stack[100];
int top = -1;
void push(char c) { stack[++top] = c; }
char pop() { return (top == -1) ? -1 : stack[top--]; }
int isMatchingPair(char a, char b) {
return (a == '(' && b == ')');
}
int checkBalanced(char* expr) {
for (int i = 0; expr[i] != ''; i++) {
if (expr[i] == '(')
push(expr[i]);
else if (expr[i] == ')') {
if (top == -1 || !isMatchingPair(stack[top], expr[i]))
return 0;
pop();
}
}
return top == -1;
}
int main() {
char expr[] = "(5/3-2 * (4+3)-(8/2))";
printf(checkBalanced(expr) ? "Balanced\n" : "Not Balanced\n");
return 0;
}
```
4. Role of Stack in Function Calls:
Stacks manage function calls by storing return addresses and local variables, allowing
recursion and nested calls.
5. C program to evaluate postfix:
```c
#include <stdio.h>
#include <ctype.h>
int stack[100], top = -1;
void push(int x) { stack[++top] = x; }
int pop() { return stack[top--]; }
int main() {
char exp[] = "23*54*+9-";
int i, op1, op2;
for (i = 0; exp[i] != '\0'; i++) {
if (isdigit(exp[i]))
push(exp[i] - '0');
else {
op2 = pop();
op1 = pop();
switch (exp[i]) {
case '+': push(op1 + op2); break;
case '-': push(op1 - op2); break;
case '*': push(op1 * op2); break;
case '/': push(op1 / op2); break;
}
}
}
printf("Result = %d\n", pop());
return 0;
}
```
6. Convert prefix to infix:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char stack[100][100];
int top = -1;
void push(char *str) {
strcpy(stack[++top], str);
}
char* pop() {
return stack[top--];
}
int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int main() {
char prefix[] = "*+AB-CD";
int len = strlen(prefix);
for (int i = len - 1; i >= 0; i--) {
if (isOperator(prefix[i])) {
char *op1 = pop();
char *op2 = pop();
char temp[100];
sprintf(temp, "(%s%c%s)", op1, prefix[i], op2);
push(temp);
} else {
char operand[2] = {prefix[i], '\0'};
push(operand);
}
}
printf("Infix: %s\n", pop());
return 0;
}
```
7. Infix to postfix algorithm with code:
```c
#include <stdio.h>
#include <ctype.h>
char stack[100];
int top = -1;
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
void infixToPostfix(char* exp) {
for (int i = 0; exp[i]; i++) {
if (isalnum(exp[i])) {
printf("%c", exp[i]);
} else if (exp[i] == '(') {
stack[++top] = exp[i];
} else if (exp[i] == ')') {
while (top != -1 && stack[top] != '(')
printf("%c", stack[top--]);
top--; // remove '('
} else {
while (top != -1 && precedence(stack[top]) >= precedence(exp[i]))
printf("%c", stack[top--]);
stack[++top] = exp[i];
}
}
while (top != -1)
printf("%c", stack[top--]);
}
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
printf("Postfix: ");
infixToPostfix(exp);
return 0;
}
```