PALINDROME
#include <stdio.h>
#include <string.h>
#define MAX 100
int main() {
char stack[MAX], queue[MAX], str[MAX];
int top = -1;
int front = 0, rear = -1;
int i, len, flag = 1;
printf("Enter string: ");
scanf("%s", str);
len = strlen(str);
/* Insert characters into stack and queue */
for (i = 0; i < len; i++) {
o stack[++top] = str[i]; // push
o queue[++rear] = str[i]; // enqueue
}
/* Compare characters */
for (i = 0; i < len; i++) {
o if (stack[top--] != queue[front++]) {
flag = 0;
break;
o }
}
if (flag)
o printf("String is Palindrome\n");
else
o printf("String is NOT Palindrome\n");
return 0;
}
HEAP SORT
#include <stdio.h>
#define MAX 100
int heap[MAX];
int n = 0;
/* ---------- Swap ---------- */
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
/* ---------- Heapify ---------- */
void heapify(int i) {
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < n && heap[left] > heap[largest])
o largest = left;
if (right < n && heap[right] > heap[largest])
o largest = right;
if (largest != i) {
o swap(&heap[i], &heap[largest]);
o heapify(largest);
}
}
/* ---------- Insert into Heap ---------- */
void insert(int value) {
heap[n] = value;
int i = n;
n++;
while (i > 0 && heap[(i-1)/2] < heap[i]) {
o swap(&heap[i], &heap[(i-1)/2]);
o i = (i-1)/2;
}
}
/* ---------- Build Heap ---------- */
void buildHeap() {
for (int i = n/2 - 1; i >= 0; i--)
o heapify(i);
}
/* ---------- Heap Sort ---------- */
void heapSort() {
int originalSize = n;
buildHeap();
for (int i = n-1; i > 0; i--) {
o swap(&heap[0], &heap[i]);
o n--;
o heapify(0);
}
n = originalSize;
}
/* ---------- Display ---------- */
void display() {
for (int i = 0; i < n; i++)
o printf("%d ", heap[i]);
printf("\n");
}
/* ---------- Main ---------- */
int main() {
int choice, value;
while (1) {
o printf("\n--- HEAP MENU ---\n");
o printf("1. Insert\n");
o printf("2. Display Heap\n");
o printf("3. Heap Sort\n");
o printf("4. Exit\n");
o printf("Enter choice: ");
o scanf("%d", &choice);
o switch(choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Heap: ");
display();
break;
case 3:
heapSort();
printf("Sorted Array: ");
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
o }
}
}
PRIORITY QUEUE (HEAP)
#include <stdio.h>
#define MAX 100
int heap[MAX];
int size = 0;
/* Swap */
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
/* Heapify */
void heapify(int i) {
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < size && heap[left] > heap[largest])
o largest = left;
if (right < size && heap[right] > heap[largest])
o largest = right;
if (largest != i) {
o swap(&heap[i], &heap[largest]);
o heapify(largest);
}
}
/* Insert (Enqueue) */
void insert(int value) {
heap[size] = value;
int i = size;
size++;
while (i > 0 && heap[(i-1)/2] < heap[i]) {
o swap(&heap[i], &heap[(i-1)/2]);
o i = (i-1)/2;
}
}
/* Delete (Dequeue highest priority) */
int deleteMax() {
if (size == 0) {
o printf("Priority Queue Empty\n");
o return -1;
}
int max = heap[0];
heap[0] = heap[size-1];
size--;
heapify(0);
return max;
}
/* Display */
void display() {
for (int i = 0; i < size; i++)
o printf("%d ", heap[i]);
printf("\n");
}
/* Main */
int main() {
int choice, value;
while (1) {
o printf("\n--- PRIORITY QUEUE MENU ---\n");
o printf("1. Insert\n");
o printf("2. Delete Highest Priority\n");
o printf("3. Display\n");
o printf("4. Exit\n");
o printf("Enter choice: ");
o scanf("%d", &choice);
o switch(choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Deleted: %d\n", deleteMax());
break;
case 3:
printf("Priority Queue: ");
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
o }
}
}
BINARY SEARCH
#include <stdio.h>
/* Recursive Binary Search */
int binarySearch(int arr[], int low, int high, int key) {
if (low > high)
o return -1;
int mid = (low + high) / 2;
if (arr[mid] == key)
o return mid;
else if (key < arr[mid])
o return binarySearch(arr, low, mid - 1, key);
else
o return binarySearch(arr, mid + 1, high, key);
}
int main() {
int n, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter sorted elements:\n");
for (int i = 0; i < n; i++)
o scanf("%d", &arr[i]);
printf("Enter key to search: ");
scanf("%d", &key);
int result = binarySearch(arr, 0, n - 1, key);
if (result == -1)
o printf("Element not found\n");
else
o printf("Element found at index %d\n", result);
return 0;
}
TOH
#include <stdio.h>
/* Recursive Function */
void hanoi(int n, char from, char aux, char to) {
if (n == 1) {
o printf("Move disk 1 from %c to %c\n", from, to);
o return;
}
hanoi(n - 1, from, to, aux);
printf("Move disk %d from %c to %c\n", n, from, to);
hanoi(n - 1, aux, from, to);
}
int main() {
int n;
printf("Enter number of disks: ");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
BINARY TREE
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *left, *right;
} node;
/* ---------- Create Node ---------- */
node* createNode(int data) {
node* n = (node*)malloc(sizeof(node));
n->data = data;
n->left = n->right = NULL;
return n;
}
/* ---------- Create Tree (User Driven) ---------- */
void createTree(node* root) {
int ch, val;
printf("Do you want LEFT child of %d? (1-Yes / 0-No): ", root->data);
scanf("%d", &ch);
if (ch == 1) {
o printf("Enter LEFT child value: ");
o scanf("%d", &val);
o root->left = createNode(val);
o createTree(root->left);
}
printf("Do you want RIGHT child of %d? (1-Yes / 0-No): ", root->data);
scanf("%d", &ch);
if (ch == 1) {
o printf("Enter RIGHT child value: ");
o scanf("%d", &val);
o root->right = createNode(val);
o createTree(root->right);
}
}
/* ---------- Traversals ---------- */
void inorder(node* root) {
if (root) {
o inorder(root->left);
o printf("%d ", root->data);
o inorder(root->right);
}
}
void preorder(node* root) {
if (root) {
o printf("%d ", root->data);
o preorder(root->left);
o preorder(root->right);
}
}
void postorder(node* root) {
if (root) {
o postorder(root->left);
o postorder(root->right);
o printf("%d ", root->data);
}
}
/* ---------- Count Functions ---------- */
int countNodes(node* root) {
if (!root) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
int countLeaf(node* root) {
if (!root) return 0;
if (!root->left && !root->right)
o return 1;
return countLeaf(root->left) + countLeaf(root->right);
}
int countNonLeaf(node* root) {
if (!root) return 0;
if (!root->left && !root->right)
o return 0;
return 1 + countNonLeaf(root->left) + countNonLeaf(root->right);
}
/* ---------- Max Value ---------- */
int maxValue(node* root) {
if (!root) return -1;
int l = maxValue(root->left);
int r = maxValue(root->right);
int max = root->data;
if (l > max) max = l;
if (r > max) max = r;
return max;
}
/* ---------- Height ---------- */
int height(node* root) {
if (!root) return 0;
int lh = height(root->left);
int rh = height(root->right);
return (lh > rh ? lh : rh) + 1;
}
/* ---------- Find Deepest Node ---------- */
node* findDeepest(node* root) {
node* temp = root;
while (temp->right)
o temp = temp->right;
return temp;
}
/* ---------- Delete Node ---------- */
void deleteNode(node* root, int key) {
if (!root) return;
node *target = NULL;
node *deepest = root;
/* Find target node */
if (root->data == key)
o target = root;
if (root->left) {
o if (root->left->data == key)
target = root->left;
o deleteNode(root->left, key);
}
if (root->right) {
o if (root->right->data == key)
target = root->right;
o deleteNode(root->right, key);
}
if (target) {
o node* temp = findDeepest(root);
o target->data = temp->data;
o temp->data = -1; // mark deleted
}
}
/* ---------- Main ---------- */
int main() {
int val, del;
printf("Enter ROOT value: ");
scanf("%d", &val);
node* root = createNode(val);
createTree(root);
printf("\nInorder Traversal : ");
inorder(root);
printf("\nPreorder Traversal : ");
preorder(root);
printf("\nPostorder Traversal : ");
postorder(root);
printf("\n\nTotal Nodes : %d", countNodes(root));
printf("\nLeaf Nodes : %d", countLeaf(root));
printf("\nNon-Leaf Nodes : %d", countNonLeaf(root));
printf("\nMaximum Value : %d", maxValue(root));
printf("\nHeight of Tree : %d", height(root));
printf("\n\nEnter value to delete: ");
scanf("%d", &del);
deleteNode(root, del);
printf("\nInorder After Deletion: ");
inorder(root);
printf("\n");
return 0;
}
EXPRESSION TREE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct node {
char data;
struct node *left, *right;
} node;
node* stack[50];
int top = -1;
void push(node* x) {
stack[++top] = x;
}
node* pop() {
return stack[top--];
}
node* newNode(char c) {
node* t = (node*)malloc(sizeof(node));
t->data = c;
t->left = t->right = NULL;
return t;
}
int isOperator(char c) {
return (c=='+' || c=='-' || c=='*' || c=='/');
}
int prec(char c) {
if (c=='+' || c=='-') return 1;
if (c=='*' || c=='/') return 2;
return 0;
}
void infixToPostfix(char infix[], char postfix[]) {
char st[50];
int t = -1, k = 0;
for (int i = 0; infix[i]; i++) {
o char c = infix[i];
o if (isalnum(c))
postfix[k++] = c;
o else if (c == '(')
st[++t] = c;
o else if (c == ')') {
while (st[t] != '(')
postfix[k++] = st[t--];
t--; // remove '('
o }
o else { // operator
while (t != -1 && prec(st[t]) >= prec(c))
postfix[k++] = st[t--];
st[++t] = c;
o }
}
while (t != -1)
o postfix[k++] = st[t--];
postfix[k] = '\0';
}
node* buildTree(char postfix[]) {
for (int i = 0; postfix[i]; i++) {
o char c = postfix[i];
o if (!isOperator(c))
push(newNode(c));
o else {
node* r = pop();
node* l = pop();
node* t = newNode(c);
t->left = l;
t->right = r;
push(t);
o }
}
return pop();
}
void inorder(node* root) {
if (root) {
o inorder(root->left);
o printf("%c ", root->data);
o inorder(root->right);
}
}
void preorder(node* root) {
if (root) {
o printf("%c ", root->data);
o preorder(root->left);
o preorder(root->right);
}
}
void postorder(node* root) {
if (root) {
o postorder(root->left);
o postorder(root->right);
o printf("%c ", root->data);
}
}
int main() {
char infix[50], postfix[50];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix: %s\n", postfix);
node* root = buildTree(postfix);
printf("Inorder : ");
inorder(root);
printf("\n");
printf("Preorder : ");
preorder(root);
printf("\n");
printf("Postorder : ");
postorder(root);
printf("\n");
return 0;
}
BINARY SEARCH TREE
#include <stdio.h>
#include <stdlib.h>
/* Node structure */
struct node
{
int data;
struct node *left;
struct node *right;
};
typedef struct node* NODE;
/* Create new node */
NODE createNode(int value)
{
NODE newNode = (NODE)malloc(sizeof(struct node));
if (newNode == NULL)
{
o printf("Memory not available\n");
o exit(0);
}
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
/* Insert into BST */
NODE insert(NODE root, int value)
{
if (root == NULL)
o return createNode(value);
if (value < root->data)
o root->left = insert(root->left, value);
else if (value > root->data)
o root->right = insert(root->right, value);
else
o printf("Duplicate value not allowed\n");
return root;
}
/* Find minimum (Inorder Successor helper) */
int findMin(NODE root)
{
while (root->left != NULL)
o root = root->left;
return root->data;
}
/* Delete from BST */
NODE deleteNode(NODE root, int value)
{
if (root == NULL)
{
o printf("Value not found\n");
o return NULL;
}
if (value < root->data)
o root->left = deleteNode(root->left, value);
else if (value > root->data)
o root->right = deleteNode(root->right, value);
else
{
o /* Case 1: No child */
o if (root->left == NULL && root->right == NULL)
o {
free(root);
return NULL;
o }
o /* Case 2: One child */
o if (root->left == NULL)
o {
NODE temp = root->right;
free(root);
return temp;
o }
o if (root->right == NULL)
o {
NODE temp = root->left;
free(root);
return temp;
o }
o /* Case 3: Two children */
o int minValue = findMin(root->right);
o root->data = minValue;
o root->right = deleteNode(root->right, minValue);
}
return root;
}
/* Inorder traversal */
void inorder(NODE root)
{
if (root != NULL)
{
o inorder(root->left);
o printf("%d ", root->data);
o inorder(root->right);
}
}
/* Main function */
int main()
{
NODE root = NULL;
int choice, value;
while (1)
{
o printf("\n--- BST MENU ---\n");
o printf("1. Insert\n");
o printf("2. Delete\n");
o printf("3. Display (Inorder)\n");
o printf("4. Exit\n");
o printf("Enter your choice: ");
o scanf("%d", &choice);
o switch (choice)
o {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 3:
printf("BST (Sorted Order): ");
inorder(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
o }
}
return 0;
}
ADD OF LONG +VE INTS
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int digit;
struct node *prev, *next;
} node;
node* insertRear(node* head, int d) {
node* newnode = (node*)malloc(sizeof(node));
newnode->digit = d;
newnode->next = NULL;
newnode->prev = NULL;
if (head == NULL)
o return newnode;
node* temp = head;
while (temp->next != NULL)
o temp = temp->next;
temp->next = newnode;
newnode->prev = temp;
return head;
}
/* Display number */
void display(node* head) {
while (head != NULL) {
o printf("%d", head->digit);
o head = head->next;
}
printf("\n");
}
/* Main */
int main() {
node *num1 = NULL, *num2 = NULL, *res = NULL;
char a[100], b[100];
int carry = 0;
printf("Enter first long number: ");
scanf("%s", a);
printf("Enter second long number: ");
scanf("%s", b);
for (int i = 0; a[i]; i++)
o num1 = insertRear(num1, a[i] - '0');
for (int i = 0; b[i]; i++)
o num2 = insertRear(num2, b[i] - '0');
node *p1 = num1, *p2 = num2;
while (p1->next) p1 = p1->next;
while (p2->next) p2 = p2->next;
while (p1 || p2 || carry) {
o int sum = carry;
o if (p1) { sum += p1->digit; p1 = p1->prev; }
o if (p2) { sum += p2->digit; p2 = p2->prev; }
o res = insertRear(res, sum % 10);
o carry = sum / 10;
}
printf("Result: ");
display(res);
return 0;
}
1️
⃣ Sum of First n Numbers
#include <stdio.h>
int sum(int n) {
if (n == 0)
o return 0;
return n + sum(n - 1);
}
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("Sum = %d", sum(n));
}
✅ 2️
⃣ Factorial
int fact(int n) {
if (n == 0)
o return 1;
return n * fact(n - 1);
}
✅ 3️
⃣ Fibonacci (nth term)
int fib(int n) {
if (n <= 1)
o return n;
return fib(n - 1) + fib(n - 2);
}
✅ 4️
⃣ Binary Search (Recursive)
int binarySearch(int arr[], int low, int high, int key) {
if (low > high)
o return -1;
int mid = (low + high) / 2;
if (arr[mid] == key)
o return mid;
else if (key < arr[mid])
o return binarySearch(arr, low, mid - 1, key);
else
o return binarySearch(arr, mid + 1, high, key);
}
✅ 5️
⃣ Sum of Digits
int sumDigits(int n) {
if (n == 0)
o return 0;
return (n % 10) + sumDigits(n / 10);
}
✅ 6️
⃣ Reverse a Number
int reverse(int n, int rev) {
if (n == 0)
o return rev;
return reverse(n / 10, rev * 10 + n % 10);
}
Call like:
printf("%d", reverse(num, 0));
✅ 7️
⃣ Count Digits
int countDigits(int n) {
if (n == 0)
o return 0;
return 1 + countDigits(n / 10);
}
✅ 8️
⃣ GCD (Euclidean Method)
int gcd(int a, int b) {
if (b == 0)
o return a;
return gcd(b, a % b);
}
✅ 9️
⃣ LCM
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
✅ 🔟 Power of a Number
int power(int base, int exp) {
if (exp == 0)
o return 1;
return base * power(base, exp - 1);
}
✅ 1️
⃣1️
⃣ Check Prime (Recursive)
int isPrime(int n, int i) {
if (n <= 2)
o return (n == 2);
if (n % i == 0)
o return 0;
if (i * i > n)
o return 1;
return isPrime(n, i + 1);
}
Call like:
if(isPrime(n,2))
✅ 1️
⃣2️
⃣ Print 1 to n
void print(int n) {
if (n == 0)
o return;
print(n - 1);
printf("%d ", n);
}
✅ 1️
⃣3️
⃣ Print n to 1
void printReverse(int n) {
if (n == 0)
o return;
printf("%d ", n);
printReverse(n - 1);
}
✅ 1️
⃣4️
⃣ Simple Palindrome (Recursive)
int palindrome(char str[], int start, int end) {
if (start >= end)
o return 1;
if (str[start] != str[end])
o return 0;
return palindrome(str, start + 1, end - 1);
}
INFIX TO PREFIX
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
char stack[MAX];
int top = -1;
/* Push */
void push(char c) {
stack[++top] = c;
}
/* Pop */
char pop() {
return stack[top--];
}
/* Precedence */
int prec(char c) {
if (c == '+' || c == '-') return 1;
if (c == '*' || c == '/') return 2;
if (c == '^') return 3;
return 0;
}
/* Reverse string */
void reverse(char exp[]) {
int i, j;
char temp;
for (i = 0, j = strlen(exp) - 1; i < j; i++, j--) {
o temp = exp[i];
o exp[i] = exp[j];
o exp[j] = temp;
}
}
/* Infix to Prefix */
void infixToPrefix(char infix[], char prefix[]) {
char temp[MAX];
int i, k = 0;
strcpy(temp, infix);
reverse(temp);
for (i = 0; temp[i]; i++) {
o if (temp[i] == '(')
temp[i] = ')';
o else if (temp[i] == ')')
temp[i] = '(';
}
for (i = 0; temp[i]; i++) {
o if (isalnum(temp[i]))
prefix[k++] = temp[i];
o else if (temp[i] == '(')
push(temp[i]);
o else if (temp[i] == ')') {
while (stack[top] != '(')
prefix[k++] = pop();
pop();
o }
o else {
while (top != -1 && prec(stack[top]) >= prec(temp[i]))
prefix[k++] = pop();
push(temp[i]);
o }
}
while (top != -1)
o prefix[k++] = pop();
prefix[k] = '\0';
reverse(prefix);
}
int main() {
char infix[MAX], prefix[MAX];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPrefix(infix, prefix);
printf("Prefix expression: %s\n", prefix);
return 0;
}
INFIX TO POSTFIX
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX 100
char opStack[MAX];
int top = -1;
/* Push operator */
void push(char c) {
opStack[++top] = c;
}
/* Pop operator */
char pop() {
return opStack[top--];
}
/* Precedence */
int prec(char c) {
if (c == '+' || c == '-') return 1;
if (c == '*' || c == '/') return 2;
if (c == '^') return 3;
return 0;
}
/* Infix to Postfix */
void infixToPostfix(char infix[], char postfix[]) {
int i, k = 0;
char c;
for (i = 0; infix[i] != '\0'; i++) {
o c = infix[i];
o if (isdigit(c))
postfix[k++] = c;
o else if (c == '(')
push(c);
o else if (c == ')') {
while (opStack[top] != '(')
postfix[k++] = pop();
pop(); // remove '('
o }
o else {
while (top != -1 && prec(opStack[top]) >= prec(c))
postfix[k++] = pop();
push(c);
o }
}
while (top != -1)
o postfix[k++] = pop();
postfix[k] = '\0';
}
/* Evaluate Postfix */
int evaluatePostfix(char postfix[]) {
int valStack[MAX];
int vtop = -1;
int i, a, b;
for (i = 0; postfix[i] != '\0'; i++) {
o if (isdigit(postfix[i]))
valStack[++vtop] = postfix[i] - '0';
o else {
b = valStack[vtop--];
a = valStack[vtop--];
switch (postfix[i]) {
case '+': valStack[++vtop] = a + b; break;
case '-': valStack[++vtop] = a - b; break;
case '*': valStack[++vtop] = a * b; break;
case '/': valStack[++vtop] = a / b; break;
case '^': valStack[++vtop] = pow(a, b); break;
}
o }
}
return valStack[vtop];
}
int main() {
char infix[MAX], postfix[MAX];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
printf("Evaluation result: %d\n", evaluatePostfix(postfix));
return 0;
}