Essential C Programming Algorithms and Data Structures

Posted by Anonymous and classified in Computers

Written on in English with a size of 3.78 KB

1. Infix to Postfix Conversion

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

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

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

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

void main() {
    char exp[20];
    int i;
    char x;
    clrscr();
    printf("Enter Infix Expression: ");
    scanf("%s", exp);
    printf("Postfix Expression: ");
    for (i = 0; exp[i] != '\0'; i++) {
        if (isalnum(exp[i])) printf("%c", exp[i]);
        else if (exp[i] == '(') push(exp[i]);
        else if (exp[i] == ')') {
            while ((x = pop()) != '(') printf("%c", x);
        } else {
            while (top != -1 && priority(stack[top]) >= priority(exp[i]))
                printf("%c", pop());
            push(exp[i]);
        }
    }
    while (top != -1) printf("%c", pop());
    getch();
}

2. String Concatenation and Manipulation

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main() {
    char s1[50] = "Flowers";
    char s2[50] = "are beautiful";
    char *p;
    clrscr();
    printf("Length of s1 = %d\n", strlen(s1));
    strcat(s1, " ");
    strcat(s1, s2);
    printf("Concatenation : %s\n", s1);
    printf("Substring : %.3s\n", &s1[2]);
    p = strstr(s2, "are");
    if (p != NULL) {
        strncpy(p, "is ", 3);
    }
    printf("After replace : %s\n", s2);
    getch();
}

3. Hash Table Insertion and Retrieval

#include <stdio.h>
#include <conio.h>

int table[10] = {0};
int hash(int key) { return key % 10; }

void insert(int key) {
    int i = hash(key);
    while (table[i] != 0) i = (i + 1) % 10;
    table[i] = key;
}

void search(int key) {
    int i = hash(key);
    int start = i;
    while (table[i] != 0) {
        if (table[i] == key) {
            printf("%d Found\n", key);
            return;
        }
        i = (i + 1) % 10;
        if (i == start) break;
    }
    printf("%d Not Found\n", key);
}

void main() {
    clrscr();
    insert(10); insert(20); insert(30);
    search(20); search(25);
    getch();
}

4. Binary and Linear Search

#include <stdio.h>
#include <conio.h>

void main() {
    int a[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
    int n = 10, key = 50, i, found = 0, low, high, mid;
    clrscr();
    for (i = 0; i < n; i++) {
        if (a[i] == key) {
            printf("Linear Search : Found at position %d\n", i + 1);
            found = 1; break;
        }
    }
    if (found == 0) printf("Linear Search : Not Found\n");
    low = 0; high = n - 1; found = 0;
    while (low <= high) {
        mid = (low + high) / 2;
        if (a[mid] == key) {
            printf("Binary Search : Found at position %d\n", mid + 1);
            found = 1; break;
        } else if (a[mid] < key) low = mid + 1;
        else high = mid - 1;
    }
    if (found == 0) printf("Binary Search : Not Found\n");
    getch();
}

5. Bubble Sort Implementation

#include <stdio.h>
#include <conio.h>

void bubble_sort(int a[], int n) {
    int i, j, temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (a[j] > a[j + 1]) {
                temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp;
            }
        }
    }
}

void main() {
    int i, n, a[20];
    clrscr();
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    printf("Enter the array elements:\n");
    for (i = 0; i < n; i++) scanf("%d", &a[i]);
    bubble_sort(a, n);
    printf("\nThe sorted elements are:\n");
    for (i = 0; i < n; i++) printf("%d ", a[i]);
    getch();
}

Related entries: