DSAPrograms
DSAPrograms
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char stack[MAX];
int isOperator(char c) {
int isOperand(char c) {
int precedence(char c) {
if (c == '^') return 3;
return 0;
void push(char c) {
stack[++top] = c;
char pop() {
if (top == -1) {
return stack[top--];
char peek() {
if (top == -1) {
return -1;
return stack[top];
int i, k = 0;
char symbol;
for (i = 0; infix[i] != '\0'; i++) {
symbol = infix[i];
if (isOperand(symbol)) {
postfix[k++] = symbol;
push(symbol);
postfix[k++] = pop();
} else if (isOperator(symbol)) {
postfix[k++] = pop();
push(symbol);
postfix[k++] = pop();
int i, j;
char temp;
temp = str[i];
str[i] = str[j];
str[j] = temp;
reverse(infix);
if (infix[i] == '(') {
infix[i] = ')';
infix[i] = '(';
char postfix[MAX];
infixToPostfix(infix, postfix);
reverse(postfix);
strcpy(prefix, postfix);
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
infixToPostfix(infix, result);
break;
case 2:
infixToPrefix(infix, result);
break;
default:
printf("Invalid choice!\n");
break;
return 0;
}
FUNCTION isOperator(c):
FUNCTION isOperand(c):
RETURN (c is alphanumeric)
FUNCTION precedence(c):
RETURN 0
FUNCTION isLeftAssociative(c):
FUNCTION push(c):
END IF
FUNCTION pop():
ELSE
FUNCTION peek():
ELSE
FUNCTION infixToPostfix(infix):
END WHILE
END WHILE
END FOR
// Pop all remaining operators from stack and append to postfix
RETURN postfix
FUNCTION reverse(str):
INITIALIZE i = 0, j = length(str) - 1
WHILE i < j:
END WHILE
FUNCTION infixToPrefix(infix):
Reverse(infix)
IF c == '(' THEN
c = ')'
c = '('
END IF
END FOR
postfix = infixToPostfix(infix)
// Reverse the postfix result to get the prefix expression
Reverse(postfix)
RETURN prefix
// Main Program
INPUT infix
INPUT choice
IF choice == 1 THEN
postfix = infixToPostfix(infix)
prefix = infixToPrefix(infix)
ELSE
END IF
END
EVALUATION OF EXPRESSION
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MAX 100
float operandStack[MAX];
char operatorStack[MAX];
operandStack[++topOperand] = value;
float popOperand() {
if (topOperand == -1) {
printf("Stack underflow!\n");
exit(1);
return operandStack[topOperand--];
operatorStack[++topOperator] = operator;
}
char popOperator() {
if (topOperator == -1) {
exit(1);
return operatorStack[topOperator--];
int isOperator(char c) {
int isOperand(char c) {
int precedence(char c) {
if (c == '^') return 3;
return 0;
switch (operator) {
default: return 0;
int i, k = 0;
char symbol;
symbol = infix[i];
if (isOperand(symbol)) {
pushOperator(symbol);
} else if (isOperator(symbol)) {
postfix[k++] = popOperator();
if (isOperand(symbol)) {
pushOperand(symbol - '0');
} else if (isOperator(symbol)) {
// The final result will be the only element left in the operand stack
return popOperand();
int main() {
float result;
printf("Enter an infix expression (use spaces between operands and operators): ");
infixToPostfix(infix, postfix);
result = evaluatePostfix(postfix);
return 0;
FUNCTION isOperand(c):
FUNCTION precedence(c):
IF c == '^' THEN
RETURN 3
RETURN 2
RETURN 1
ELSE
RETURN 0
IF c is an operand THEN:
ADD c to postfix
WHILE operator stack is not empty AND top of stack is not '(':
WHILE operator stack is not empty AND precedence of operator at top of stack >= precedence
of current operator:
END FOR
RETURN postfix
FUNCTION evaluatePostfix(postfix):
IF c is an operand THEN:
END FOR
MAIN FUNCTION:
PRINT "Enter an infix expression (use spaces between operands and operators):"
IMPLEMENTATION OF QUEUE
#include <stdio.h>
#include <stdlib.h>
// Queue structure
struct Queue {
int front;
int rear;
int items[MAX];
};
q->front = -1;
q->rear = -1;
if (q->rear == MAX - 1) {
return 1;
return 0;
return 1;
return 0;
if (isFull(q)) {
} else {
q->front = 0;
q->rear++;
q->items[q->rear] = value;
if (isEmpty(q)) {
} else {
q->front++;
}
return dequeuedValue;
if (isEmpty(q)) {
printf("Queue is empty!\n");
} else {
printf("Queue: ");
printf("\n");
int main() {
struct Queue q;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("4. Exit\n");
switch (choice) {
case 1:
// Enqueue operation
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
// Dequeue operation
value = dequeue(&q);
if (value != -1) {
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
}
}
return 0;
SET Queue.front = -1
SET Queue.rear = -1
FUNCTION isFull(Queue):
RETURN True
ELSE
RETURN False
FUNCTION isEmpty(Queue):
RETURN True
ELSE
RETURN False
IF isFull(Queue) THEN
ELSE:
IF Queue.front == -1 THEN
END IF
INCREMENT Queue.rear
IF isEmpty(Queue) THEN
ELSE:
INCREMENT Queue.front
SET Queue.front = -1
END IF
RETURN dequeuedValue
FUNCTION display(Queue):
IF isEmpty(Queue) THEN
ELSE:
END FOR
MAIN:
CALL initQueue(Queue)
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
CASE 1:
INPUT value
CASE 2:
IF value != -1 THEN
END IF
CASE 3:
CALL display(Queue)
CASE 4:
EXIT
DEFAULT:
END SWITCH
END REPEAT
#include <stdlib.h>
struct Queue {
int items[MAX];
};
q->front = -1;
q->rear = -1;
if (q->rear == MAX - 1) {
return 1;
return 0;
return 1;
return 0;
if (isFull(q)) {
} else {
if (q->front == -1) { // If the queue is empty
q->front = 0;
q->rear++;
q->items[q->rear] = value;
if (isEmpty(q)) {
} else {
q->front++;
return dequeuedValue;
if (isEmpty(q)) {
} else {
return dequeuedValue;
if (isEmpty(q)) {
printf("Queue is empty!\n");
} else {
printf("Queue: ");
printf("\n");
int main() {
struct Queue q;
while (1) {
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
// Enqueue operation
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeueFront(&q);
if (value != -1) {
break;
case 3:
value = dequeueRear(&q);
if (value != -1) {
break;
case 4:
display(&q);
break;
case 5:
exit(0);
default:
return 0;
SET Queue.front = -1
SET Queue.rear = -1
FUNCTION isFull(Queue):
RETURN True
ELSE
RETURN False
FUNCTION isEmpty(Queue):
RETURN True
ELSE
RETURN False
IF isFull(Queue) THEN
ELSE:
IF Queue.front == -1 THEN
END IF
INCREMENT Queue.rear
FUNCTION dequeueFront(Queue):
IF isEmpty(Queue) THEN
ELSE:
INCREMENT Queue.front
SET Queue.front = -1
END IF
RETURN dequeuedValue
FUNCTION dequeueRear(Queue):
IF isEmpty(Queue) THEN
DECREMENT Queue.rear
SET Queue.front = -1
END IF
RETURN dequeuedValue
FUNCTION display(Queue):
IF isEmpty(Queue) THEN
ELSE:
PRINT Queue.items[i]
END FOR
MAIN:
CALL initQueue(Queue)
4. Display Queue
5. Exit
CASE 1:
INPUT value
CASE 2:
IF value != -1 THEN
END IF
CASE 3:
IF value != -1 THEN
END IF
CASE 4:
CALL display(Queue)
CASE 5:
EXIT
DEFAULT:
END SWITCH
END REPEAT
TREE TRAVERSAL
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
};
node->data = value;
return node;
if (root != NULL) {
if (root != NULL) {
if (root != NULL) {
if (root == NULL) {
return newNode(value);
// Otherwise, insert the value in the left or right subtree based on user choice
printf("Enter 'L' to insert %d to the left or 'R' to insert it to the right of %d: ", value, root->data);
char direction;
} else {
return root;
}
int main() {
int value, n, i;
scanf("%d", &n);
scanf("%d", &value);
if (root == NULL) {
root = newNode(value);
} else {
inorder(root);
printf("\n");
preorder(root);
printf("\n");
postorder(root);
printf("\n");
return 0;
RETURN node
RETURN newNode(value)
END IF
PRINT "Enter 'L' to insert [value] to the left or 'R' to insert it to the right of [root.data]: "
READ direction
ELSE
END IF
RETURN root
FUNCTION inorder(root):
END IF
FUNCTION preorder(root):
END IF
FUNCTION postorder(root):
END IF
MAIN:
FOR i = 1 TO n DO:
READ value
ELSE
END FOR
CALL inorder(root)
CALL preorder(root)
CALL postorder(root)
#include <limits.h>
// Function to find the vertex with the minimum key value that has not been included in the MST
min = key[v];
minIndex = v;
return minIndex;
key[i] = INT_MAX;
key[0] = 0;
// Pick the minimum key vertex from the set of vertices not yet included in the MST
mstSet[u] = 1;
// Update key value and parent index of the adjacent vertices of the picked vertex
for (int v = 0; v < V; v++) {
key[v] = graph[u][v];
parent[v] = u;
int main() {
int V, E;
scanf("%d", &V);
scanf("%d", &E);
int u, v, weight;
graph[v][u] = weight;
primMST(graph, V);
return 0;
Edge 1: 0 1 2
Edge 2: 0 3 6
Edge 3: 1 2 3
Edge 4: 1 3 8
Edge 5: 1 4 5
Edge 6: 2 4 7
Edge 7: 3 4 9
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
PSEUDO CODE OF MINIMUM SPANNING TREE
FUNCTION minKey(key[], mstSet[], V):
SET minIndex = -1
SET minIndex = v
END FOR
RETURN minIndex
END FOR
key[i] = INFINITY
END FOR
key[0] = 0
// Pick the vertex u with the minimum key value that is not yet included in MST
mstSet[u] = 1
key[v] = graph[u][v]
parent[v] = u
END IF
END FOR
END FOR
MAIN:
READ V
READ E
READ u, v, weight
graph[u][v] = weight
END FOR
CALL primMST(graph, V)